Skip to content

Commit 54b4850

Browse files
authored
Merge pull request #85 from crperezt/feature4
Cleaned up data format; fixed errors in throttling
2 parents b6155f2 + 691f11e commit 54b4850

File tree

6 files changed

+81
-24
lines changed

6 files changed

+81
-24
lines changed

src/app/containers/MainContainer.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ function MainContainer(): any {
8484
// if viewIndex is -1, then use the sliderIndex instead
8585
const snapshotView = viewIndex === -1 ? snapshots[sliderIndex] : snapshots[viewIndex];
8686
// gabi :: cleannign hierarchy and snapshotView from stateless data
87-
const statelessCleanning = (obj:{name?:string; componentData?:object; state?:object|string;stateSnaphot?:object; children?:any[]}) => {
87+
const statelessCleanning = (
88+
obj:{
89+
name?:string;
90+
componentData?:object;
91+
state?:{hooksState?: any | {componentData?: any}[]} | string;
92+
stateSnaphot?:object;
93+
children?:any[];
94+
}) => {
8895
const newObj = { ...obj };
8996
if (newObj.name === 'nameless') {
9097
delete newObj.name;
@@ -95,6 +102,14 @@ function MainContainer(): any {
95102
if (newObj.state === 'stateless') {
96103
delete newObj.state;
97104
}
105+
if (newObj.state && newObj.state.hooksState) {
106+
if (Array.isArray(newObj.state.hooksState)) {
107+
newObj.state.hooksState.forEach(s => {
108+
delete s.componentData;
109+
});
110+
}
111+
}
112+
98113
if (newObj.stateSnaphot) {
99114
newObj.stateSnaphot = statelessCleanning(obj.stateSnaphot);
100115
}

src/app/reducers/mainReducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default (state, action) => produce(state, draft => {
1818
// eslint-disable-next-line consistent-return
1919
const findName = (index, obj) => {
2020
// eslint-disable-next-line eqeqeq
21-
if (obj.index == index) {
21+
if (obj && obj.index == index) {
2222
return obj.name;
2323
}
2424

src/backend/helpers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export const throttle = (f, t) => {
1717
if (isOnCooldown && isCallQueued) return;
1818
if (isOnCooldown) {
1919
isCallQueued = true;
20+
console.log('snapshot update already queued');
2021
return;
2122
}
23+
console.log('no queue, updating snapshot from trigger func');
2224
f();
2325
isOnCooldown = true;
2426
isCallQueued = false;
@@ -27,11 +29,14 @@ export const throttle = (f, t) => {
2729
if (isCallQueued) {
2830
isCallQueued = false;
2931
isOnCooldown = true; // not needed I think
32+
console.log('calling queued call');
33+
f();
3034
setTimeout(runAfterTimeout, t);
3135
return;
3236
}
3337
isOnCooldown = false;
3438
};
39+
console.log('queueing snapshot update');
3540
setTimeout(runAfterTimeout, t);
3641
};
3742
return throttledFunc;

src/backend/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function getRouteURL(node: SnapshotNode): string {
4040
window.addEventListener('message', ({ data: { action, payload } } : MsgData) => {
4141
switch (action) {
4242
case 'jumpToSnap':
43-
timeJump(payload); // * This sets state with given payload
43+
timeJump(payload, true); // * This sets state with given payload
4444
// Get the pathname from payload and add new entry to browser history
4545
// MORE: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
4646

src/backend/linkFiber.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ export default (snap: Snapshot, mode: Mode): ()=>void => {
7878
// Carlos: these two are legacy comments, we should look into them later
7979
// prevents useEffect from crashing on load
8080
// if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
81-
// console.log('traverse hooks memoizedState', memoizedState);
82-
if (memoizedState.memoizedState) {
81+
if (memoizedState.memoizedState && memoizedState.queue.lastRenderedReducer && memoizedState.queue.lastRenderedReducer.name === 'basicStateReducer') {
8382
hooksStates.push({
8483
component: memoizedState.queue,
8584
state: memoizedState.memoizedState,
@@ -112,6 +111,10 @@ export default (snap: Snapshot, mode: Mode): ()=>void => {
112111
treeBaseDuration,
113112
} = currentFiber;
114113

114+
<<<<<<< HEAD
115+
let newState: any = {};
116+
let componentData: ComponentData = {};
117+
=======
115118
let newState: any;
116119
let componentData: ComponentData = {};
117120
/* = {
@@ -122,6 +125,7 @@ export default (snap: Snapshot, mode: Mode): ()=>void => {
122125
treeBaseDuration: 0,
123126
};
124127
*/
128+
>>>>>>> master
125129
let componentFound = false;
126130

127131
// Check if node is a stateful setState component
@@ -146,11 +150,15 @@ export default (snap: Snapshot, mode: Mode): ()=>void => {
146150
hooksStates.forEach((state, i) => {
147151
hooksIndex = componentActionsRecord.saveNew(state.state, state.component);
148152
if (newState && newState.hooksState) {
149-
newState.hooksState.push([{ [hooksNames[i]]: state.state }, hooksIndex]);
153+
newState.hooksState.push({ [hooksNames[i]]: state.state, componentData: { index: hooksIndex } });
154+
// newState.hooksState.push([{ [hooksNames[i]]: state.state }, hooksIndex]);
150155
} else if (newState) {
151-
newState.hooksState = [{ [hooksNames[i]]: state.state }, hooksIndex];
156+
newState.hooksState = [{ [hooksNames[i]]: state.state, componentData: { index: hooksIndex } }];
157+
// newState.hooksState = [{ [hooksNames[i]]: state.state }, hooksIndex];
152158
} else {
153-
newState = { hooksState: [{ [hooksNames[i]]: state.state }, hooksIndex] };
159+
// newState = { hooksState: [{ [hooksNames[i]]: state.state }, hooksIndex] };
160+
newState = { hooksState: [] };
161+
newState.hooksState.push({ [hooksNames[i]]: state.state, componentData: { index: hooksIndex } });
154162
}
155163
componentFound = true;
156164
});
@@ -174,7 +182,10 @@ export default (snap: Snapshot, mode: Mode): ()=>void => {
174182

175183
let newNode = null;
176184
// We want to add this fiber node to the snapshot
185+
<<<<<<< HEAD
186+
=======
177187
// const snapshotState = newState.state || newState.hooksState;
188+
>>>>>>> master
178189
if (componentFound || newState === 'stateless') {
179190
if (fromSibling) {
180191
newNode = tree.addSibling(newState,
@@ -185,6 +196,7 @@ export default (snap: Snapshot, mode: Mode): ()=>void => {
185196
elementType ? elementType.name : 'nameless',
186197
componentData);
187198
}
199+
if (newState !== 'stateless') console.log('state updated:', newState);
188200
} else {
189201
newNode = tree;
190202
}
@@ -217,7 +229,10 @@ export default (snap: Snapshot, mode: Mode): ()=>void => {
217229

218230
function onVisibilityChange(): void {
219231
doWork = !document.hidden;
232+
<<<<<<< HEAD
233+
=======
220234
// console.log('doWork is:', doWork);
235+
>>>>>>> master
221236
}
222237

223238
return () => {
@@ -244,7 +259,12 @@ export default (snap: Snapshot, mode: Mode): ()=>void => {
244259
return function (...args) {
245260
// eslint-disable-next-line prefer-destructuring
246261
fiberRoot = args[1];
247-
if (doWork) throttledUpdateSnapshot();
262+
console.log('in CFR committed fiber');
263+
if (doWork) {
264+
console.log('in CFR: updating snapshot');
265+
throttledUpdateSnapshot();
266+
}
267+
console.log('in CFR updated snapshot');
248268
return original(...args);
249269
};
250270
}(devTools.onCommitFiberRoot));

src/backend/timeJump.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ import 'core-js';
1515
// const componentActionsRecord = require('./masterState');
1616
import componentActionsRecord from './masterState';
1717

18+
const circularComponentTable = new Set();
19+
1820
// Carlos: origin is latest snapshot, linking to the fiber,
1921
// so changes to origin change app
2022
// module.exports = (origin, mode) => {
2123
export default (origin, mode) => {
2224
// Recursively change state of tree
2325
// Carlos: target is past state we are travelling to
2426

25-
function jump(target) {
27+
function jump(target, firstCall = false) {
2628
// Set the state of the origin tree if the component is stateful
29+
2730
if (!target) return;
31+
2832
if (target.state === 'stateless') target.children.forEach(child => jump(child));
2933
const component = componentActionsRecord.getComponentByIndex(target.componentData.index);
3034
if (component && component.setState) {
@@ -40,26 +44,39 @@ export default (origin, mode) => {
4044
}
4145

4246
// Check for hooks state and set it with dispatch()
43-
if (target.state.hooksState) {
44-
const hooksComponent = componentActionsRecord.getComponentByIndex(target.state.hooksState[1]);
45-
// const [hooksState] = [target.state.hooksState];
46-
const hooksState = Object.values(target.state.hooksState[0])[0];
47-
if (hooksComponent && hooksComponent.dispatch) {
48-
//hooksComponent.dispatch(Object.values(target.state.hooksState[0])[0]);
49-
console.log('setting hooksState of component id:', target.state.hooksState[1], 'to:', hooksState)
50-
hooksComponent.dispatch(hooksState);
51-
}
52-
target.children.forEach(child => jump(child));
53-
}
47+
if (target.state && target.state.hooksState) {
48+
console.log('SNAPSHOT TARGET: ', target);
49+
// const hooksComponent = componentActionsRecord.getComponentByIndex(target.state.hooksState[1]);
50+
target.state.hooksState.forEach(hook => {
51+
console.log('HOOK IS:', JSON.stringify(hook));
52+
if (!hook.componentData) return;
53+
const hooksComponent = componentActionsRecord.getComponentByIndex(hook.componentData.index);
54+
// const [hooksState] = [target.state.hooksState];
55+
delete hook.componentData;
56+
const hooksState = Object.values(hook);
57+
if (hooksComponent && hooksComponent.dispatch) {
58+
// hooksComponent.dispatch(Object.values(target.state.hooksState[0])[0]);
59+
hooksComponent.dispatch(hooksState);
60+
}
61+
});
62+
//target.children.forEach(child => jump(child));
5463

55-
if ((!component || !component.state) && !target.state.hooksState) {
56-
target.children.forEach(child => jump(child));
5764
}
65+
66+
target.children.forEach(child => {
67+
if (!circularComponentTable.has(child)) {
68+
circularComponentTable.add(child);
69+
jump(child);
70+
}
71+
});
72+
73+
// }
5874
}
5975

60-
return target => {
76+
return (target, firstCall = false) => {
6177
// * Setting mode disables setState from posting messages to window
6278
mode.jumping = true;
79+
if (firstCall) circularComponentTable.clear();
6380
jump(target);
6481
setTimeout(() => {
6582
mode.jumping = false;

0 commit comments

Comments
 (0)