From 7d197494925d90594e2640e6619c2c3bb09830f9 Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Sat, 28 Mar 2026 17:50:59 +0545 Subject: [PATCH 1/5] refactor: remove looping as it had no practical function --- src/main.ts | 55 ++++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/main.ts b/src/main.ts index beb6b88..67b53e1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,30 +6,24 @@ import NodeFactorSettingTab from "./settings"; export default class NodeFactor extends Plugin { settings: NodeFactorSettings; - // interval Id of continously running loop - private loopId: NodeJS.Timer; - + private nodes: Array; async onload() { await this.loadSettings(); this.addSettingTab(new NodeFactorSettingTab(this.app, this)); this.registerEvent( - this.app.workspace.on("layout-change", () => { + 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) { - clearInterval(this.loopId); - return; - } + if (!leaf) return; // @ts-ignore - const nodes: Array = leaf.view.renderer.nodes; - if (nodes.length === 0) return; + this.nodes = leaf.view.renderer.nodes; + if (this.nodes.length === 0) return; - this.clearSizeCache(); - this.updateLoop(nodes); + this.updateGraph(); }), ); @@ -54,25 +48,25 @@ export default class NodeFactor extends Plugin { } async onunload() { - clearInterval(this.loopId); + // clearInterval(this.loopId); } - private updateLoop(nodes: Array) { - this.loopId = setInterval(() => this.updateNodes(nodes), 500); - } - - private storedSize: Map = new Map(); - private updateNodes(nodes: Array) { - nodes.forEach((node, _i) => { - let weight = 0; - if (this.storedSize.get(node.id) != undefined) { - weight = this.storedSize.get(node.id) as number; - } else { - weight = this.calcNodeWeight(node); - this.storedSize.set(node.id, weight); - } - node.weight = weight; - }); + private sizeCache: Map = new Map(); + private updateGraph() { + const nodes = this.nodes; + this.treeOptimizeMap.clear(); + setTimeout(() => { + nodes.forEach((node, _i) => { + let weight = 0; + if (this.sizeCache.get(node.id) != undefined) { + weight = this.sizeCache.get(node.id) as number; + } else { + weight = this.calcNodeWeight(node); + this.sizeCache.set(node.id, weight); + } + node.weight = weight; + }); + }, 500); } private calcNodeWeight(node: ObsidianNode): number { @@ -131,8 +125,9 @@ export default class NodeFactor extends Plugin { } clearSizeCache() { - this.storedSize.clear(); + this.sizeCache.clear(); this.treeOptimizeMap.clear(); + this.updateGraph(); } async loadSettings() { From b319d722fd9abbabd8f46705d281e29097f94c0b Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Sat, 28 Mar 2026 17:59:23 +0545 Subject: [PATCH 2/5] fix(calc): calculation error associated with treeOptimizeMap --- src/main.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main.ts b/src/main.ts index 67b53e1..0701474 100644 --- a/src/main.ts +++ b/src/main.ts @@ -54,7 +54,6 @@ export default class NodeFactor extends Plugin { private sizeCache: Map = new Map(); private updateGraph() { const nodes = this.nodes; - this.treeOptimizeMap.clear(); setTimeout(() => { nodes.forEach((node, _i) => { let weight = 0; @@ -95,7 +94,6 @@ export default class NodeFactor extends Plugin { return file.stat.size; } - private treeOptimizeMap: Map = new Map(); private fwdNodeTreeSize( node: ObsidianNode, antiLoopSet: Set, @@ -103,11 +101,6 @@ export default class NodeFactor extends Plugin { let size = 0; antiLoopSet.add(node.id); - const sizeMap = this.treeOptimizeMap.get(node.id); - if (sizeMap != undefined) { - return sizeMap as number; - } - Object.entries(node.forward).forEach(([key, value]) => { // @ts-ignore const childNode: ObsidianNode = value.target; @@ -120,13 +113,11 @@ export default class NodeFactor extends Plugin { size += childSize; }); - this.treeOptimizeMap.set(node.id, size); return size; } clearSizeCache() { this.sizeCache.clear(); - this.treeOptimizeMap.clear(); this.updateGraph(); } From a9b433ba2a9175e129ca63c81e0f52eaf5dfcbef Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Sat, 28 Mar 2026 18:05:38 +0545 Subject: [PATCH 3/5] perf(calc): don't run calculations if graph view isn't loaded --- src/main.ts | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/main.ts b/src/main.ts index 0701474..8b87c0b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,23 +6,13 @@ import NodeFactorSettingTab from "./settings"; export default class NodeFactor extends Plugin { settings: NodeFactorSettings; - private nodes: Array; 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 = 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 = 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; From 777de23d9afe5248927da18393594ebf9eacdb4c Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Sat, 28 Mar 2026 18:15:07 +0545 Subject: [PATCH 4/5] refactor: rename clearSizeCache() -> recalulateSize() this represents what the function is doing better --- src/main.ts | 10 +++++----- src/settings.ts | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main.ts b/src/main.ts index 8b87c0b..1ca4014 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,17 +23,17 @@ export default class NodeFactor extends Plugin { // create for every existing file when initally loading obsidian // https://docs.obsidian.md/plugins/guides/load-time#Pitfalls this.registerEvent( - this.app.vault.on("create", () => this.clearSizeCache()), + this.app.vault.on("create", () => this.recalculateSize()), ); }); this.registerEvent( - this.app.vault.on("modify", () => this.clearSizeCache()), + this.app.vault.on("modify", () => this.recalculateSize()), ); this.registerEvent( - this.app.vault.on("delete", () => this.clearSizeCache()), + this.app.vault.on("delete", () => this.recalculateSize()), ); this.registerEvent( - this.app.vault.on("rename", () => this.clearSizeCache()), + this.app.vault.on("rename", () => this.recalculateSize()), ); } @@ -113,7 +113,7 @@ export default class NodeFactor extends Plugin { return size; } - clearSizeCache() { + recalculateSize() { this.sizeCache.clear(); this.updateGraph(); } diff --git a/src/settings.ts b/src/settings.ts index e03f806..aa84cf9 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -23,7 +23,7 @@ export default class NodeFactorSettingTab extends PluginSettingTab { .setDynamicTooltip() .setValue(this.plugin.settings.fwdMultiplier) .onChange(async (value) => { - this.plugin.clearSizeCache(); + this.plugin.recalculateSize(); this.plugin.settings.fwdMultiplier = value; await this.plugin.saveSettings(); }), @@ -38,7 +38,7 @@ export default class NodeFactorSettingTab extends PluginSettingTab { toggle .setValue(this.plugin.settings.fwdTree) .onChange(async (value) => { - this.plugin.clearSizeCache(); + this.plugin.recalculateSize(); this.plugin.settings.fwdTree = value; await this.plugin.saveSettings(); }), @@ -53,7 +53,7 @@ export default class NodeFactorSettingTab extends PluginSettingTab { .setDynamicTooltip() .setValue(this.plugin.settings.bwdMultiplier) .onChange(async (value) => { - this.plugin.clearSizeCache(); + this.plugin.recalculateSize(); this.plugin.settings.bwdMultiplier = value; await this.plugin.saveSettings(); }), From 783607292d3807e185469352a78118b6e31bfe02 Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Sat, 28 Mar 2026 18:28:06 +0545 Subject: [PATCH 5/5] perf(update): prevent graph update from running multiple times at once --- src/main.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 1ca4014..6b3f691 100644 --- a/src/main.ts +++ b/src/main.ts @@ -40,6 +40,7 @@ export default class NodeFactor extends Plugin { async onunload() {} private sizeCache: Map = new Map(); + private timeoutId: NodeJS.Timeout; private updateGraph() { const leaf = this.app.workspace.getLeavesOfType("graph").first(); // don't run if graph page isn't loaded @@ -51,7 +52,8 @@ export default class NodeFactor extends Plugin { // Slight delay in calculations is needed to fix node size // if graph view is initially opened when opening obsidian - setTimeout(() => { + clearTimeout(this.timeoutId); + this.timeoutId = setTimeout(() => { nodes.forEach((node, _i) => { let weight = 0; if (this.sizeCache.get(node.id) != undefined) {