Skip to content

Commit b542fdf

Browse files
committed
update to diff jsx
2 parents e3424b8 + c3ee9de commit b542fdf

24 files changed

+526
-279
lines changed

dev-reactime/index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44
*/
55
import 'core-js';
66
import 'regenerator-runtime/runtime';
7-
import { exist } from 'acorn-jsx/xhtml';
7+
import linkFiberStart from './linkFiber';
8+
import timeJumpStart from './timeJump';
89

910
// * State snapshot object initialized here
1011
const snapShot = {
11-
tree: null,
12-
unfilteredTree: null
12+
tree: null,
13+
unfilteredTree: null
1314
};
1415

15-
1616
const mode = {
1717
jumping: false,
1818
paused: false,
1919
locked: false,
2020
};
2121

22-
const linkFiber = require('./linkFiber')(snapShot, mode);
23-
console.log('import timeJump in index.js:', JSON.parse(JSON.stringify(snapShot)));
24-
const timeJump = require('./timeJump')(snapShot, mode);
22+
// const linkFiber = require('./linkFiber')(snapShot, mode);
23+
// console.log('import timeJump in index.js:', JSON.parse(JSON.stringify(snapShot)));
24+
// const timeJump = require('./timeJump')(snapShot, mode);
25+
26+
27+
28+
const linkFiber = linkFiberStart(snapShot, mode);
29+
const timeJump = timeJumpStart(snapShot, mode);
2530

2631

2732
function getRouteURL(node) {
@@ -40,7 +45,7 @@ function getRouteURL(node) {
4045
window.addEventListener('message', ({ data: { action, payload } }) => {
4146
switch (action) {
4247
case 'jumpToSnap':
43-
console.log('payload in jumpToSnap', payload);
48+
// console.log('payload in jumpToSnap', payload);
4449
timeJump(payload); // * This sets state with given payload
4550
// Get the pathname from payload and add new entry to browser history
4651
// MORE: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
@@ -59,5 +64,8 @@ window.addEventListener('message', ({ data: { action, payload } }) => {
5964
}
6065
});
6166

67+
console.log('index.js: loading reactime');
68+
linkFiber();
69+
6270
// module.exports = linkFiber;
63-
export default linkFiber;
71+
// export default linkFiber;

dev-reactime/linkFiber.js

Lines changed: 111 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,52 @@
4444
/* eslint-disable no-use-before-define */
4545
/* eslint-disable no-param-reassign */
4646

47-
const Tree = require('./tree');
48-
const componentActionsRecord = require('./masterState');
47+
// const Tree = require('./tree').default;
48+
// const componentActionsRecord = require('./masterState');
49+
import Tree from './tree';
50+
import componentActionsRecord from './masterState';
4951

50-
module.exports = (snap, mode) => {
52+
const DEBUG_MODE = false;
53+
54+
const alwaysLog = console.log;
55+
56+
console.log = (original => {
57+
return (...args) => {
58+
if (DEBUG_MODE) original(...args);
59+
}
60+
})(console.log);
61+
62+
63+
const circularComponentTable = new Set();
64+
65+
// module.exports = (snap, mode) => {
66+
export default (snap, mode) => {
5167
let fiberRoot = null;
5268

53-
async function sendSnapshot() {
69+
function sendSnapshot() {
70+
alwaysLog('sendSnapshot called');
5471
// Don't send messages while jumping or while paused
72+
circularComponentTable.clear();
73+
// console.log('sending snapshot');
5574
if (mode.jumping || mode.paused) return;
56-
console.log('PAYLOAD: before cleaning', snap.tree);
75+
// console.log('PAYLOAD: before cleaning', snap.tree);
76+
77+
if (!snap.tree) {
78+
// console.log('snapshot empty, sending root');
79+
snap.tree = new Tree('root', 'root');
80+
}
5781
const payload = snap.tree.cleanTreeCopy();// snap.tree.getCopy();
58-
console.log('PAYLOAD: after cleaning', payload);
59-
try {
60-
await window.postMessage({
82+
83+
// console.log('PAYLOAD: after cleaning', payload);
84+
// try {
85+
// await window.postMessage({
86+
window.postMessage({
6187
action: 'recordSnap',
6288
payload,
6389
});
64-
} catch (e) {
65-
console.log('failed to send postMessage:', e);
66-
}
90+
// } catch (e) {
91+
// console.log('failed to send postMessage:', e);
92+
// }
6793
}
6894

6995
// Carlos: Injects instrumentation to update our state tree every time
@@ -75,13 +101,11 @@ module.exports = (snap, mode) => {
75101
// prevents useEffect from crashing on load
76102
// if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
77103
if (memoizedState.memoizedState) {
78-
console.log('memoizedState in traverseHooks is:', memoizedState);
79104
hooksStates.push({
80105
component: memoizedState.queue,
81106
state: memoizedState.memoizedState,
82107
});
83108
}
84-
// console.log('GOT STATE', memoizedState.memoizedState);
85109
memoizedState = memoizedState.next !== memoizedState
86110
? memoizedState.next : null;
87111
}
@@ -90,8 +114,11 @@ module.exports = (snap, mode) => {
90114

91115
// Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
92116
//
93-
function createTree(currentFiber, tree = new Tree('root')) {
117+
118+
let ctRunning = 0;
119+
function createTree(currentFiber, tree = new Tree('root', 'root'), fromSibling = false) {
94120
// Base case: child or sibling pointed to null
121+
console.log('createTree: creating tree');
95122
if (!currentFiber) return null;
96123
if (!tree) return tree;
97124

@@ -115,22 +142,20 @@ module.exports = (snap, mode) => {
115142
let componentFound = false;
116143

117144
// Check if node is a stateful setState component
118-
if (stateNode && stateNode.state && (tag === 0 || tag === 1)) {
119-
console.log('in create tree if');
120-
console.log('this is currentFiber from createTree', currentFiber);
145+
if (stateNode && stateNode.state && (tag === 0 || tag === 1 || tag ===2)) { // { || tag === 2)) {
121146
// Save component's state and setState() function to our record for future
122147
// time-travel state changing. Add record index to snapshot so we can retrieve.
148+
console.log('createTree() found setState component');
123149
componentData.index = componentActionsRecord.saveNew(stateNode.state, stateNode);
124150
newState = stateNode.state;
125151
componentFound = true;
126152
}
127153

128154
// Check if node is a hooks useState function
129155
let hooksIndex;
130-
if (memoizedState && (tag === 0 || tag === 1 || tag === 10)) {
131-
console.log('in create tree if');
132-
console.log('this is currentFiber from createTree', currentFiber);
156+
if (memoizedState && (tag === 0 || tag === 1 || tag === 2 || tag === 10)) {
133157
if (memoizedState.queue) {
158+
console.log('createTree() found hooks component');
134159
// Hooks states are stored as a linked list using memoizedState.next,
135160
// so we must traverse through the list and get the states.
136161
// We then store them along with the corresponding memoizedState.queue,
@@ -151,9 +176,10 @@ module.exports = (snap, mode) => {
151176
}
152177

153178
// This grabs stateless components
154-
if (!componentFound && (tag === 0 || tag === 1)) {
179+
/*
180+
if (!componentFound && (tag === 0 || tag === 1 || tag === 2)) {
155181
newState = 'stateless';
156-
}
182+
}*/
157183

158184
// Adds performance metrics to the component data
159185
componentData = {
@@ -164,52 +190,70 @@ module.exports = (snap, mode) => {
164190
treeBaseDuration,
165191
};
166192

167-
if (componentFound) {
168-
tree.addChild(newState, elementType.name ? elementType.name : elementType, componentData);
169-
} else if (newState === 'stateless') {
170-
tree.addChild(newState, elementType.name ? elementType.name : elementType, componentData);
193+
let newNode = null;
194+
if (componentFound || newState === 'stateless') {
195+
if (fromSibling) {
196+
console.log('createTree(), relevant component found in sibling');
197+
newNode = tree.addSibling(newState,
198+
elementType ? elementType.name : 'nameless',
199+
componentData);
200+
} else {
201+
console.log('createTree(), relevant component found in child');
202+
newNode = tree.addChild(newState,
203+
elementType ? elementType.name : 'nameless',
204+
componentData);
205+
}
206+
} else {
207+
console.log('createTree(), no new relevant nodes, continuing from same node')
208+
newNode = tree;
171209
}
172210

173211
// Recurse on children
174-
if (child) {
212+
213+
if (child && !circularComponentTable.has(child)) {
175214
// If this node had state we appended to the children array,
176215
// so attach children to the newly appended child.
177216
// Otherwise, attach children to this same node.
178-
if (tree.children.length > 0) {
179-
createTree(child, tree.children[tree.children.length - 1]);
180-
} else {
181-
createTree(child, tree);
182-
}
217+
console.log('going into child');
218+
circularComponentTable.add(child);
219+
createTree(child, newNode);
183220
}
184221
// Recurse on siblings
185-
if (sibling) createTree(sibling, tree);
222+
if (sibling && !circularComponentTable.has(sibling)) {
223+
console.log('going into sibling');
224+
circularComponentTable.add(sibling);
225+
createTree(sibling, newNode, true);
226+
}
227+
228+
if (circularComponentTable.has(child)) {
229+
console.log('found circular child, exiting tree loop');
230+
}
231+
232+
if (circularComponentTable.has(sibling)) {
233+
console.log('found circular sibling, exiting tree loop');
234+
}
186235

236+
// // console.log('linkFiber.js: processed children and sibling, returning tree');
187237
return tree;
188238
}
189239

190-
// ! BUG: skips 1st hook click
240+
let updateSnapshotTreeCount = 0;
191241
function updateSnapShotTree() {
192-
/* let current;
193-
// If concurrent mode, grab current.child
194-
if (concurrent) {
195-
// we need a way to wait for current child to populate
196-
const promise = new Promise((resolve, reject) => {
197-
setTimeout(() => resolve(fiberRoot.current.child), 400);
198-
});
199-
current = await promise;
200-
current = fiberRoot.current.child;
201-
} else {
202-
current = fiberRoot.current;
203-
} */
204-
const { current } = fiberRoot; // Carlos: get rid of concurrent mode for now
242+
// console.log('updateSnapshotTree(), checking if fiberRoot updated');
205243

206-
// console.log('FIBER COMMITTED, new fiber is:', util.inspect(current, false, 4));
207-
// fs.appendFile('fiberlog.txt', util.inspect(current, false, 10));
208-
snap.tree = createTree(current); // Carlos: pass new hooks state here?
244+
updateSnapshotTreeCount++;
245+
if (updateSnapshotTreeCount > 1) alwaysLog('MULTIPLE SNAPSHOT TREE UPDATES:', updateSnapshotTreeCount);
246+
if (fiberRoot) {
247+
console.log('updateSnapshotTree(), updating snapshot', snap.tree);
248+
const { current } = fiberRoot;
249+
snap.tree = createTree(current);
250+
console.log('updateSnapshotTree(), completed snapshot', snap.tree);
251+
}
252+
updateSnapshotTreeCount--;
209253
}
210254

211-
return async container => {
212-
// Point fiberRoot to FiberRootNode
255+
return async () => {
256+
/* const container = document.getElementById('root');
213257
if (container._internalRoot) {
214258
fiberRoot = container._internalRoot;
215259
} else {
@@ -220,28 +264,35 @@ module.exports = (snap, mode) => {
220264
// Only assign internal root if it actually exists
221265
fiberRoot = _internalRoot || _reactRootContainer;
222266
}
267+
*/
223268
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
224-
console.log('this is devTools', devTools);
225269
const reactInstance = devTools ? devTools.renderers.get(1) : null;
226-
270+
fiberRoot = devTools.getFiberRoots(1).values().next().value;
271+
272+
alwaysLog('fiberRoot:', fiberRoot);
227273
if (reactInstance && reactInstance.version) {
228274
devTools.onCommitFiberRoot = (function (original) {
229275
return function (...args) {
230276
fiberRoot = args[1];
231-
console.log('this is fiberRoot', fiberRoot);
277+
//console.log('Fiber committed, updating snapshot tree with:', fiberRoot);
232278
updateSnapShotTree();
279+
console.log('Fiber committed, sending latest snapshot');
233280
sendSnapshot();
281+
console.log('Fiber committed, latest snapshot sent');
234282
return original(...args);
235283
};
236284
}(devTools.onCommitFiberRoot));
237285
}
238286
updateSnapShotTree();
287+
sendSnapshot();
288+
// updateSnapShotTree();
239289
// Send the initial snapshot once the content script has started up
240290
// This message is sent from contentScript.js in chrome extension bundles
241-
window.addEventListener('message', ({ data: { action } }) => {
242-
if (action === 'contentScriptStarted') {
243-
sendSnapshot();
244-
}
245-
});
291+
// window.addEventListener('message', ({ data: { action } }) => {
292+
// if (action === 'contentScriptStarted') {
293+
// // console.log('content script started received at linkFiber.js')
294+
// sendSnapshot();
295+
// }
296+
// });
246297
};
247298
};

dev-reactime/masterState.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
const componentActionsRecord = {};
66
let index = 0;
77

8-
module.exports = {
8+
// module.exports = {
9+
export default {
910
saveNew: (state, component) => {
1011
componentActionsRecord[index] = { state, component };
1112
index++;

0 commit comments

Comments
 (0)