mirror of
https://github.com/calfmoon/node-factor.git
synced 2026-07-22 05:42:24 +00:00
optimize(calc): use array to store calculations
This commit is contained in:
parent
fb774d3cd4
commit
e4da1ffc39
1 changed files with 25 additions and 1 deletions
26
src/main.ts
26
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
|
// This is a hash map that helps in specifically optimizing the forward tree
|
||||||
private treeOptimizeMap: Map<string, number> = new Map();
|
private treeOptimizeMap: Map<string, number> = new Map();
|
||||||
|
|
||||||
|
// This array helps in optimizing the whole calculation process
|
||||||
|
private storedSized: Array<number> = [];
|
||||||
|
|
||||||
async onload() {
|
async onload() {
|
||||||
await this.loadSettings();
|
await this.loadSettings();
|
||||||
this.addSettingTab(new SampleSettingTab(this.app, this));
|
this.addSettingTab(new SampleSettingTab(this.app, this));
|
||||||
|
|
@ -33,6 +37,20 @@ export default class NodeFactor extends Plugin {
|
||||||
this.calcLoop(nodes);
|
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[]) {
|
private calcLoop(nodes: ObsidianNode[]) {
|
||||||
|
|
@ -45,7 +63,13 @@ export default class NodeFactor extends Plugin {
|
||||||
private updateNodes(nodes: ObsidianNode[]) {
|
private updateNodes(nodes: ObsidianNode[]) {
|
||||||
this.treeOptimizeMap.clear();
|
this.treeOptimizeMap.clear();
|
||||||
nodes.forEach((node, i) => {
|
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;
|
node.weight = weight;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue