Skip to content

Commit

Permalink
try/finally simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
josephsavona committed Oct 14, 2022
1 parent 1e9bba2 commit d4469c1
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 198 deletions.
25 changes: 14 additions & 11 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import type {
MemoCache,
MutableSource,
MutableSourceGetSnapshotFn,
MutableSourceSubscribeFn,
Expand All @@ -21,7 +20,7 @@ import type {
Fiber,
Dispatcher,
HookType,
MemoCacheStorage,
MemoCache,
EventFunctionWrapper,
} from './ReactInternalTypes';
import type {Lanes, Lane} from './ReactFiberLane.new';
Expand Down Expand Up @@ -49,7 +48,6 @@ import {
REACT_CONTEXT_TYPE,
REACT_SERVER_CONTEXT_TYPE,
} from 'shared/ReactSymbols';
import {createMemoCache, MEMO_CACHE_UNSET_SENTINEL} from './ReactMemoCache';

import {
NoMode,
Expand Down Expand Up @@ -197,7 +195,7 @@ export type FunctionComponentUpdateQueue = {
events: Array<() => mixed> | null,
stores: Array<StoreConsistencyCheck<any>> | null,
// NOTE: optional, only set when enableUseMemoCacheHook is enabled
memoCache?: MemoCacheStorage | null,
memoCache?: MemoCache | null,
};

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

function useMemoCache(size: number): MemoCache {
const MEMO_CACHE_UNSET_SENTINEL: symbol = Symbol.for(
'react.usememocache_sentinel',
);
function useMemoCache(size: number): Array<any> {
let memoCache = null;
// Fast-path, load memo cache from wip fiber if already prepared
let updateQueue: FunctionComponentUpdateQueue | null = (currentlyRenderingFiber.updateQueue: any);
Expand All @@ -853,8 +854,7 @@ function useMemoCache(size: number): MemoCache {
if (current !== null) {
const currentUpdateQueue: FunctionComponentUpdateQueue | null = (current.updateQueue: any);
if (currentUpdateQueue !== null) {
const currentMemoCache: ?MemoCacheStorage =
currentUpdateQueue.memoCache;
const currentMemoCache: ?MemoCache = currentUpdateQueue.memoCache;
if (currentMemoCache != null) {
memoCache = {
data: currentMemoCache.data.map(array => array.slice()),
Expand Down Expand Up @@ -882,6 +882,9 @@ function useMemoCache(size: number): MemoCache {
data = memoCache.data[memoCache.index] = new Array(size).fill(
MEMO_CACHE_UNSET_SENTINEL,
);
// Attach sentinel to the array, eventually we can make the sentinel a public
// export from React and avoid modifying the array
(data: any)._ = MEMO_CACHE_UNSET_SENTINEL;
} else if (data.length !== size) {
// TODO: consider warning or throwing here
if (__DEV__) {
Expand All @@ -894,7 +897,7 @@ function useMemoCache(size: number): MemoCache {
}
}
memoCache.index++;
return createMemoCache(data);
return data;
}

function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
Expand Down Expand Up @@ -3611,7 +3614,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = function(
size: number,
): MemoCache {
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
Expand Down Expand Up @@ -3798,7 +3801,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = function(
size: number,
): MemoCache {
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
Expand Down Expand Up @@ -3986,7 +3989,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = function(
size: number,
): MemoCache {
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
Expand Down
25 changes: 14 additions & 11 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import type {
MemoCache,
MutableSource,
MutableSourceGetSnapshotFn,
MutableSourceSubscribeFn,
Expand All @@ -21,7 +20,7 @@ import type {
Fiber,
Dispatcher,
HookType,
MemoCacheStorage,
MemoCache,
EventFunctionWrapper,
} from './ReactInternalTypes';
import type {Lanes, Lane} from './ReactFiberLane.old';
Expand Down Expand Up @@ -49,7 +48,6 @@ import {
REACT_CONTEXT_TYPE,
REACT_SERVER_CONTEXT_TYPE,
} from 'shared/ReactSymbols';
import {createMemoCache, MEMO_CACHE_UNSET_SENTINEL} from './ReactMemoCache';

import {
NoMode,
Expand Down Expand Up @@ -197,7 +195,7 @@ export type FunctionComponentUpdateQueue = {
events: Array<() => mixed> | null,
stores: Array<StoreConsistencyCheck<any>> | null,
// NOTE: optional, only set when enableUseMemoCacheHook is enabled
memoCache?: MemoCacheStorage | null,
memoCache?: MemoCache | null,
};

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

function useMemoCache(size: number): MemoCache {
const MEMO_CACHE_UNSET_SENTINEL: symbol = Symbol.for(
'react.usememocache_sentinel',
);
function useMemoCache(size: number): Array<any> {
let memoCache = null;
// Fast-path, load memo cache from wip fiber if already prepared
let updateQueue: FunctionComponentUpdateQueue | null = (currentlyRenderingFiber.updateQueue: any);
Expand All @@ -853,8 +854,7 @@ function useMemoCache(size: number): MemoCache {
if (current !== null) {
const currentUpdateQueue: FunctionComponentUpdateQueue | null = (current.updateQueue: any);
if (currentUpdateQueue !== null) {
const currentMemoCache: ?MemoCacheStorage =
currentUpdateQueue.memoCache;
const currentMemoCache: ?MemoCache = currentUpdateQueue.memoCache;
if (currentMemoCache != null) {
memoCache = {
data: currentMemoCache.data.map(array => array.slice()),
Expand Down Expand Up @@ -882,6 +882,9 @@ function useMemoCache(size: number): MemoCache {
data = memoCache.data[memoCache.index] = new Array(size).fill(
MEMO_CACHE_UNSET_SENTINEL,
);
// Attach sentinel to the array, eventually we can make the sentinel a public
// export from React and avoid modifying the array
(data: any)._ = MEMO_CACHE_UNSET_SENTINEL;
} else if (data.length !== size) {
// TODO: consider warning or throwing here
if (__DEV__) {
Expand All @@ -894,7 +897,7 @@ function useMemoCache(size: number): MemoCache {
}
}
memoCache.index++;
return createMemoCache(data);
return data;
}

function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
Expand Down Expand Up @@ -3611,7 +3614,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = function(
size: number,
): MemoCache {
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
Expand Down Expand Up @@ -3798,7 +3801,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = function(
size: number,
): MemoCache {
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
Expand Down Expand Up @@ -3986,7 +3989,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = function(
size: number,
): MemoCache {
): Array<any> {
warnInvalidHookAccess();
return useMemoCache(size);
};
Expand Down
5 changes: 2 additions & 3 deletions packages/react-reconciler/src/ReactInternalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import type {Source} from 'shared/ReactElementType';
import type {
MemoCache,
RefObject,
ReactContext,
MutableSourceSubscribeFn,
Expand Down Expand Up @@ -74,7 +73,7 @@ export type Dependencies = {
...
};

export type MemoCacheStorage = {
export type MemoCache = {
data: Array<Array<any>>,
index: number,
};
Expand Down Expand Up @@ -429,7 +428,7 @@ export type Dispatcher = {
): T,
useId(): string,
useCacheRefresh?: () => <T>(?() => T, ?T) => void,
useMemoCache?: (size: number) => MemoCache,
useMemoCache?: (size: number) => Array<any>,

unstable_isNewReconciler?: boolean,
};
59 changes: 0 additions & 59 deletions packages/react-reconciler/src/ReactMemoCache.js

This file was deleted.

Loading

0 comments on commit d4469c1

Please sign in to comment.