Skip to content

Commit 9677b69

Browse files
committed
Less forking, more allocations
1 parent f0e52a1 commit 9677b69

1 file changed

Lines changed: 25 additions & 39 deletions

File tree

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ type HookLogEntry = {
4949
value: mixed,
5050
debugInfo: ReactDebugInfo | null,
5151
dispatcherHookName: string,
52-
/**
53-
* A list of hook function names that may call this dispatcher method.
54-
* If `null`, we assume that the wrapper name is equal to the dispatcher mthod name.
55-
*/
56-
wrapperNames: Array<string> | null,
52+
wrapperNames: Array<string>,
5753
};
5854

5955
let hookLog: Array<HookLogEntry> = [];
@@ -222,7 +218,7 @@ function use<T>(usable: Usable<T>): T {
222218
debugInfo:
223219
thenable._debugInfo === undefined ? null : thenable._debugInfo,
224220
dispatcherHookName: 'Use',
225-
wrapperNames: null,
221+
wrapperNames: ['Use'],
226222
});
227223
return fulfilledValue;
228224
}
@@ -241,7 +237,7 @@ function use<T>(usable: Usable<T>): T {
241237
debugInfo:
242238
thenable._debugInfo === undefined ? null : thenable._debugInfo,
243239
dispatcherHookName: 'Use',
244-
wrapperNames: null,
240+
wrapperNames: ['Use'],
245241
});
246242
throw SuspenseException;
247243
} else if (usable.$$typeof === REACT_CONTEXT_TYPE) {
@@ -255,7 +251,7 @@ function use<T>(usable: Usable<T>): T {
255251
value,
256252
debugInfo: null,
257253
dispatcherHookName: 'Use',
258-
wrapperNames: null,
254+
wrapperNames: ['Use'],
259255
});
260256

261257
return value;
@@ -275,7 +271,7 @@ function useContext<T>(context: ReactContext<T>): T {
275271
value: value,
276272
debugInfo: null,
277273
dispatcherHookName: 'Context',
278-
wrapperNames: null,
274+
wrapperNames: ['Context'],
279275
});
280276
return value;
281277
}
@@ -298,7 +294,7 @@ function useState<S>(
298294
value: state,
299295
debugInfo: null,
300296
dispatcherHookName: 'State',
301-
wrapperNames: null,
297+
wrapperNames: ['State'],
302298
});
303299
return [state, (action: BasicStateAction<S>) => {}];
304300
}
@@ -322,7 +318,7 @@ function useReducer<S, I, A>(
322318
value: state,
323319
debugInfo: null,
324320
dispatcherHookName: 'Reducer',
325-
wrapperNames: null,
321+
wrapperNames: ['Reducer'],
326322
});
327323
return [state, (action: A) => {}];
328324
}
@@ -337,7 +333,7 @@ function useRef<T>(initialValue: T): {current: T} {
337333
value: ref.current,
338334
debugInfo: null,
339335
dispatcherHookName: 'Ref',
340-
wrapperNames: null,
336+
wrapperNames: ['Ref'],
341337
});
342338
return ref;
343339
}
@@ -351,7 +347,7 @@ function useCacheRefresh(): () => void {
351347
value: hook !== null ? hook.memoizedState : function refresh() {},
352348
debugInfo: null,
353349
dispatcherHookName: 'CacheRefresh',
354-
wrapperNames: null,
350+
wrapperNames: ['CacheRefresh'],
355351
});
356352
return () => {};
357353
}
@@ -368,7 +364,7 @@ function useLayoutEffect(
368364
value: create,
369365
debugInfo: null,
370366
dispatcherHookName: 'LayoutEffect',
371-
wrapperNames: null,
367+
wrapperNames: ['LayoutEffect'],
372368
});
373369
}
374370

@@ -384,7 +380,7 @@ function useInsertionEffect(
384380
value: create,
385381
debugInfo: null,
386382
dispatcherHookName: 'InsertionEffect',
387-
wrapperNames: null,
383+
wrapperNames: ['InsertionEffect'],
388384
});
389385
}
390386

@@ -400,7 +396,7 @@ function useEffect(
400396
value: create,
401397
debugInfo: null,
402398
dispatcherHookName: 'Effect',
403-
wrapperNames: null,
399+
wrapperNames: ['Effect'],
404400
});
405401
}
406402

@@ -425,7 +421,7 @@ function useImperativeHandle<T>(
425421
value: instance,
426422
debugInfo: null,
427423
dispatcherHookName: 'ImperativeHandle',
428-
wrapperNames: null,
424+
wrapperNames: ['ImperativeHandle'],
429425
});
430426
}
431427

@@ -437,7 +433,7 @@ function useDebugValue(value: any, formatterFn: ?(value: any) => any) {
437433
value: typeof formatterFn === 'function' ? formatterFn(value) : value,
438434
debugInfo: null,
439435
dispatcherHookName: 'DebugValue',
440-
wrapperNames: null,
436+
wrapperNames: ['DebugValue'],
441437
});
442438
}
443439

@@ -450,7 +446,7 @@ function useCallback<T>(callback: T, inputs: Array<mixed> | void | null): T {
450446
value: hook !== null ? hook.memoizedState[0] : callback,
451447
debugInfo: null,
452448
dispatcherHookName: 'Callback',
453-
wrapperNames: null,
449+
wrapperNames: ['Callback'],
454450
});
455451
return callback;
456452
}
@@ -468,7 +464,7 @@ function useMemo<T>(
468464
value,
469465
debugInfo: null,
470466
dispatcherHookName: 'Memo',
471-
wrapperNames: null,
467+
wrapperNames: ['Memo'],
472468
});
473469
return value;
474470
}
@@ -491,7 +487,7 @@ function useSyncExternalStore<T>(
491487
value,
492488
debugInfo: null,
493489
dispatcherHookName: 'SyncExternalStore',
494-
wrapperNames: null,
490+
wrapperNames: ['SyncExternalStore'],
495491
});
496492
return value;
497493
}
@@ -515,7 +511,7 @@ function useTransition(): [
515511
value: isPending,
516512
debugInfo: null,
517513
dispatcherHookName: 'Transition',
518-
wrapperNames: null,
514+
wrapperNames: ['Transition'],
519515
});
520516
return [isPending, () => {}];
521517
}
@@ -530,7 +526,7 @@ function useDeferredValue<T>(value: T, initialValue?: T): T {
530526
value: prevValue,
531527
debugInfo: null,
532528
dispatcherHookName: 'DeferredValue',
533-
wrapperNames: null,
529+
wrapperNames: ['DeferredValue'],
534530
});
535531
return prevValue;
536532
}
@@ -545,7 +541,7 @@ function useId(): string {
545541
value: id,
546542
debugInfo: null,
547543
dispatcherHookName: 'Id',
548-
wrapperNames: null,
544+
wrapperNames: ['Id'],
549545
});
550546
return id;
551547
}
@@ -597,7 +593,7 @@ function useOptimistic<S, A>(
597593
value: state,
598594
debugInfo: null,
599595
dispatcherHookName: 'Optimistic',
600-
wrapperNames: null,
596+
wrapperNames: ['Optimistic'],
601597
});
602598
return [state, (action: A) => {}];
603599
}
@@ -658,7 +654,7 @@ function useFormState<S, P>(
658654
value: value,
659655
debugInfo: debugInfo,
660656
dispatcherHookName: 'FormState',
661-
wrapperNames: null,
657+
wrapperNames: ['FormState'],
662658
});
663659

664660
if (error !== null) {
@@ -729,7 +725,7 @@ function useActionState<S, P>(
729725
value: value,
730726
debugInfo: debugInfo,
731727
dispatcherHookName: 'ActionState',
732-
wrapperNames: null,
728+
wrapperNames: ['ActionState'],
733729
});
734730

735731
if (error !== null) {
@@ -914,18 +910,8 @@ function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
914910
) {
915911
i++;
916912
}
917-
if (hook.wrapperNames !== null) {
918-
for (let j = 0; j < hook.wrapperNames.length; j++) {
919-
const wrapperName = hook.wrapperNames[j];
920-
if (
921-
i < hookStack.length - 1 &&
922-
isReactWrapper(hookStack[i].functionName, wrapperName)
923-
) {
924-
i++;
925-
}
926-
}
927-
} else {
928-
const wrapperName = hook.dispatcherHookName;
913+
for (let j = 0; j < hook.wrapperNames.length; j++) {
914+
const wrapperName = hook.wrapperNames[j];
929915
if (
930916
i < hookStack.length - 1 &&
931917
isReactWrapper(hookStack[i].functionName, wrapperName)

0 commit comments

Comments
 (0)