From e4da1ffc394f04c6369f7dfdbca18ca95fbb37e7 Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Tue, 11 Mar 2025 17:05:28 +0545 Subject: [PATCH] optimize(calc): use array to store calculations --- src/main.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 7875775..3d73a77 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,6 +11,10 @@ export default class NodeFactor extends Plugin { // This is a hash map that helps in specifically optimizing the forward tree private treeOptimizeMap: Map = new Map(); + + // This array helps in optimizing the whole calculation process + private storedSized: Array = []; + async onload() { await this.loadSettings(); this.addSettingTab(new SampleSettingTab(this.app, this)); @@ -33,6 +37,20 @@ export default class NodeFactor extends Plugin { this.calcLoop(nodes); }), ); + + // clear cache when there is change in the vault + this.registerEvent( + this.app.vault.on("create", () => (this.storedSized = [])), + ); + this.registerEvent( + this.app.vault.on("modify", () => (this.storedSized = [])), + ); + this.registerEvent( + this.app.vault.on("delete", () => (this.storedSized = [])), + ); + this.registerEvent( + this.app.vault.on("rename", () => (this.storedSized = [])), + ); } private calcLoop(nodes: ObsidianNode[]) { @@ -45,7 +63,13 @@ export default class NodeFactor extends Plugin { private updateNodes(nodes: ObsidianNode[]) { this.treeOptimizeMap.clear(); nodes.forEach((node, i) => { - const weight = this.calcNodeWeight(node); + let weight: number; + if (this.storedSized[i] != undefined) { + weight = this.storedSized[i]; + } else { + weight = this.calcNodeWeight(node); + this.storedSized[i] = weight; + } node.weight = weight; }); }