Skip to content

Commit 62f17a9

Browse files
RobbyTiptonfscgoldenjoeeparkdavidkim7773khobread
committed
Improved logic in masterState saveNew function to account for case where target application has multiple stateful functional components. Made accompanying edits to linkFiber where saveNew is invoked and backendTypes where the HookStateItem type is defined. Next commit should aim to resolve case where target application has multiple React components with the same name (i.e. tic-tac-toe with 9 box components).
Co-authored-by: Chris LeBrett <[email protected]> Co-authored-by: Robby Tipton <[email protected]> Co-authored-by: Joseph Park <[email protected]> Co-authored-by: David Kim <[email protected]> Co-authored-by: Kevin HoEun Lee <[email protected]>
1 parent fe52f47 commit 62f17a9

File tree

3 files changed

+35
-37
lines changed

3 files changed

+35
-37
lines changed

src/backend/linkFiber.ts

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ function createTree(
357357
// time-travel state changing. Add record index to snapshot so we can retrieve.
358358
componentData.index = componentActionsRecord.saveNew(
359359
stateNode.state,
360-
stateNode
360+
stateNode,
361+
currentFiber.elementType.name,
361362
);
362363
newState = stateNode.state;
363364
componentFound = true;
@@ -367,32 +368,32 @@ function createTree(
367368
atomArray.push(memoizedProps);
368369

369370
// RECOIL HOOKS
370-
if (
371-
memoizedState
372-
&& (tag === 0 || tag === 1 || tag === 2 || tag === 10)
373-
) {
374-
if (memoizedState.queue) {
375-
// Hooks states are stored as a linked list using memoizedState.next,
376-
// so we must traverse through the list and get the states.
377-
// We then store them along with the corresponding memoizedState.queue,
378-
// which includes the dispatch() function we use to change their state.
379-
const hooksStates = traverseRecoilHooks(memoizedState, memoizedProps);
380-
hooksStates.forEach((state, i) => {
381-
hooksIndex = componentActionsRecord.saveNew(
382-
state.state,
383-
state.component
384-
);
385-
componentData.hooksIndex = hooksIndex;
386-
if (!newState) {
387-
newState = { hooksState: [] };
388-
} else if (!newState.hooksState) {
389-
newState.hooksState = [];
390-
}
391-
newState.hooksState.push({ [i]: state.state });
392-
componentFound = true;
393-
});
394-
}
395-
}
371+
// if (
372+
// memoizedState
373+
// && (tag === 0 || tag === 1 || tag === 2 || tag === 10)
374+
// ) {
375+
// if (memoizedState.queue) {
376+
// // Hooks states are stored as a linked list using memoizedState.next,
377+
// // so we must traverse through the list and get the states.
378+
// // We then store them along with the corresponding memoizedState.queue,
379+
// // which includes the dispatch() function we use to change their state.
380+
// const hooksStates = traverseRecoilHooks(memoizedState, memoizedProps);
381+
// hooksStates.forEach((state, i) => {
382+
// hooksIndex = componentActionsRecord.saveNew(
383+
// state.state,
384+
// state.component
385+
// );
386+
// componentData.hooksIndex = hooksIndex;
387+
// if (!newState) {
388+
// newState = { hooksState: [] };
389+
// } else if (!newState.hooksState) {
390+
// newState.hooksState = [];
391+
// }
392+
// newState.hooksState.push({ [i]: state.state });
393+
// componentFound = true;
394+
// });
395+
// }
396+
// }
396397

397398
// Check if node is a hooks useState function
398399
// REGULAR REACT HOOKS
@@ -412,7 +413,8 @@ function createTree(
412413
hooksStates.forEach((state, i) => {
413414
hooksIndex = componentActionsRecord.saveNew(
414415
state.state,
415-
state.component
416+
state.component,
417+
currentFiber.elementType.name,
416418
);
417419
componentData.hooksIndex = hooksIndex;
418420
if (!newState) {

src/backend/masterState.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,13 @@ let index = 0;
2020

2121
export default {
2222
// adds new component to ComponentActionsRecord
23-
saveNew: (state, component): number => {
24-
componentActionsRecord[index] = { state, component };
25-
console.log('entire record of components is', [...componentActionsRecord]);
23+
saveNew: (state, component, name): number => {
24+
componentActionsRecord[index] = { state, component, name };
2625
index++;
2726

2827
for (let i = 0; i < componentActionsRecord.length - 2; i++) {
29-
if (
30-
componentActionsRecord[i].component.constructor.name ===
31-
component.constructor.name
32-
) {
33-
console.log('reassigning componentActionsRecord at index', i);
34-
componentActionsRecord[i] = { state, component };
28+
if (componentActionsRecord[i].name === name) {
29+
componentActionsRecord[i] = { state, component, name };
3530
}
3631
}
3732

src/backend/types/backendTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export interface ComponentData {
4141
export interface HookStateItem {
4242
state: any;
4343
component: any;
44+
name?: string;
4445
}
4546

4647
export type HookStates = Array<HookStateItem>;

0 commit comments

Comments
 (0)