Skip to content

Commit 60b5ceb

Browse files
authored
Merge pull request #32 from oslabs-beta/rydang/componentname
tree snapshot is better formatted and now contains name
2 parents 31a6c8f + ba8d2b8 commit 60b5ceb

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

package/timeJump.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = (origin, mode) => {
1313
const originNode = traverseTree(origin.tree, coords);
1414

1515
// set the state of the origin tree
16-
originNode.component.setState(target.component.state, () => {
16+
originNode.component.setState(target.state, () => {
1717
// iterate through new children once state has been set
1818
target.children.forEach((child, i) => {
1919
jump(child, coords.concat(i));

package/tree.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
class Tree {
2-
constructor(component) {
2+
constructor(component, useStateInstead = false, name) {
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+
this.name = name;
11+
}
712
this.children = [];
813
}
914

@@ -14,12 +19,9 @@ class Tree {
1419
}
1520

1621
// 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-
22+
getCopy(copy = new Tree('root', true, 'root')) {
2123
// copy state of children
22-
copy.children = this.children.map(child => new Tree({ state: child.component.state }));
24+
copy.children = this.children.map(child => new Tree(child.component.state, true, child.component.constructor.name));
2325

2426
// copy children's children recursively
2527
this.children.forEach((child, i) => child.getCopy(copy.children[i]));
@@ -30,10 +32,13 @@ class Tree {
3032
print() {
3133
const children = ['children: '];
3234
this.children.forEach((child) => {
33-
children.push(child.component.state);
35+
children.push(child.state || child.component.state);
3436
});
35-
if (children.length === 1) console.log(this.component.state);
36-
else console.log(this.component.state, ...children);
37+
if (this.name) console.log(this.name);
38+
if (children.length === 1) {
39+
console.log(this.state || this.component.state);
40+
}
41+
else console.log(this.state || this.component.state, ...children);
3742
this.children.forEach((child) => {
3843
child.print();
3944
});

0 commit comments

Comments
 (0)