Skip to content

Commit 653582a

Browse files
committed
Working on adding Next JS compatibility
1 parent 2d50d8d commit 653582a

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

src/backend/controllers/createTree/index.ts

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,21 @@ import {
4444
filterAndFormatData,
4545
} from '../../controllers/createTree/statePropExtractors';
4646

47+
let rtidCounter = 0;
48+
const nextJSDefaultComponent = new Set(['ReactDevOverlay', 'Portal']);
4749
// -------------------------CREATE TREE TO SEND TO FRONT END--------------------
4850
/**
4951
* This is a recursive function that runs after every Fiber commit using the following logic:
5052
* 1. Traverse from FiberRootNode
5153
* 2. Create an instance of custom Tree class
5254
* 3. Build a new state snapshot
5355
* Every time a state change is made in the accompanying app, the extension creates a Tree “snapshot” of the current state, and adds it to the current “cache” of snapshots in the extension
54-
* @function createTree
5556
* @param currentFiberNode A Fiber object
5657
* @param tree A Tree object, default initialized to an instance given 'root' and 'root'
5758
* @param fromSibling A boolean, default initialized to false
5859
* @return An instance of a Tree object
5960
*/
6061
// TODO: Not sure why the ritd need to be outside of the createTree function. Want to put inside, but in case this need to be keep track for front end.
61-
let rtidCounter = 0;
62-
6362
export default function createTree(
6463
currentFiberNode: Fiber,
6564
circularComponentTable: Set<Fiber> = new Set(),
@@ -93,22 +92,22 @@ export default function createTree(
9392
treeBaseDuration,
9493
_debugHookTypes,
9594
} = currentFiberNode;
96-
// console.log('LinkFiber', {
97-
// currentFiberNode,
98-
// // tag,
99-
// // elementType,
100-
// componentName:
101-
// elementType?._context?.displayName || //For ContextProvider
102-
// elementType?._result?.name || //For lazy Component
103-
// elementType?.render?.name ||
104-
// elementType?.name ||
105-
// elementType,
106-
// // memoizedProps,
107-
// // memoizedState,
108-
// // stateNode,
109-
// // dependencies,
110-
// // _debugHookTypes,
111-
// });
95+
console.log('LinkFiber', {
96+
currentFiberNode,
97+
// tag,
98+
// elementType,
99+
componentName:
100+
elementType?._context?.displayName || //For ContextProvider
101+
elementType?._result?.name || //For lazy Component
102+
elementType?.render?.name ||
103+
elementType?.name ||
104+
elementType,
105+
// memoizedProps,
106+
// memoizedState,
107+
// stateNode,
108+
// dependencies,
109+
// _debugHookTypes,
110+
});
112111

113112
// TODO: Understand this if statement
114113
if (tag === HostComponent) {
@@ -244,26 +243,34 @@ export default function createTree(
244243
tag === ContextProvider) //TODOD: Need to figure out why we need context provider
245244
) {
246245
if (memoizedState.queue) {
247-
// Hooks states are stored as a linked list using memoizedState.next,
248-
// so we must traverse through the list and get the states.
249-
// We then store them along with the corresponding memoizedState.queue,
250-
// which includes the dispatch() function we use to change their state.
251-
const hooksStates = getHooksStateAndUpdateMethod(memoizedState);
252-
const hooksNames = getHooksNames(elementType.toString());
253-
// Intialize state & index:
254-
newState.hooksState = [];
255-
componentData.hooksState = {};
256-
componentData.hooksIndex = [];
257-
hooksStates.forEach(({ state, component }, i) => {
258-
// Save component's state and dispatch() function to our record for future time-travel state changing. Add record index to snapshot so we can retrieve.
259-
componentData.hooksIndex.push(componentActionsRecord.saveNew(component));
260-
// Save state information in componentData.
261-
newState.hooksState.push({ [hooksNames[i].varName]: state });
262-
// Passess to front end
263-
componentData.hooksState[hooksNames[i].varName] = state;
264-
});
265-
isStatefulComponent = true;
246+
try {
247+
// Hooks states are stored as a linked list using memoizedState.next,
248+
// so we must traverse through the list and get the states.
249+
// We then store them along with the corresponding memoizedState.queue,
250+
// which includes the dispatch() function we use to change their state.
251+
const hooksStates = getHooksStateAndUpdateMethod(memoizedState);
252+
const hooksNames = getHooksNames(elementType.toString());
253+
// Intialize state & index:
254+
newState.hooksState = [];
255+
componentData.hooksState = {};
256+
componentData.hooksIndex = [];
257+
hooksStates.forEach(({ state, component }, i) => {
258+
// Save component's state and dispatch() function to our record for future time-travel state changing. Add record index to snapshot so we can retrieve.
259+
componentData.hooksIndex.push(componentActionsRecord.saveNew(component));
260+
// Save state information in componentData.
261+
newState.hooksState.push({ [hooksNames[i].varName]: state });
262+
// Passess to front end
263+
componentData.hooksState[hooksNames[i].varName] = state;
264+
});
265+
} catch (err) {
266+
console.log('Failed Element', { component: elementType?.name });
267+
// if (!nextJSDefaultComponent.has(elementType?.name)) {
268+
// throw new Error(err);
269+
// } else {
270+
// console.log('We are good');
271+
}
266272
}
273+
isStatefulComponent = true;
267274
}
268275

269276
// This grabs stateless components

src/backend/controllers/createTree/statePropExtractors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ export function getHooksNames(elementType: string): { hookName: string; varName:
217217
try {
218218
AST = JSXParser.parse(elementType);
219219
} catch (e) {
220+
console.log('This component was not converted', { elementType });
220221
throw Error('Error occurs at helpers getHooksName.ts Cannot parse functional component.');
222+
return;
221223
}
222224
// Begin search for hook names, only if ast has a body property.
223225
AST = AST.body;

0 commit comments

Comments
 (0)