Skip to content

Commit

Permalink
devtools: compare effects by deps not referential equality
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Jan 27, 2021
1 parent c4a45ff commit 2d58205
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import type {
ComponentFilter,
ElementType,
} from 'react-devtools-shared/src/types';
import is from 'shared/objectIs';

type getDisplayNameForFiberType = (fiber: Fiber) => string | null;
type getTypeSymbolType = (type: any) => Symbol | number;
Expand Down Expand Up @@ -1073,6 +1074,48 @@ export function attach(
return null;
}

function areHookInputsEqual(
nextDeps: Array<mixed>,
prevDeps: Array<mixed> | null,
) {
if (prevDeps === null) {
return false;
}

for (let i = 0; i < prevDeps.length && i < nextDeps.length; i++) {
if (is(nextDeps[i], prevDeps[i])) {
continue;
}
return false;
}
return true;
}

function isEffect(memoizedState) {
return (
memoizedState !== null &&
typeof memoizedState === 'object' &&
memoizedState.hasOwnProperty('tag') &&
memoizedState.hasOwnProperty('create') &&
memoizedState.hasOwnProperty('destroy') &&
memoizedState.hasOwnProperty('deps') &&
memoizedState.hasOwnProperty('next')
);
}

function didHookChange(prev: Hook, next: Hook): boolean {
const prevMemoizedState = prev.memoizedState;
const nextMemoizedState = next.memoizedState;

if (isEffect(prevMemoizedState) && isEffect(nextMemoizedState)) {
return !areHookInputsEqual(
nextMemoizedState.deps,
prevMemoizedState.deps,
);
}
return nextMemoizedState !== prevMemoizedState;
}

function didHooksChange(prev: any, next: any): boolean {
if (prev == null || next == null) {
return false;
Expand All @@ -1086,7 +1129,7 @@ export function attach(
next.hasOwnProperty('queue')
) {
while (next !== null) {
if (next.memoizedState !== prev.memoizedState) {
if (didHookChange(prev, next)) {
return true;
} else {
next = next.next;
Expand Down

0 comments on commit 2d58205

Please sign in to comment.