|
| 1 | +import 'core-js'; |
| 2 | +/* eslint-disable no-multiple-empty-lines */ |
| 3 | +/* eslint-disable max-classes-per-file */ |
| 4 | +/* eslint-disable no-console */ |
| 5 | +/* eslint-disable no-param-reassign */ |
| 6 | +// import * as reactWorkTags from './reactWorkTags'; |
| 7 | + |
| 8 | +const Flatted = require('flatted'); |
| 9 | + |
| 10 | +let copyInstances: number = 0; |
| 11 | +const circularComponentTable = new Set<Tree> (); |
| 12 | + |
| 13 | +// removes unserializable state data such as functions |
| 14 | +function scrubUnserializableMembers(tree: Tree): Tree { |
| 15 | + Object.entries(tree.state).forEach(keyValuePair => { |
| 16 | + if (typeof keyValuePair[1] === 'function') tree.state[keyValuePair[0]] = 'function'; |
| 17 | + }); |
| 18 | + return tree; |
| 19 | +} |
| 20 | + |
| 21 | +// this is the current snapshot that is being sent to the snapshots array. |
| 22 | +class Tree { |
| 23 | + state: string | {}; |
| 24 | + name: string; |
| 25 | + componentData: {}; |
| 26 | + children: (Tree | string)[] ; |
| 27 | + parent: Tree |
| 28 | + constructor(state : string | {}, name: string = 'nameless', componentData: {} = {}) { |
| 29 | + this.state = state === 'root' ? 'root' : JSON.parse(JSON.stringify(state)); |
| 30 | + this.name = name; |
| 31 | + this.componentData = componentData ? JSON.parse(JSON.stringify(componentData)) : {}; |
| 32 | + this.children = []; |
| 33 | + this.parent = null; // ref to parent so we can add siblings |
| 34 | + } |
| 35 | + |
| 36 | + addChild(state: string | {} , name: string, componentData: {}): Tree { |
| 37 | + const newChild: Tree = new Tree(state, name, componentData); |
| 38 | + newChild.parent = this; |
| 39 | + this.children.push(newChild); |
| 40 | + return newChild; |
| 41 | + } |
| 42 | + |
| 43 | + addSibling(state: string | {} , name: string, componentData: {}): Tree { |
| 44 | + const newSibling: Tree = new Tree(state, name, componentData); |
| 45 | + newSibling.parent = this.parent; |
| 46 | + this.parent.children.push(newSibling); |
| 47 | + return newSibling; |
| 48 | + } |
| 49 | + |
| 50 | + cleanTreeCopy(): Tree { |
| 51 | + // Clear circular component table only on first call, not recursive ones |
| 52 | + if (copyInstances === 0) { |
| 53 | + copyInstances++; |
| 54 | + circularComponentTable.clear(); |
| 55 | + } |
| 56 | + // creates copy of present node |
| 57 | + let copy: Tree = new Tree(this.state, this.name, this.componentData); |
| 58 | + delete copy.parent; |
| 59 | + circularComponentTable.add(this); |
| 60 | + copy = scrubUnserializableMembers(copy); |
| 61 | + |
| 62 | + // copy.children = this.children; |
| 63 | + |
| 64 | + // creates copy of each child of the present node |
| 65 | + |
| 66 | + copy.children = this.children.map((child: Tree): Tree | string => { |
| 67 | + if (!circularComponentTable.has(child)) { |
| 68 | + return child.cleanTreeCopy(); |
| 69 | + } |
| 70 | + return 'circular'; |
| 71 | + }); |
| 72 | + |
| 73 | + |
| 74 | + // returns copy |
| 75 | + copyInstances--; |
| 76 | + return copy; |
| 77 | + } |
| 78 | + |
| 79 | + // print out the tree structure in the console |
| 80 | + // DEV: Process may be different for useState components |
| 81 | + // BUG FIX: Don't print the Router as a component |
| 82 | + // Change how the children are printed |
| 83 | +// print() { |
| 84 | +// const children = ['children: ']; |
| 85 | +// // DEV: What should we push instead for components using hooks (it wouldn't be state) |
| 86 | +// // if this.children is always initialized to empty array, when would there ever be anything to iterate through here? |
| 87 | +// this.children.forEach((child: any) => { |
| 88 | +// children.push(child.state || child.component.state); |
| 89 | +// }); |
| 90 | +// if (this.name) console.log('this.name if exists: ', this.name); |
| 91 | +// if (children.length === 1) { |
| 92 | +// console.log(`children length 1. ${this.state ? 'this.state: ' : 'this.component.state: '}`, this.state || this.component.state); |
| 93 | +// } else console.log(`children length !== 1. ${this.state ? 'this.state: ' : 'this.component.state, children: '}`, this.state || this.component.state, ...children); |
| 94 | +// this.children.forEach((child: any) => { |
| 95 | +// child.print(); |
| 96 | +// }); |
| 97 | +// } |
| 98 | +} |
| 99 | + |
| 100 | +export default Tree; |
0 commit comments