Skip to content

Commit d4469c1

Browse files
committed
try/finally simplification
1 parent 1e9bba2 commit d4469c1

File tree

10 files changed

+88
-198
lines changed

10 files changed

+88
-198
lines changed

packages/react-reconciler/src/ReactFiberHooks.new.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import type {
11-
MemoCache,
1211
MutableSource,
1312
MutableSourceGetSnapshotFn,
1413
MutableSourceSubscribeFn,
@@ -21,7 +20,7 @@ import type {
2120
Fiber,
2221
Dispatcher,
2322
HookType,
24-
MemoCacheStorage,
23+
MemoCache,
2524
EventFunctionWrapper,
2625
} from './ReactInternalTypes';
2726
import type {Lanes, Lane} from './ReactFiberLane.new';
@@ -49,7 +48,6 @@ import {
4948
REACT_CONTEXT_TYPE,
5049
REACT_SERVER_CONTEXT_TYPE,
5150
} from 'shared/ReactSymbols';
52-
import {createMemoCache, MEMO_CACHE_UNSET_SENTINEL} from './ReactMemoCache';
5351

5452
import {
5553
NoMode,
@@ -197,7 +195,7 @@ export type FunctionComponentUpdateQueue = {
197195
events: Array<() => mixed> | null,
198196
stores: Array<StoreConsistencyCheck<any>> | null,
199197
// NOTE: optional, only set when enableUseMemoCacheHook is enabled
200-
memoCache?: MemoCacheStorage | null,
198+
memoCache?: MemoCache | null,
201199
};
202200

203201
type BasicStateAction<S> = (S => S) | S;
@@ -840,7 +838,10 @@ function use<T>(usable: Usable<T>): T {
840838
throw new Error('An unsupported type was passed to use(): ' + String(usable));
841839
}
842840

843-
function useMemoCache(size: number): MemoCache {
841+
const MEMO_CACHE_UNSET_SENTINEL: symbol = Symbol.for(
842+
'react.usememocache_sentinel',
843+
);
844+
function useMemoCache(size: number): Array<any> {
844845
let memoCache = null;
845846
// Fast-path, load memo cache from wip fiber if already prepared
846847
let updateQueue: FunctionComponentUpdateQueue | null = (currentlyRenderingFiber.updateQueue: any);
@@ -853,8 +854,7 @@ function useMemoCache(size: number): MemoCache {
853854
if (current !== null) {
854855
const currentUpdateQueue: FunctionComponentUpdateQueue | null = (current.updateQueue: any);
855856
if (currentUpdateQueue !== null) {
856-
const currentMemoCache: ?MemoCacheStorage =
857-
currentUpdateQueue.memoCache;
857+
const currentMemoCache: ?MemoCache = currentUpdateQueue.memoCache;
858858
if (currentMemoCache != null) {
859859
memoCache = {
860860
data: currentMemoCache.data.map(array => array.slice()),
@@ -882,6 +882,9 @@ function useMemoCache(size: number): MemoCache {
882882
data = memoCache.data[memoCache.index] = new Array(size).fill(
883883
MEMO_CACHE_UNSET_SENTINEL,
884884
);
885+
// Attach sentinel to the array, eventually we can make the sentinel a public
886+
// export from React and avoid modifying the array
887+
(data: any)._ = MEMO_CACHE_UNSET_SENTINEL;
885888
} else if (data.length !== size) {
886889
// TODO: consider warning or throwing here
887890
if (__DEV__) {
@@ -894,7 +897,7 @@ function useMemoCache(size: number): MemoCache {
894897
}
895898
}
896899
memoCache.index++;
897-
return createMemoCache(data);
900+
return data;
898901
}
899902

900903
function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
@@ -3611,7 +3614,7 @@ if (__DEV__) {
36113614
if (enableUseMemoCacheHook) {
36123615
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = function(
36133616
size: number,
3614-
): MemoCache {
3617+
): Array<any> {
36153618
warnInvalidHookAccess();
36163619
return useMemoCache(size);
36173620
};
@@ -3798,7 +3801,7 @@ if (__DEV__) {
37983801
if (enableUseMemoCacheHook) {
37993802
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = function(
38003803
size: number,
3801-
): MemoCache {
3804+
): Array<any> {
38023805
warnInvalidHookAccess();
38033806
return useMemoCache(size);
38043807
};
@@ -3986,7 +3989,7 @@ if (__DEV__) {
39863989
if (enableUseMemoCacheHook) {
39873990
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = function(
39883991
size: number,
3989-
): MemoCache {
3992+
): Array<any> {
39903993
warnInvalidHookAccess();
39913994
return useMemoCache(size);
39923995
};

packages/react-reconciler/src/ReactFiberHooks.old.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import type {
11-
MemoCache,
1211
MutableSource,
1312
MutableSourceGetSnapshotFn,
1413
MutableSourceSubscribeFn,
@@ -21,7 +20,7 @@ import type {
2120
Fiber,
2221
Dispatcher,
2322
HookType,
24-
MemoCacheStorage,
23+
MemoCache,
2524
EventFunctionWrapper,
2625
} from './ReactInternalTypes';
2726
import type {Lanes, Lane} from './ReactFiberLane.old';
@@ -49,7 +48,6 @@ import {
4948
REACT_CONTEXT_TYPE,
5049
REACT_SERVER_CONTEXT_TYPE,
5150
} from 'shared/ReactSymbols';
52-
import {createMemoCache, MEMO_CACHE_UNSET_SENTINEL} from './ReactMemoCache';
5351

5452
import {
5553
NoMode,
@@ -197,7 +195,7 @@ export type FunctionComponentUpdateQueue = {
197195
events: Array<() => mixed> | null,
198196
stores: Array<StoreConsistencyCheck<any>> | null,
199197
// NOTE: optional, only set when enableUseMemoCacheHook is enabled
200-
memoCache?: MemoCacheStorage | null,
198+
memoCache?: MemoCache | null,
201199
};
202200

203201
type BasicStateAction<S> = (S => S) | S;
@@ -840,7 +838,10 @@ function use<T>(usable: Usable<T>): T {
840838
throw new Error('An unsupported type was passed to use(): ' + String(usable));
841839
}
842840

843-
function useMemoCache(size: number): MemoCache {
841+
const MEMO_CACHE_UNSET_SENTINEL: symbol = Symbol.for(
842+
'react.usememocache_sentinel',
843+
);
844+
function useMemoCache(size: number): Array<any> {
844845
let memoCache = null;
845846
// Fast-path, load memo cache from wip fiber if already prepared
846847
let updateQueue: FunctionComponentUpdateQueue | null = (currentlyRenderingFiber.updateQueue: any);
@@ -853,8 +854,7 @@ function useMemoCache(size: number): MemoCache {
853854
if (current !== null) {
854855
const currentUpdateQueue: FunctionComponentUpdateQueue | null = (current.updateQueue: any);
855856
if (currentUpdateQueue !== null) {
856-
const currentMemoCache: ?MemoCacheStorage =
857-
currentUpdateQueue.memoCache;
857+
const currentMemoCache: ?MemoCache = currentUpdateQueue.memoCache;
858858
if (currentMemoCache != null) {
859859
memoCache = {
860860
data: currentMemoCache.data.map(array => array.slice()),
@@ -882,6 +882,9 @@ function useMemoCache(size: number): MemoCache {
882882
data = memoCache.data[memoCache.index] = new Array(size).fill(
883883
MEMO_CACHE_UNSET_SENTINEL,
884884
);
885+
// Attach sentinel to the array, eventually we can make the sentinel a public
886+
// export from React and avoid modifying the array
887+
(data: any)._ = MEMO_CACHE_UNSET_SENTINEL;
885888
} else if (data.length !== size) {
886889
// TODO: consider warning or throwing here
887890
if (__DEV__) {
@@ -894,7 +897,7 @@ function useMemoCache(size: number): MemoCache {
894897
}
895898
}
896899
memoCache.index++;
897-
return createMemoCache(data);
900+
return data;
898901
}
899902

900903
function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
@@ -3611,7 +3614,7 @@ if (__DEV__) {
36113614
if (enableUseMemoCacheHook) {
36123615
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = function(
36133616
size: number,
3614-
): MemoCache {
3617+
): Array<any> {
36153618
warnInvalidHookAccess();
36163619
return useMemoCache(size);
36173620
};
@@ -3798,7 +3801,7 @@ if (__DEV__) {
37983801
if (enableUseMemoCacheHook) {
37993802
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = function(
38003803
size: number,
3801-
): MemoCache {
3804+
): Array<any> {
38023805
warnInvalidHookAccess();
38033806
return useMemoCache(size);
38043807
};
@@ -3986,7 +3989,7 @@ if (__DEV__) {
39863989
if (enableUseMemoCacheHook) {
39873990
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = function(
39883991
size: number,
3989-
): MemoCache {
3992+
): Array<any> {
39903993
warnInvalidHookAccess();
39913994
return useMemoCache(size);
39923995
};

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import type {Source} from 'shared/ReactElementType';
1111
import type {
12-
MemoCache,
1312
RefObject,
1413
ReactContext,
1514
MutableSourceSubscribeFn,
@@ -74,7 +73,7 @@ export type Dependencies = {
7473
...
7574
};
7675

77-
export type MemoCacheStorage = {
76+
export type MemoCache = {
7877
data: Array<Array<any>>,
7978
index: number,
8079
};
@@ -429,7 +428,7 @@ export type Dispatcher = {
429428
): T,
430429
useId(): string,
431430
useCacheRefresh?: () => <T>(?() => T, ?T) => void,
432-
useMemoCache?: (size: number) => MemoCache,
431+
useMemoCache?: (size: number) => Array<any>,
433432

434433
unstable_isNewReconciler?: boolean,
435434
};

packages/react-reconciler/src/ReactMemoCache.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)