From bdf1a746b2971e5c1049decd7895bef948b7baf2 Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Sun, 9 Mar 2025 21:46:45 +0545 Subject: [PATCH] refactor(calc): use setTimeout instead of setInterval to loop this was done as per suggestion of https://developer.mozilla.org/en-US/docs/Web/API/Window/setInterval , this makes it so that even if the task insides takes longer the tasks won't stack --- src/main.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main.ts b/src/main.ts index 3b228b8..00cdc76 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,28 +24,29 @@ export default class NodeFactor extends Plugin { const start = performance.now(); const treeOptimizeMap: Map = new Map(); - nodes.forEach((node) => { const weight = this.calcNodeWeight(node, treeOptimizeMap); node.weight = weight; }); - // setInterval(() => { - // const treeOptimizeMap: Map = new Map(); - // nodes.forEach((node) => { - // const weight = this.calcNodeWeight( - // node, - // treeOptimizeMap, - // ); - // node.weight = weight; - // }); - // }, 10); + // this.calcLoop(nodes); console.log(performance.now() - start); }), ); } + private calcLoop(nodes: ObsidianNode[]) { + setTimeout(() => { + const treeOptimizeMap: Map = new Map(); + nodes.forEach((node) => { + const weight = this.calcNodeWeight(node, treeOptimizeMap); + node.weight = weight; + }); + this.calcLoop(nodes); + }, 10); + } + private calcNodeWeight( node: ObsidianNode, treeOptimizeMap: Map,