Skip to content

Commit

Permalink
manifold: node.destroy now destroys all children first. closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
progrium committed Mar 6, 2023
1 parent ec59f4b commit 1a6872e
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/manifold/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,13 @@ export class Node {
}

destroy() {
this.module.destroy(this);
// TODO: also walk components of nodes
const nodes = [];
this.walk((n: Node): boolean => {
nodes.push(n);
return false;
});
nodes.reverse().forEach(n => this.module.destroy(n));
}

addComponent(obj: any) {
Expand Down Expand Up @@ -160,12 +166,22 @@ export class Node {
return this.module.find([this.getPath(), path].join("/"));
}

walk(cb: (n: Node) => boolean): boolean {
if (cb(this)) {
return true;
}
for (const child of this.getChildren()) {
if (child.walk(cb)) return true;
}
return false;
}

// getComponentsInChildren
// getComponentsInParents
// getAncestors
// getPath

// walk

// duplicate?
}

Expand Down Expand Up @@ -289,15 +305,8 @@ export class Module {
}

walk(cb: (n: Node) => boolean) {
const walkChildren = (n: Node, cb: (n: Node) => boolean): boolean => {
if (cb(n)) return true;
for (const child of n.getChildren()) {
if (walkChildren(child, cb)) return true;
}
return false;
}
for (const root of this.roots()) {
if (walkChildren(root, cb)) return;
if (root.walk(cb)) return;
}
}
}
Expand Down

0 comments on commit 1a6872e

Please # to comment.