From a9b433ba2a9175e129ca63c81e0f52eaf5dfcbef Mon Sep 17 00:00:00 2001 From: "Kritagya Bhattarai (CalfMoon)" Date: Sat, 28 Mar 2026 18:05:38 +0545 Subject: [PATCH] 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;