Skip to content

Commit

Permalink
Flush out useMemoCache API
Browse files Browse the repository at this point in the history
  • Loading branch information
josephsavona committed Oct 14, 2022
1 parent 3b81432 commit d3aa0d0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
6 changes: 4 additions & 2 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
import {
REACT_CONTEXT_TYPE,
REACT_SERVER_CONTEXT_TYPE,
REACT_USE_MEMO_CACHE_SENTINEL,
} from 'shared/ReactSymbols';

import {
Expand Down Expand Up @@ -845,8 +846,6 @@ function useMemoCache(size: number): Array<any> {
memoCache = updateQueue.memoCache;
}
// Otherwise clone from the current fiber
// TODO: not sure how to access the current fiber here other than going through
// currentlyRenderingFiber.alternate
if (memoCache == null) {
const current: Fiber | null = currentlyRenderingFiber.alternate;
if (current !== null) {
Expand Down Expand Up @@ -878,6 +877,9 @@ function useMemoCache(size: number): Array<any> {
let data = memoCache.data[memoCache.index];
if (data === undefined) {
data = memoCache.data[memoCache.index] = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = REACT_USE_MEMO_CACHE_SENTINEL;
}
} else if (data.length !== size) {
// TODO: consider warning or throwing here
if (__DEV__) {
Expand Down
6 changes: 4 additions & 2 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
import {
REACT_CONTEXT_TYPE,
REACT_SERVER_CONTEXT_TYPE,
REACT_USE_MEMO_CACHE_SENTINEL,
} from 'shared/ReactSymbols';

import {
Expand Down Expand Up @@ -845,8 +846,6 @@ function useMemoCache(size: number): Array<any> {
memoCache = updateQueue.memoCache;
}
// Otherwise clone from the current fiber
// TODO: not sure how to access the current fiber here other than going through
// currentlyRenderingFiber.alternate
if (memoCache == null) {
const current: Fiber | null = currentlyRenderingFiber.alternate;
if (current !== null) {
Expand Down Expand Up @@ -878,6 +877,9 @@ function useMemoCache(size: number): Array<any> {
let data = memoCache.data[memoCache.index];
if (data === undefined) {
data = memoCache.data[memoCache.index] = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = REACT_USE_MEMO_CACHE_SENTINEL;
}
} else if (data.length !== size) {
// TODO: consider warning or throwing here
if (__DEV__) {
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/__tests__/useMemoCache-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const {REACT_USE_MEMO_CACHE_SENTINEL} = require('shared/ReactSymbols');

let React;
let ReactNoop;
let act;
Expand Down Expand Up @@ -46,7 +48,7 @@ describe('useMemoCache()', () => {
const cache = useMemoCache(1);
expect(Array.isArray(cache)).toBe(true);
expect(cache.length).toBe(1);
expect(cache[0]).toBe(undefined);
expect(cache[0]).toBe(REACT_USE_MEMO_CACHE_SENTINEL);
return 'Ok';
}
const root = ReactNoop.createRoot();
Expand Down
7 changes: 6 additions & 1 deletion packages/react-server/src/ReactFizzHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import is from 'shared/objectIs';
import {
REACT_SERVER_CONTEXT_TYPE,
REACT_CONTEXT_TYPE,
REACT_USE_MEMO_CACHE_SENTINEL,
} from 'shared/ReactSymbols';

type BasicStateAction<S> = (S => S) | S;
Expand Down Expand Up @@ -666,7 +667,11 @@ function useCacheRefresh(): <T>(?() => T, ?T) => void {
}

function useMemoCache(size: number): Array<any> {
return new Array(size);
const data = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = REACT_USE_MEMO_CACHE_SENTINEL;
}
return data;
}

function noop(): void {}
Expand Down
11 changes: 9 additions & 2 deletions packages/react-server/src/ReactFlightHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import type {Dispatcher} from 'react-reconciler/src/ReactInternalTypes';
import type {Request} from './ReactFlightServer';
import type {ReactServerContext, Thenable, Usable} from 'shared/ReactTypes';
import type {ThenableState} from './ReactFlightWakeable';
import {REACT_SERVER_CONTEXT_TYPE} from 'shared/ReactSymbols';
import {
REACT_SERVER_CONTEXT_TYPE,
REACT_USE_MEMO_CACHE_SENTINEL,
} from 'shared/ReactSymbols';
import {readContext as readContextImpl} from './ReactFlightNewContext';
import {enableUseHook} from 'shared/ReactFeatureFlags';
import {
Expand Down Expand Up @@ -90,7 +93,11 @@ export const HooksDispatcher: Dispatcher = {
return unsupportedRefresh;
},
useMemoCache(size: number): Array<any> {
return new Array(size);
const data = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = REACT_USE_MEMO_CACHE_SENTINEL;
}
return data;
},
use: enableUseHook ? use : (unsupportedHook: any),
};
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
REACT_SCOPE_TYPE,
REACT_CACHE_TYPE,
REACT_TRACING_MARKER_TYPE,
REACT_USE_MEMO_CACHE_SENTINEL,
} from 'shared/ReactSymbols';

import {Component, PureComponent} from './ReactBaseClasses';
Expand Down Expand Up @@ -139,6 +140,7 @@ export {
REACT_CACHE_TYPE as unstable_Cache,
use as experimental_use,
useMemoCache as unstable_useMemoCache,
REACT_USE_MEMO_CACHE_SENTINEL as unstable_MemoCacheSentinel,
// enableScopeAPI
REACT_SCOPE_TYPE as unstable_Scope,
// enableTransitionTracing
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/ReactSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export const REACT_SERVER_CONTEXT_DEFAULT_VALUE_NOT_LOADED: symbol = Symbol.for(
'react.default_value',
);

export const REACT_USE_MEMO_CACHE_SENTINEL: symbol = Symbol.for(
'react.use_memo_cache_sentinel',
);

const MAYBE_ITERATOR_SYMBOL = Symbol.iterator;
const FAUX_ITERATOR_SYMBOL = '@@iterator';

Expand Down

0 comments on commit d3aa0d0

Please sign in to comment.