From d3663e9de81ddbdb52d252b4b98e277825721302 Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Sat, 28 Mar 2026 01:31:01 +0545 Subject: [PATCH] fix: size being 0 if graph view is initially open (#2) this basically fixes the issue by increasing the time it takes for the update cycle to run. Therefore giving obsidian more time to the link between nodes. --- src/main.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main.ts b/src/main.ts index 5d754eb..64361d0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,7 +7,7 @@ export default class NodeFactor extends Plugin { settings: NodeFactorSettings; // stops loop when graph isn't open - private updateLoop: boolean; + private loopId: NodeJS.Timer; async onload() { await this.loadSettings(); @@ -19,23 +19,25 @@ export default class NodeFactor extends Plugin { .getLeavesOfType("graph") .first(); if (!leaf) { - this.updateLoop = false; + clearInterval(this.loopId); return; } - this.updateLoop = true; // @ts-ignore const nodes: ObsidianNode[] = leaf.view.renderer.nodes; if (nodes.length === 0) return; + this.clearSizeCache(); this.calcLoop(nodes); }), ); // clear cache when there is change in the vault - this.registerEvent( - this.app.vault.on("create", () => this.clearSizeCache()), - ); + this.app.workspace.onLayoutReady(() => { + this.registerEvent( + this.app.vault.on("create", () => this.clearSizeCache()), + ); + }); this.registerEvent( this.app.vault.on("modify", () => this.clearSizeCache()), ); @@ -48,19 +50,17 @@ export default class NodeFactor extends Plugin { } async onunload() { - this.updateLoop = false; + clearInterval(this.loopId); } private calcLoop(nodes: ObsidianNode[]) { - setTimeout(() => { + this.loopId = setInterval(() => { this.updateNodes(nodes); - if (this.updateLoop) this.calcLoop(nodes); - }, 10); + }, 500); } private storedSize: Map = new Map(); private updateNodes(nodes: ObsidianNode[]) { - this.treeOptimizeMap.clear(); nodes.forEach((node, i) => { let weight: number; if (this.storedSize.get(node.id) != undefined) { @@ -130,6 +130,7 @@ export default class NodeFactor extends Plugin { clearSizeCache() { this.storedSize.clear(); + this.treeOptimizeMap.clear(); } async loadSettings() {