Skip to content

Commit 6a8cfb8

Browse files
committed
refactored how trees send copys
1 parent e09570d commit 6a8cfb8

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

package/tree.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
class Tree {
2-
constructor(component) {
2+
constructor(component, useStateInstead = false) {
33
// special case when component is root
44
// give it a special state = 'root'
55
// a setState function that just calls the callback instantly
6-
this.component = (component === 'root') ? { state: 'root', setState: (partial, callback) => callback() } : component;
6+
if (!useStateInstead) {
7+
this.component = (component === 'root') ? { state: 'root', setState: (partial, callback) => callback() } : component;
8+
} else {
9+
this.state = component;
10+
}
711
this.children = [];
812
}
913

@@ -14,12 +18,9 @@ class Tree {
1418
}
1519

1620
// deep copies only the state of each component and creates a new tree
17-
getCopy(copy = new Tree(null)) {
18-
const { state } = this.component;
19-
if (!copy.component) copy.component = { state };
20-
21+
getCopy(copy = new Tree('root', true)) {
2122
// copy state of children
22-
copy.children = this.children.map(child => new Tree({ state: child.component.state }));
23+
copy.children = this.children.map(child => new Tree(child.component.state, true));
2324

2425
// copy children's children recursively
2526
this.children.forEach((child, i) => child.getCopy(copy.children[i]));

0 commit comments

Comments
 (0)