Skip to content

Commit 3caab37

Browse files
committed
Step change has been broken
1 parent 5fb3916 commit 3caab37

File tree

1 file changed

+77
-19
lines changed

1 file changed

+77
-19
lines changed

src/backend/linkFiber.ts

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function updateSnapShotTree(snap: Snapshot, mode: Status): void {
132132
}
133133

134134
/**
135-
* @method traverseHooks
135+
* @function traverseHooks
136136
* @param memoizedState memoizedState property on a stateful functional component's FiberNode object
137137
* @return An array of array of HookStateItem objects
138138
*
@@ -155,7 +155,7 @@ function traverseHooks(memoizedState: any): HookStates {
155155
}
156156

157157
/**
158-
* @method createTree
158+
* @function createTree
159159
* @param currentFiber A Fiber object
160160
* @param tree A Tree object, default initialized to an instance given 'root' and 'root'
161161
* @param fromSibling A boolean, default initialized to false
@@ -190,6 +190,7 @@ const exclude = new Set([
190190
'flags',
191191
'get key',
192192
'getState',
193+
'hash',
193194
'key',
194195
'lanes',
195196
'lastBaseUpdate',
@@ -213,8 +214,11 @@ const exclude = new Set([
213214
'return',
214215
'route',
215216
'routeContext',
217+
'search',
216218
'shared',
217219
'sibling',
220+
'state',
221+
'store',
218222
'subscribe',
219223
'subscription',
220224
'stateNode',
@@ -261,19 +265,38 @@ function convertDataToString(reactDevData, reactimeData = {}, excludeSet?: any)
261265
}
262266
return reactimeData;
263267
}
264-
// ------------------------TRIMMING CONTEXT DATA--------------------------------
265-
function trimContextData(memoizedState, reactimeContextData = {}) {
268+
// ------------------------TRIMMING STATE DATA--------------------------------
269+
function trimContextData(memoizedState, _debugHookTypes) {
270+
// Initialize object to store state data of the component
271+
const reactimeStateData = {};
272+
// Initialize the set of excluded properties:
266273
const exclude = new Set(['children', 'store', 'subscription']);
267-
while (memoizedState) {
268-
//Trim the current level of memoizedState data:
269-
const updateMemoizedState = convertDataToString(memoizedState?.memoizedState[0], {}, exclude);
270-
//Update Reactime data:
271-
Object.assign(reactimeContextData, updateMemoizedState);
274+
// Initialize the set of React Hooks:
275+
const reactHooks = new Set(['useState', 'useMemo']);
276+
// Initialize counter for the default naming. If user use reactHook, such as useState, react will only pass in the value, and not the name of the state.
277+
let stateCounter = 1;
278+
for (const hook of _debugHookTypes) {
279+
if (reactHooks.has(hook)) {
280+
let stateData = memoizedState?.memoizedState;
281+
// ReactHook condition:
282+
if (typeof stateData !== 'object') {
283+
const defaultName = `state${stateCounter}`;
284+
stateData = { [defaultName]: stateData };
285+
stateCounter++;
286+
}
287+
// If user does not use reactHook => state is store in memoizedState array, at i = 0
288+
else {
289+
stateData = stateData[0];
290+
}
291+
//Trim the current level of memoizedState data:
292+
console.log({ stateData });
293+
convertDataToString(stateData, reactimeStateData);
294+
}
272295
//Move on to the next level:
273296
memoizedState = memoizedState?.next;
274297
}
275298

276-
return reactimeContextData;
299+
return reactimeStateData;
277300
}
278301
// -------------------------CREATE TREE TO SEND TO FRONT END--------------------
279302
/**
@@ -319,7 +342,8 @@ function createTree(
319342
elementType,
320343
memoizedProps,
321344
memoizedState,
322-
dependencies,
345+
stateNode,
346+
// dependencies,
323347
_debugHookTypes,
324348
});
325349

@@ -367,20 +391,43 @@ function createTree(
367391
*/
368392
// const filteredComponents = tag != ContextProvider;
369393
const filteredComponents = true;
394+
395+
// ----------------APPEND PROP DATA FROM REACT DEV TOOL-----------------------
370396
// check to see if the parent component has any state/props
371397
if (filteredComponents && memoizedProps && Object.keys(memoizedProps).length) {
372398
componentData.props = convertDataToString(memoizedProps);
373399
}
400+
401+
// ----------------APPEND STATE DATA FROM REACT DEV TOOL----------------------
402+
// If user uses Redux, context data will be stored in memoizedState of the component => grab context object stored in the memoizedState
403+
if (
404+
(tag === FunctionComponent || tag === ClassComponent) &&
405+
Array.isArray(memoizedState?.memoizedState)
406+
) {
407+
componentData.context = trimContextData(memoizedState, _debugHookTypes);
408+
}
409+
// if user uses useContext hook, context data will be stored in memoizedProps.value of the Context.Provider component => grab context object stored in memoizedprops
410+
// Different from other provider, such as Routes, BrowswerRouter, ReactRedux, ..., Context.Provider does not have a displayName
411+
if (tag === ContextProvider && !elementType._context.displayName) {
412+
let stateData = memoizedProps.value;
413+
if (stateData === null || typeof stateData !== 'object') {
414+
stateData = { CONTEXT: stateData };
415+
}
416+
componentData.context = stateData;
417+
}
418+
419+
// Check to see if the component has any context:
374420
// if the component uses the useContext hook, we want to grab the context object and add it to the componentData object for that fiber
375-
// if (tag === 0 && _debugHookTypes && dependencies?.firstContext?.memoizedValue) {
421+
// if (tag === FunctionComponent && _debugHookTypes && dependencies?.firstContext?.memoizedValue) {
376422
// componentData.context = convertDataToString(dependencies.firstContext.memoizedValue);
377423
// }
378-
if ((tag === FunctionComponent || tag === ClassComponent) && memoizedState?.memoizedState) {
379-
componentData.context = trimContextData(memoizedState);
380-
}
381-
424+
382425
// Check if node is a stateful class component
383-
if (stateNode && stateNode.state && (tag === 0 || tag === 1 || tag === 2)) {
426+
if (
427+
stateNode &&
428+
stateNode.state &&
429+
(tag === FunctionComponent || tag === ClassComponent || tag === IndeterminateComponent)
430+
) {
384431
// Save component's state and setState() function to our record for future
385432
// time-travel state changing. Add record index to snapshot so we can retrieve.
386433
componentData.index = componentActionsRecord.saveNew(stateNode.state, stateNode);
@@ -391,8 +438,16 @@ function createTree(
391438

392439
// Check if node is a hooks useState function
393440
// REGULAR REACT HOOKS
394-
if (memoizedState && (tag === 0 || tag === 1 || tag === 2 || tag === 10)) {
441+
if (
442+
memoizedState &&
443+
(tag === FunctionComponent ||
444+
tag === ClassComponent ||
445+
tag === IndeterminateComponent ||
446+
tag === ContextProvider)
447+
) {
448+
console.log('Out here');
395449
if (memoizedState.queue) {
450+
console.log('In Queue');
396451
// Hooks states are stored as a linked list using memoizedState.next,
397452
// so we must traverse through the list and get the states.
398453
// We then store them along with the corresponding memoizedState.queue,
@@ -415,7 +470,10 @@ function createTree(
415470
}
416471

417472
// This grabs stateless components
418-
if (!componentFound && (tag === 0 || tag === 1 || tag === 2)) {
473+
if (
474+
!componentFound &&
475+
(tag === FunctionComponent || tag === ClassComponent || tag === IndeterminateComponent)
476+
) {
419477
newState = 'stateless';
420478
}
421479

0 commit comments

Comments
 (0)