fix: size being 0 if graph view is initially open (#2)

this basically fixes the issue by increasing the time it takes for the
update cycle to run. Therefore giving obsidian more time to the link
between nodes.
This commit is contained in:
Kritagya Bhattarai (CalfMoon) 2026-03-28 01:31:01 +05:45
parent 70b51e9a3d
commit d3663e9de8

View file

@ -7,7 +7,7 @@ export default class NodeFactor extends Plugin {
settings: NodeFactorSettings; settings: NodeFactorSettings;
// stops loop when graph isn't open // stops loop when graph isn't open
private updateLoop: boolean; private loopId: NodeJS.Timer;
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
@ -19,23 +19,25 @@ export default class NodeFactor extends Plugin {
.getLeavesOfType("graph") .getLeavesOfType("graph")
.first(); .first();
if (!leaf) { if (!leaf) {
this.updateLoop = false; clearInterval(this.loopId);
return; return;
} }
this.updateLoop = true;
// @ts-ignore // @ts-ignore
const nodes: ObsidianNode[] = leaf.view.renderer.nodes; const nodes: ObsidianNode[] = leaf.view.renderer.nodes;
if (nodes.length === 0) return; if (nodes.length === 0) return;
this.clearSizeCache();
this.calcLoop(nodes); this.calcLoop(nodes);
}), }),
); );
// clear cache when there is change in the vault // clear cache when there is change in the vault
this.registerEvent( this.app.workspace.onLayoutReady(() => {
this.app.vault.on("create", () => this.clearSizeCache()), this.registerEvent(
); this.app.vault.on("create", () => this.clearSizeCache()),
);
});
this.registerEvent( this.registerEvent(
this.app.vault.on("modify", () => this.clearSizeCache()), this.app.vault.on("modify", () => this.clearSizeCache()),
); );
@ -48,19 +50,17 @@ export default class NodeFactor extends Plugin {
} }
async onunload() { async onunload() {
this.updateLoop = false; clearInterval(this.loopId);
} }
private calcLoop(nodes: ObsidianNode[]) { private calcLoop(nodes: ObsidianNode[]) {
setTimeout(() => { this.loopId = setInterval(() => {
this.updateNodes(nodes); this.updateNodes(nodes);
if (this.updateLoop) this.calcLoop(nodes); }, 500);
}, 10);
} }
private storedSize: Map<string, number> = new Map(); private storedSize: Map<string, number> = new Map();
private updateNodes(nodes: ObsidianNode[]) { private updateNodes(nodes: ObsidianNode[]) {
this.treeOptimizeMap.clear();
nodes.forEach((node, i) => { nodes.forEach((node, i) => {
let weight: number; let weight: number;
if (this.storedSize.get(node.id) != undefined) { if (this.storedSize.get(node.id) != undefined) {
@ -130,6 +130,7 @@ export default class NodeFactor extends Plugin {
clearSizeCache() { clearSizeCache() {
this.storedSize.clear(); this.storedSize.clear();
this.treeOptimizeMap.clear();
} }
async loadSettings() { async loadSettings() {