Skip to content

Commit 7652bdf

Browse files
authored
Merge pull request #41 from oslabs-beta/staging
Staging
2 parents 11be34f + 03de0ee commit 7652bdf

File tree

9 files changed

+159
-141
lines changed

9 files changed

+159
-141
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: 76 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,41 @@
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');\
4949

50-
module.exports = (snap, mode) => {
50+
import Tree from './tree';
51+
import componentActionsRecord from './masterState';
52+
53+
const circularComponentTable = new Map();
54+
55+
// module.exports = (snap, mode) => {
56+
export default (snap, mode) => {
5157
let fiberRoot = null;
5258

53-
async function sendSnapshot() {
59+
function sendSnapshot() {
5460
// Don't send messages while jumping or while paused
61+
circularComponentTable.clear();
62+
// console.log('sending snapshot');
5563
if (mode.jumping || mode.paused) return;
56-
console.log('PAYLOAD: before cleaning', snap.tree);
64+
// console.log('PAYLOAD: before cleaning', snap.tree);
65+
66+
if (!snap.tree) {
67+
// console.log('snapshot empty, sending root');
68+
snap.tree = new Tree('root', 'root');
69+
}
5770
const payload = snap.tree.cleanTreeCopy();// snap.tree.getCopy();
58-
console.log('PAYLOAD: after cleaning', payload);
59-
try {
60-
await window.postMessage({
71+
72+
// console.log('PAYLOAD: after cleaning', payload);
73+
// try {
74+
// await window.postMessage({
75+
window.postMessage({
6176
action: 'recordSnap',
6277
payload,
6378
});
64-
} catch (e) {
65-
console.log('failed to send postMessage:', e);
66-
}
79+
// } catch (e) {
80+
// console.log('failed to send postMessage:', e);
81+
// }
6782
}
6883

6984
// Carlos: Injects instrumentation to update our state tree every time
@@ -75,13 +90,11 @@ module.exports = (snap, mode) => {
7590
// prevents useEffect from crashing on load
7691
// if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
7792
if (memoizedState.memoizedState) {
78-
console.log('memoizedState in traverseHooks is:', memoizedState);
7993
hooksStates.push({
8094
component: memoizedState.queue,
8195
state: memoizedState.memoizedState,
8296
});
8397
}
84-
// console.log('GOT STATE', memoizedState.memoizedState);
8598
memoizedState = memoizedState.next !== memoizedState
8699
? memoizedState.next : null;
87100
}
@@ -90,8 +103,9 @@ module.exports = (snap, mode) => {
90103

91104
// Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
92105
//
93-
function createTree(currentFiber, tree = new Tree('root')) {
106+
function createTree(currentFiber, tree = new Tree('root', 'root'), fromSibling = false) {
94107
// Base case: child or sibling pointed to null
108+
// console.log('linkFiber.js: creating tree');
95109
if (!currentFiber) return null;
96110
if (!tree) return tree;
97111

@@ -115,11 +129,10 @@ module.exports = (snap, mode) => {
115129
let componentFound = false;
116130

117131
// 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);
132+
if (stateNode && stateNode.state && (tag === 0 || tag === 1 || tag === 2)) {
121133
// Save component's state and setState() function to our record for future
122134
// time-travel state changing. Add record index to snapshot so we can retrieve.
135+
// console.log('linkFiber.js: found stateNode component');
123136
componentData.index = componentActionsRecord.saveNew(stateNode.state, stateNode);
124137
newState = stateNode.state;
125138
componentFound = true;
@@ -128,9 +141,8 @@ module.exports = (snap, mode) => {
128141
// Check if node is a hooks useState function
129142
let hooksIndex;
130143
if (memoizedState && (tag === 0 || tag === 1 || tag === 10)) {
131-
console.log('in create tree if');
132-
console.log('this is currentFiber from createTree', currentFiber);
133144
if (memoizedState.queue) {
145+
// console.log('linkFiber.js: found hooks component');
134146
// Hooks states are stored as a linked list using memoizedState.next,
135147
// so we must traverse through the list and get the states.
136148
// We then store them along with the corresponding memoizedState.queue,
@@ -151,7 +163,7 @@ module.exports = (snap, mode) => {
151163
}
152164

153165
// This grabs stateless components
154-
if (!componentFound && (tag === 0 || tag === 1)) {
166+
if (!componentFound && (tag === 0 || tag === 1 || tag === 2)) {
155167
newState = 'stateless';
156168
}
157169

@@ -164,52 +176,55 @@ module.exports = (snap, mode) => {
164176
treeBaseDuration,
165177
};
166178

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);
179+
let newNode = null;
180+
if (componentFound || newState === 'stateless') {
181+
if (fromSibling) {
182+
newNode = tree.addSibling(newState,
183+
elementType.name ? elementType.name : elementType,
184+
componentData);
185+
} else {
186+
newNode = tree.addChild(newState,
187+
elementType.name ? elementType.name : elementType,
188+
componentData);
189+
}
190+
} else {
191+
newNode = tree;
171192
}
172193

173194
// Recurse on children
174-
if (child) {
195+
196+
if (child) { // && !circularComponentTable.has(child)) {
175197
// If this node had state we appended to the children array,
176198
// so attach children to the newly appended child.
177199
// 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-
}
200+
// console.log('going into child');
201+
// circularComponentTable.set(child, true);
202+
createTree(child, newNode);
183203
}
184204
// Recurse on siblings
185-
if (sibling) createTree(sibling, tree);
205+
if (sibling) { // && !circularComponentTable.has(sibling)) {
206+
// console.log('going into sibling');
207+
// circularComponentTable.set(sibling, true);
208+
createTree(sibling, newNode, true);
209+
}
186210

211+
// console.log('linkFiber.js: processed children and sibling, returning tree');
187212
return tree;
188213
}
189214

190-
// ! BUG: skips 1st hook click
191215
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
205-
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?
216+
// console.log('linkFiber.js, updateSnapshotTree(), checking if fiberRoot updated');
217+
if (fiberRoot) {
218+
// console.log('linkFiber.js, updateSnapshotTree(), updating snapshot', snap.tree);
219+
const { current } = fiberRoot;
220+
snap.tree = createTree(current);
221+
// console.log('linkFiber.js, updateSnapshotTree(), completed snapshot', snap.tree);
222+
}
209223
}
210224

211-
return async container => {
212-
// Point fiberRoot to FiberRootNode
225+
return async () => {
226+
227+
const container = document.getElementById('root');
213228
if (container._internalRoot) {
214229
fiberRoot = container._internalRoot;
215230
} else {
@@ -220,28 +235,31 @@ module.exports = (snap, mode) => {
220235
// Only assign internal root if it actually exists
221236
fiberRoot = _internalRoot || _reactRootContainer;
222237
}
238+
223239
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
224-
console.log('this is devTools', devTools);
225240
const reactInstance = devTools ? devTools.renderers.get(1) : null;
226241

242+
//console.log('fiberRoot retrieved:', fiberRoot);
227243
if (reactInstance && reactInstance.version) {
228244
devTools.onCommitFiberRoot = (function (original) {
229245
return function (...args) {
230246
fiberRoot = args[1];
231-
console.log('this is fiberRoot', fiberRoot);
232247
updateSnapShotTree();
233248
sendSnapshot();
234249
return original(...args);
235250
};
236251
}(devTools.onCommitFiberRoot));
237252
}
238253
updateSnapShotTree();
254+
sendSnapshot();
255+
// updateSnapShotTree();
239256
// Send the initial snapshot once the content script has started up
240257
// 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-
});
258+
// window.addEventListener('message', ({ data: { action } }) => {
259+
// if (action === 'contentScriptStarted') {
260+
// // console.log('content script started received at linkFiber.js')
261+
// sendSnapshot();
262+
// }
263+
// });
246264
};
247265
};

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++;

dev-reactime/timeJump.js

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
/* eslint-disable no-param-reassign */
1313

14-
const componentActionsRecord = require('./masterState');
14+
// const componentActionsRecord = require('./masterState');
15+
import componentActionsRecord from './masterState';
1516

1617
// Carlos: origin is latest snapshot, linking to the fiber,
1718
// so changes to origin change app
18-
module.exports = (origin, mode) => {
19+
// module.exports = (origin, mode) => {
20+
export default (origin, mode) => {
1921
// Recursively change state of tree
2022
// Carlos: target is past state we are travelling to
2123

@@ -40,6 +42,7 @@ module.exports = (origin, mode) => {
4042
target.state.hooksState.forEach(hooksState => {
4143
if (component && component.dispatch) {
4244
const hooksComponent = componentActionsRecord.getComponentByIndex(hooksState[1]);
45+
// console.log('dispatch going back to hooks state: ', target.state.hooksState[0]);
4346
hooksComponent.dispatch(target.state.hooksState[0]);
4447
}
4548
});
@@ -49,47 +52,6 @@ module.exports = (origin, mode) => {
4952
if ((!component || !component.state) && !target.state.hooksState) {
5053
target.children.forEach(child => jump(child));
5154
}
52-
53-
54-
55-
/*
56-
if (originNode.component.setState) {
57-
console.log('stateful component jump, originNode: ', originNode.component);
58-
console.log('new target is:', target);
59-
// * Use the function argument when setting state to account for any state properties
60-
// * that may not have existed in the past
61-
originNode.component.setState(prevState => {
62-
Object.keys(prevState).forEach(key => {
63-
if (target.state[key] === undefined) {
64-
target.state[key] = undefined;
65-
}
66-
});
67-
return target.state;
68-
}, () => {
69-
// Iterate through new children once state has been set
70-
// gabi :: Will the target and origin have the same amount of dependents?
71-
// carlos: i don't know wtf
72-
jump(target.children[0], originNode.children[0]);
73-
});
74-
} else {
75-
// If component uses hooks, traverse through the memoize tree
76-
let current = originNode.component;
77-
//console.log("changing state of hooks component:", current);
78-
//console.log("new state is:", target);
79-
let index = 0;
80-
const hooks = returnState();
81-
// While loop through the memoize tree
82-
console.log('hooks:', hooks);
83-
while (current && current.queue) { // allows time travel with useEffect
84-
console.log('about to call hooks dispatch, target state is: ', target);
85-
//console.log('calling dispatch with arg:', target.state[hooks[index]]);
86-
current.queue.dispatch(target.state[hooks[index]]);
87-
// Reassign the current value
88-
current = current.next;
89-
index += 2;
90-
}
91-
}
92-
*/
9355
}
9456

9557
return target => {

0 commit comments

Comments
 (0)