|
1 |
| -/** |
2 |
| - * This file contains core module functionality. |
3 |
| - * |
4 |
| - * It exports an anonymous |
5 |
| - * @function |
6 |
| - * that is invoked on |
7 |
| - * @param snap --> Current snapshot |
8 |
| - * @param mode --> Current mode (jumping i.e. time-traveling, locked, or paused) |
9 |
| - * and @returns a function to be invoked on the rootContainer HTMLElement |
10 |
| - * |
11 |
| - * @function updateSnapShotTree |
12 |
| - * --> Middleware #1: Updates snap object with latest snapshot |
13 |
| - * |
14 |
| - * @function sendSnapshot |
15 |
| - * --> Middleware #2: Gets a copy of the current snap.tree and posts a message to the window |
16 |
| - * |
17 |
| - * @function changeSetState |
18 |
| - * @param component : stateNode property on a stateful class component's FiberNode object |
19 |
| - * --> Binds class component setState method to the component |
20 |
| - * --> Injects middleware into class component's setState method |
21 |
| - * |
22 |
| - * @function changeUseState |
23 |
| - * @param component : memoizedState property on a stateful functional component's FiberNode object |
24 |
| - * --> Binds functional component dispatch method to the component |
25 |
| - * --> Injects middleware into component's dispatch method |
26 |
| - * Note: dispatch is hook equivalent to setState() |
27 |
| - * |
28 |
| - * @function traverseHooks |
29 |
| - * @param memoizedState : memoizedState property on a stateful fctnl component's FiberNode object |
30 |
| - * --> Helper function to traverse through memoizedState |
31 |
| - * --> Invokes @changeUseState on each stateful functional component |
32 |
| - * |
33 |
| - * @function createTree |
34 |
| - * @param currentFiber : a FiberNode object |
35 |
| - * --> Recursive function to traverse from FiberRootNode and create |
36 |
| - * an instance of custom Tree class and build up state snapshot |
37 |
| - */ |
38 |
| - |
39 |
| -/* eslint-disable no-underscore-dangle */ |
40 |
| -/* eslint-disable func-names */ |
41 |
| -/* eslint-disable no-use-before-define */ |
42 |
| -/* eslint-disable no-param-reassign */ |
43 |
| - |
44 |
| -const Tree = require('./tree'); |
45 |
| -const componentActionsRecord = require('./masterState'); |
46 |
| - |
47 |
| -module.exports = (snap, mode) => { |
48 |
| - let fiberRoot = null; |
49 |
| - let astHooks; |
50 |
| - let concurrent = false; // flag to check if we are in concurrent mode |
51 |
| - |
52 |
| - async function sendSnapshot() { |
53 |
| - // Don't send messages while jumping or while paused |
54 |
| - if (mode.jumping || mode.paused) return; |
55 |
| - // console.log('PAYLOAD: before cleaning', snap.tree); |
56 |
| - const payload = snap.tree.cleanTreeCopy();// snap.tree.getCopy(); |
57 |
| - // console.log('PAYLOAD: after cleaning', payload); |
58 |
| - try { |
59 |
| - await window.postMessage({ |
60 |
| - action: 'recordSnap', |
61 |
| - payload, |
62 |
| - }); |
63 |
| - } catch (e) { |
64 |
| - console.log('failed to send postMessage:', e); |
65 |
| - } |
66 |
| - } |
67 |
| - |
68 |
| - // Carlos: Injects instrumentation to update our state tree every time |
69 |
| - // a hooks component changes state |
70 |
| - function traverseHooks(memoizedState) { |
71 |
| - const hooksComponents = []; |
72 |
| - while (memoizedState && memoizedState.queue) { |
73 |
| - // Carlos: these two are legacy comments, we should look into them later |
74 |
| - // prevents useEffect from crashing on load |
75 |
| - // if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates |
76 |
| - if (memoizedState.memoizedState) { |
77 |
| - console.log('memoizedState in traverseHooks is:', memoizedState); |
78 |
| - hooksComponents.push({ |
79 |
| - component: memoizedState.queue, |
80 |
| - state: memoizedState.memoizedState, |
81 |
| - }); |
82 |
| - } |
83 |
| - // console.log('GOT STATE', memoizedState.memoizedState); |
84 |
| - memoizedState = memoizedState.next !== memoizedState |
85 |
| - ? memoizedState.next : null; |
86 |
| - } |
87 |
| - return hooksComponents; |
88 |
| - } |
89 |
| - |
90 |
| - // Carlos: This runs after EVERY Fiber commit. It creates a new snapshot, |
91 |
| - // |
92 |
| - function createTree(currentFiber, tree = new Tree('root')) { |
93 |
| - // Base case: child or sibling pointed to null |
94 |
| - if (!currentFiber) return tree; |
95 |
| - console.log("createTree -> currentFiber", currentFiber); |
96 |
| - |
97 |
| - // These have the newest state. We update state and then |
98 |
| - // called updateSnapshotTree() |
99 |
| - const { |
100 |
| - sibling, |
101 |
| - stateNode, |
102 |
| - child, |
103 |
| - memoizedState, |
104 |
| - elementType, |
105 |
| - tag, |
106 |
| - actualDuration, |
107 |
| - // actualStartTime, |
108 |
| - // selfBaseDuration, |
109 |
| - // treeBaseDuration, |
110 |
| - } = currentFiber; |
111 |
| - |
112 |
| - let index; |
113 |
| - // Check if node is a stateful component |
114 |
| - if (stateNode && stateNode.state && (tag === 0 || tag === 1)) { |
115 |
| - // Save component's state and setState() function to our record for future |
116 |
| - // time-travel state changing. Add record index to snapshot so we can retrieve. |
117 |
| - index = componentActionsRecord.saveNew(stateNode.state, stateNode); |
118 |
| - tree.appendChild(stateNode.state, elementType.name, index, actualDuration); // Add component to tree |
119 |
| - } else { |
120 |
| - // grab stateless components here |
121 |
| - } |
122 |
| - |
123 |
| - // Check if node is a hooks function |
124 |
| - if (memoizedState && (tag === 0 || tag === 1 || tag === 10)) { |
125 |
| - if (memoizedState.queue) { |
126 |
| - const hooksComponents = traverseHooks(memoizedState); |
127 |
| - hooksComponents.forEach(c => { |
128 |
| - if (elementType.name) { |
129 |
| - index = componentActionsRecord.saveNew(c.state, c.component); |
130 |
| - tree.appendChild(c.state, elementType.name ? elementType.name : 'nameless', index, actualDuration); |
131 |
| - } |
132 |
| - }); |
133 |
| - } |
134 |
| - } |
135 |
| - |
136 |
| - // Recurse on siblings |
137 |
| - createTree(sibling, tree); |
138 |
| - // Recurse on children |
139 |
| - if (tree.children.length > 0) { |
140 |
| - createTree(child, tree.children[0]); |
141 |
| - } else { |
142 |
| - createTree(child, tree); |
143 |
| - } |
144 |
| - |
145 |
| - return tree; |
146 |
| - } |
147 |
| - |
148 |
| - // ! BUG: skips 1st hook click |
149 |
| - function updateSnapShotTree() { |
150 |
| - /* let current; |
151 |
| - // If concurrent mode, grab current.child |
152 |
| - if (concurrent) { |
153 |
| - // we need a way to wait for current child to populate |
154 |
| - const promise = new Promise((resolve, reject) => { |
155 |
| - setTimeout(() => resolve(fiberRoot.current.child), 400); |
156 |
| - }); |
157 |
| - current = await promise; |
158 |
| - current = fiberRoot.current.child; |
159 |
| - } else { |
160 |
| - current = fiberRoot.current; |
161 |
| - } */ |
162 |
| - const { current } = fiberRoot; // Carlos: get rid of concurrent mode for now |
163 |
| - |
164 |
| - // console.log('FIBER COMMITTED, new fiber is:', util.inspect(current, false, 4)); |
165 |
| - // fs.appendFile('fiberlog.txt', util.inspect(current, false, 10)); |
166 |
| - snap.tree = createTree(current); // Carlos: pass new hooks state here? |
167 |
| - } |
168 |
| - |
169 |
| - return async container => { |
170 |
| - // Point fiberRoot to FiberRootNode |
171 |
| - if (container._internalRoot) { |
172 |
| - fiberRoot = container._internalRoot; |
173 |
| - concurrent = true; |
174 |
| - } else { |
175 |
| - const { |
176 |
| - _reactRootContainer: { _internalRoot }, |
177 |
| - _reactRootContainer, |
178 |
| - } = container; |
179 |
| - // Only assign internal root if it actually exists |
180 |
| - fiberRoot = _internalRoot || _reactRootContainer; |
181 |
| - // console.log('_reactRootContainer is:', _reactRootContainer); |
182 |
| - // console.log('linkFiber.js, fiberRoot:', fiberRoot); |
183 |
| - } |
184 |
| - const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__; |
185 |
| - const reactInstance = devTools ? devTools.renderers.get(1) : null; |
186 |
| - const overrideHookState = reactInstance ? reactInstance.overrideHookState : null; |
187 |
| - |
188 |
| - if (reactInstance && reactInstance.version) { |
189 |
| - devTools.onCommitFiberRoot = (function (original) { |
190 |
| - return function (...args) { |
191 |
| - fiberRoot = args[1]; |
192 |
| - updateSnapShotTree(); |
193 |
| - sendSnapshot(); |
194 |
| - return original(...args); |
195 |
| - }; |
196 |
| - }(devTools.onCommitFiberRoot)); |
197 |
| - } |
198 |
| - updateSnapShotTree(); |
199 |
| - // Send the initial snapshot once the content script has started up |
200 |
| - // This message is sent from contentScript.js in chrome extension bundles |
201 |
| - window.addEventListener('message', ({ data: { action } }) => { |
202 |
| - if (action === 'contentScriptStarted') { |
203 |
| - sendSnapshot(); |
204 |
| - } |
205 |
| - }); |
206 |
| - }; |
207 |
| -}; |
0 commit comments