perf(calc): don't run calculations if graph view isn't loaded

This commit is contained in:
Kritagya Bhattarai (CalfMoon) 2026-03-28 18:05:38 +05:45
parent b319d722fd
commit a9b433ba2a

View file

@ -6,23 +6,13 @@ import NodeFactorSettingTab from "./settings";
export default class NodeFactor extends Plugin {
settings: NodeFactorSettings;
private nodes: Array<ObsidianNode>;
async onload() {
await this.loadSettings();
this.addSettingTab(new NodeFactorSettingTab(this.app, this));
this.registerEvent(
this.app.workspace.on("active-leaf-change", () => {
const leaf = this.app.workspace
.getLeavesOfType("graph")
.first();
// exit loop if the loaded page isn't grah
if (!leaf) return;
// @ts-ignore
this.nodes = leaf.view.renderer.nodes;
if (this.nodes.length === 0) return;
// we can still use cache when only active leaf changes
this.updateGraph();
}),
);
@ -47,13 +37,20 @@ export default class NodeFactor extends Plugin {
);
}
async onunload() {
// clearInterval(this.loopId);
}
async onunload() {}
private sizeCache: Map<string, number> = new Map();
private updateGraph() {
const nodes = this.nodes;
const leaf = this.app.workspace.getLeavesOfType("graph").first();
// don't run if graph page isn't loaded
if (!leaf) return;
// @ts-ignore
let nodes: Array<ObsidianNode> = leaf.view.renderer.nodes;
if (nodes.length === 0) return;
// Slight delay in calculations is needed to fix node size
// if graph view is initially opened when opening obsidian
setTimeout(() => {
nodes.forEach((node, _i) => {
let weight = 0;