From fb774d3cd4e5f7b0e7fc5aa684d64282a896e82f Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Tue, 11 Mar 2025 16:41:36 +0545 Subject: [PATCH] refactor(calc): extract update loop into a function --- src/main.ts | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/main.ts b/src/main.ts index c8a3e00..7875775 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,10 +7,10 @@ export default class NodeFactor extends Plugin { settings: NodeFactorSettings; // stops loop when graph isn't open - updateLoop: boolean; + private updateLoop: boolean; // This is a hash map that helps in specifically optimizing the forward tree - treeOptimizeMap: Map = new Map(); + private treeOptimizeMap: Map = new Map(); async onload() { await this.loadSettings(); this.addSettingTab(new SampleSettingTab(this.app, this)); @@ -30,30 +30,26 @@ export default class NodeFactor extends Plugin { const nodes: ObsidianNode[] = leaf.view.renderer.nodes; if (nodes.length === 0) return; - const start = performance.now(); - - nodes.forEach((node) => { - const weight = this.calcNodeWeight(node); - node.weight = weight; - }); - - // this.calcLoop(nodes); - - console.log(performance.now() - start); + this.calcLoop(nodes); }), ); } private calcLoop(nodes: ObsidianNode[]) { setTimeout(() => { - nodes.forEach((node) => { - const weight = this.calcNodeWeight(node); - node.weight = weight; - }); + this.updateNodes(nodes); if (this.updateLoop) this.calcLoop(nodes); }, 10); } + private updateNodes(nodes: ObsidianNode[]) { + this.treeOptimizeMap.clear(); + nodes.forEach((node, i) => { + const weight = this.calcNodeWeight(node); + node.weight = weight; + }); + } + private calcNodeWeight(node: ObsidianNode): number { const settings = this.settings; let weight = 0;