26
ContextProvider,
27
ForwardRef,
28
} from 'react-reconciler/src/ReactWorkTags';
29
+import {REACT_MEMO_CACHE_SENTINEL} from 'shared/ReactSymbols';
30
31
type CurrentDispatcherRef = typeof ReactSharedInternals.ReactCurrentDispatcher;
32
94
return primitiveStackCache;
95
}
96
97
+let currentFiber: null | Fiber = null;
98
let currentHook: null | Hook = null;
99
+
100
function nextHook(): null | Hook {
101
const hook = currentHook;
102
if (hook !== null) {
322
323
// useMemoCache is an implementation detail of Forget's memoization
324
// it should not be called directly in user-generated code
322
-// we keep it as a stub for dispatcher
325
function useMemoCache(size: number): Array<any> {
324
- return [];
326
+ const fiber = currentFiber;
327
+ // Don't throw, in case this is called from getPrimitiveStackCache
328
+ if (fiber == null) {
329
+ return [];
330
+ }
331
+
332
+ // $FlowFixMe[incompatible-use]: updateQueue is mixed
333
+ const memoCache = fiber.updateQueue?.memoCache;
334
+ if (memoCache == null) {
335
+ return [];
336
+ }
337
+
338
+ let data = memoCache.data[memoCache.index];
339
+ if (data === undefined) {
340
+ data = memoCache.data[memoCache.index] = new Array(size);
341
+ for (let i = 0; i < size; i++) {
342
+ data[i] = REACT_MEMO_CACHE_SENTINEL;
343
+ }
344
+ }
345
+
346
+ // We don't write anything to hookLog on purpose, so this hook remains invisible to users.
347
+
348
+ memoCache.index++;
349
+ return data;
350
}
351
352
const Dispatcher: DispatcherType = {
724
}
725
726
const previousDispatcher = currentDispatcher.current;
702
- let readHookLog;
727
currentDispatcher.current = DispatcherProxy;
728
+
729
+ let readHookLog;
730
let ancestorStackError;
731
+
732
try {
733
ancestorStackError = new Error();
734
renderFunction(props);
825
'Unknown Fiber. Needs to be a function component to inspect hooks.',
826
);
827
}
828
+
829
// Warm up the cache so that it doesn't consume the currentHook.
830
getPrimitiveStackCache();
831
+
832
+ // Set up the current hook so that we can step through and read the
833
+ // current state from them.
834
+ currentHook = (fiber.memoizedState: Hook);
835
+ currentFiber = fiber;
836
+
837
const type = fiber.type;
838
let props = fiber.memoizedProps;
839
if (type !== fiber.elementType) {
840
props = resolveDefaultProps(type, props);
841
}
808
- // Set up the current hook so that we can step through and read the
809
- // current state from them.
810
- currentHook = (fiber.memoizedState: Hook);
811
- const contextMap = new Map<ReactContext<$FlowFixMe>, $FlowFixMe>();
842
+
843
+ const contextMap = new Map<ReactContext<any>, any>();
844
try {
845
setupContexts(contextMap, fiber);
846
+
847
if (fiber.tag === ForwardRef) {
848
return inspectHooksOfForwardRef(
849
type.render,
853
includeHooksSource,
854
);
855
}
856
+
857
return inspectHooks(type, props, currentDispatcher, includeHooksSource);
858
} finally {
859
+ currentFiber = null;
860
currentHook = null;
861
+
862
restoreContexts(contextMap);
863
}
864
}