@@ -15,6 +15,7 @@ import type {
15
15
ReactProviderType ,
16
16
StartTransitionOptions ,
17
17
} from 'shared/ReactTypes' ;
18
+ import { REACT_MEMO_CACHE_SENTINEL } from 'shared/ReactSymbols' ;
18
19
import type {
19
20
Fiber ,
20
21
Dispatcher as DispatcherType ,
@@ -51,9 +52,19 @@ type Dispatch<A> = A => void;
51
52
52
53
let primitiveStackCache : null | Map < string , Array < any >> = null ;
53
54
55
+ type MemoCache = {
56
+ data : Array < Array < any >> ,
57
+ index : number ,
58
+ } ;
59
+
60
+ type FunctionComponentUpdateQueue = {
61
+ memoCache ?: MemoCache | null ,
62
+ } ;
63
+
54
64
type Hook = {
55
65
memoizedState : any ,
56
66
next : Hook | null ,
67
+ updateQueue : FunctionComponentUpdateQueue | null ,
57
68
} ;
58
69
59
70
function getPrimitiveStackCache ( ) : Map < string , Array < any >> {
@@ -79,6 +90,10 @@ function getPrimitiveStackCache(): Map<string, Array<any>> {
79
90
Dispatcher . useDebugValue ( null ) ;
80
91
Dispatcher . useCallback ( ( ) => { } ) ;
81
92
Dispatcher . useMemo ( ( ) => null ) ;
93
+ if ( typeof Dispatcher . useMemoCache === 'function' ) {
94
+ // This type check is for Flow only.
95
+ Dispatcher . useMemoCache ( 0 ) ;
96
+ }
82
97
} finally {
83
98
readHookLog = hookLog ;
84
99
hookLog = [ ] ;
@@ -326,6 +341,37 @@ function useId(): string {
326
341
return id ;
327
342
}
328
343
344
+ function useMemoCache(size: number): Array< any > {
345
+ const hook = nextHook ( ) ;
346
+ let memoCache : MemoCache ;
347
+ if (
348
+ hook !== null &&
349
+ hook . updateQueue !== null &&
350
+ hook . updateQueue . memoCache != null
351
+ ) {
352
+ memoCache = hook . updateQueue . memoCache ;
353
+ } else {
354
+ memoCache = {
355
+ data : [ ] ,
356
+ index : 0 ,
357
+ } ;
358
+ }
359
+
360
+ let data = memoCache . data [ memoCache . index ] ;
361
+ if ( data === undefined ) {
362
+ data = new Array ( size ) ;
363
+ for ( let i = 0 ; i < size ; i ++ ) {
364
+ data [ i ] = REACT_MEMO_CACHE_SENTINEL ;
365
+ }
366
+ }
367
+ hookLog . push ( {
368
+ primitive : 'MemoCache' ,
369
+ stackError : new Error ( ) ,
370
+ value : data ,
371
+ } ) ;
372
+ return data ;
373
+ }
374
+
329
375
const Dispatcher: DispatcherType = {
330
376
readContext ,
331
377
useCacheRefresh ,
@@ -337,6 +383,7 @@ const Dispatcher: DispatcherType = {
337
383
useLayoutEffect ,
338
384
useInsertionEffect ,
339
385
useMemo ,
386
+ useMemoCache ,
340
387
useReducer ,
341
388
useRef ,
342
389
useState ,
0 commit comments