Skip to content

Commit cb087db

Browse files
committed
Added a check to initial start to prevent atoms to selectors relationship from populating with same core Fibers but different keys from populating the array
1 parent de5a3f6 commit cb087db

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/backend/linkFiber.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ let fiberRoot = null;
3333
let doWork = true;
3434
const circularComponentTable = new Set();
3535
let isRecoil = false;
36+
let allAtomsRelationship = [];
37+
let initialstart = false;
3638

3739
// Simple check for whether our target app uses Recoil
3840
if (window[`$recoilDebugStates`]) {
@@ -154,7 +156,6 @@ function traverseHooks(memoizedState: any): HookStates {
154156
return hooksStates;
155157
}
156158

157-
let allAtomsRelationship = [];
158159
/**
159160
* @method createTree
160161
* @param currentFiber A Fiber object
@@ -167,8 +168,8 @@ let allAtomsRelationship = [];
167168
* 3. Build a new state snapshot
168169
*/
169170
// This runs after every Fiber commit. It creates a new snapshot
170-
171-
let recoilObj = {};
171+
let atomsSelectors = {};
172+
let atomsComponents = {};
172173

173174
function createTree(
174175
currentFiber: Fiber,
@@ -196,49 +197,50 @@ function createTree(
196197
treeBaseDuration,
197198
} = currentFiber;
198199

199-
//Checks Recoil Atom and Selector Relationships
200+
//Checks Recoil Atom and Selector Relationships
200201
if (
201202
currentFiber.memoizedState &&
202203
currentFiber.memoizedState.next &&
203204
currentFiber.memoizedState.next.memoizedState &&
204205
currentFiber.memoizedState.next.memoizedState.deps &&
205206
isRecoil &&
206-
currentFiber.tag !== 15
207+
currentFiber.tag === 0 &&
208+
currentFiber.key === null //prevents capturing the same Fiber nodes but different key values that result from being changed
207209
) {
208-
209210
let pointer = currentFiber.memoizedState.next;
210-
let componentName = currentFiber.elementType.name
211-
212-
if(!recoilObj[componentName]){
213-
recoilObj[componentName] = [];
214-
while (pointer !== null) {
215-
if (!Array.isArray(pointer.memoizedState)) {
216-
let atomName = pointer.memoizedState.deps[0]['key'];
217-
recoilObj[componentName].push(
218-
atomName,
219-
);
211+
let componentName = currentFiber.elementType.name;
212+
213+
if (!atomsComponents[componentName]) {
214+
atomsComponents[componentName] = [];
215+
while (pointer !== null) {
216+
if (!Array.isArray(pointer.memoizedState)) {
217+
let atomName = pointer.memoizedState.deps[0]['key'];
218+
atomsComponents[componentName].push(atomName);
220219
}
221-
pointer = pointer.next;
220+
pointer = pointer.next;
222221
}
223222
}
224223

225-
if (currentFiber.memoizedState.next.memoizedState.deps[1].current) {
224+
if (
225+
currentFiber.memoizedState.next.memoizedState.deps[1].current &&
226+
!initialstart
227+
) {
226228
let getState = currentFiber.memoizedState.next.memoizedState.deps[1].current.getState()
227229
.graphsByVersion;
228-
229230
getState.entries().forEach((value) => {
230231
value[1].nodeDeps.entries().forEach((obj) => {
231-
if (!recoilObj[obj[0]]) {
232-
recoilObj[obj[0]] = [];
232+
if (!atomsSelectors[obj[0]]) {
233+
atomsSelectors[obj[0]] = [];
233234
}
234235
obj[1].values().forEach((selector) => {
235-
if (!recoilObj[obj[0]].includes(selector)) {
236-
recoilObj[obj[0]].push(selector);
236+
if (!atomsSelectors[obj[0]].includes(selector)) {
237+
atomsSelectors[obj[0]].push(selector);
237238
}
238239
});
239240
});
240241
});
241-
}
242+
initialstart = true;
243+
}
242244
}
243245

244246
let newState: any | { hooksState?: any[] } = {};

0 commit comments

Comments
 (0)