Skip to content

Commit 9377581

Browse files
committed
Scaffolding for experimental_use hook
Sets up a new experimental hook behind a feature flag, but does not implement it yet.
1 parent 5b6e318 commit 9377581

19 files changed

+135
-0
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
MutableSourceSubscribeFn,
1414
ReactContext,
1515
StartTransitionOptions,
16+
Usable,
1617
} from 'shared/ReactTypes';
1718
import type {Fiber, Dispatcher, HookType} from './ReactInternalTypes';
1819
import type {Lanes, Lane} from './ReactFiberLane.new';
@@ -32,6 +33,7 @@ import {
3233
enableLazyContextPropagation,
3334
enableUseMutableSource,
3435
enableTransitionTracing,
36+
enableUseHook,
3537
} from 'shared/ReactFeatureFlags';
3638

3739
import {
@@ -721,6 +723,10 @@ function createFunctionComponentUpdateQueue(): FunctionComponentUpdateQueue {
721723
};
722724
}
723725

726+
function use<T>(usable: Usable<T>): T {
727+
throw new Error('Not implemented.');
728+
}
729+
724730
function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
725731
// $FlowFixMe: Flow doesn't like mixed types
726732
return typeof action === 'function' ? action(state) : action;
@@ -2416,6 +2422,9 @@ if (enableCache) {
24162422
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
24172423
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
24182424
}
2425+
if (enableUseHook) {
2426+
(ContextOnlyDispatcher: Dispatcher).use = throwInvalidHookError;
2427+
}
24192428

24202429
const HooksDispatcherOnMount: Dispatcher = {
24212430
readContext,
@@ -2444,6 +2453,9 @@ if (enableCache) {
24442453
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
24452454
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
24462455
}
2456+
if (enableUseHook) {
2457+
(HooksDispatcherOnMount: Dispatcher).use = use;
2458+
}
24472459
const HooksDispatcherOnUpdate: Dispatcher = {
24482460
readContext,
24492461

@@ -2471,6 +2483,9 @@ if (enableCache) {
24712483
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
24722484
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
24732485
}
2486+
if (enableUseHook) {
2487+
(HooksDispatcherOnUpdate: Dispatcher).use = use;
2488+
}
24742489

24752490
const HooksDispatcherOnRerender: Dispatcher = {
24762491
readContext,
@@ -2499,6 +2514,9 @@ if (enableCache) {
24992514
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
25002515
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
25012516
}
2517+
if (enableUseHook) {
2518+
(HooksDispatcherOnRerender: Dispatcher).use = use;
2519+
}
25022520

25032521
let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
25042522
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
@@ -2674,6 +2692,9 @@ if (__DEV__) {
26742692
return mountRefresh();
26752693
};
26762694
}
2695+
if (enableUseHook) {
2696+
(HooksDispatcherOnMountInDEV: Dispatcher).use = use;
2697+
}
26772698

26782699
HooksDispatcherOnMountWithHookTypesInDEV = {
26792700
readContext<T>(context: ReactContext<T>): T {
@@ -2816,6 +2837,9 @@ if (__DEV__) {
28162837
return mountRefresh();
28172838
};
28182839
}
2840+
if (enableUseHook) {
2841+
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).use = use;
2842+
}
28192843

28202844
HooksDispatcherOnUpdateInDEV = {
28212845
readContext<T>(context: ReactContext<T>): T {
@@ -2958,6 +2982,9 @@ if (__DEV__) {
29582982
return updateRefresh();
29592983
};
29602984
}
2985+
if (enableUseHook) {
2986+
(HooksDispatcherOnUpdateInDEV: Dispatcher).use = use;
2987+
}
29612988

29622989
HooksDispatcherOnRerenderInDEV = {
29632990
readContext<T>(context: ReactContext<T>): T {
@@ -3101,6 +3128,9 @@ if (__DEV__) {
31013128
return updateRefresh();
31023129
};
31033130
}
3131+
if (enableUseHook) {
3132+
(HooksDispatcherOnRerenderInDEV: Dispatcher).use = use;
3133+
}
31043134

31053135
InvalidNestedHooksDispatcherOnMountInDEV = {
31063136
readContext<T>(context: ReactContext<T>): T {
@@ -3260,6 +3290,14 @@ if (__DEV__) {
32603290
return mountRefresh();
32613291
};
32623292
}
3293+
if (enableUseHook) {
3294+
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).use = function<T>(
3295+
usable: Usable<T>,
3296+
): T {
3297+
warnInvalidHookAccess();
3298+
return use(usable);
3299+
};
3300+
}
32633301

32643302
InvalidNestedHooksDispatcherOnUpdateInDEV = {
32653303
readContext<T>(context: ReactContext<T>): T {
@@ -3419,6 +3457,14 @@ if (__DEV__) {
34193457
return updateRefresh();
34203458
};
34213459
}
3460+
if (enableUseHook) {
3461+
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).use = function<T>(
3462+
usable: Usable<T>,
3463+
): T {
3464+
warnInvalidHookAccess();
3465+
return use(usable);
3466+
};
3467+
}
34223468

34233469
InvalidNestedHooksDispatcherOnRerenderInDEV = {
34243470
readContext<T>(context: ReactContext<T>): T {
@@ -3579,4 +3625,12 @@ if (__DEV__) {
35793625
return updateRefresh();
35803626
};
35813627
}
3628+
if (enableUseHook) {
3629+
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).use = function<T>(
3630+
usable: Usable<T>,
3631+
): T {
3632+
warnInvalidHookAccess();
3633+
return use(usable);
3634+
};
3635+
}
35823636
}

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
MutableSourceSubscribeFn,
1414
ReactContext,
1515
StartTransitionOptions,
16+
Usable,
1617
} from 'shared/ReactTypes';
1718
import type {Fiber, Dispatcher, HookType} from './ReactInternalTypes';
1819
import type {Lanes, Lane} from './ReactFiberLane.old';
@@ -32,6 +33,7 @@ import {
3233
enableLazyContextPropagation,
3334
enableUseMutableSource,
3435
enableTransitionTracing,
36+
enableUseHook,
3537
} from 'shared/ReactFeatureFlags';
3638

3739
import {
@@ -721,6 +723,10 @@ function createFunctionComponentUpdateQueue(): FunctionComponentUpdateQueue {
721723
};
722724
}
723725

726+
function use<T>(usable: Usable<T>): T {
727+
throw new Error('Not implemented.');
728+
}
729+
724730
function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
725731
// $FlowFixMe: Flow doesn't like mixed types
726732
return typeof action === 'function' ? action(state) : action;
@@ -2416,6 +2422,9 @@ if (enableCache) {
24162422
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
24172423
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
24182424
}
2425+
if (enableUseHook) {
2426+
(ContextOnlyDispatcher: Dispatcher).use = throwInvalidHookError;
2427+
}
24192428

24202429
const HooksDispatcherOnMount: Dispatcher = {
24212430
readContext,
@@ -2444,6 +2453,9 @@ if (enableCache) {
24442453
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
24452454
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
24462455
}
2456+
if (enableUseHook) {
2457+
(HooksDispatcherOnMount: Dispatcher).use = use;
2458+
}
24472459
const HooksDispatcherOnUpdate: Dispatcher = {
24482460
readContext,
24492461

@@ -2471,6 +2483,9 @@ if (enableCache) {
24712483
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
24722484
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
24732485
}
2486+
if (enableUseHook) {
2487+
(HooksDispatcherOnUpdate: Dispatcher).use = use;
2488+
}
24742489

24752490
const HooksDispatcherOnRerender: Dispatcher = {
24762491
readContext,
@@ -2499,6 +2514,9 @@ if (enableCache) {
24992514
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
25002515
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
25012516
}
2517+
if (enableUseHook) {
2518+
(HooksDispatcherOnRerender: Dispatcher).use = use;
2519+
}
25022520

25032521
let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
25042522
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
@@ -2674,6 +2692,9 @@ if (__DEV__) {
26742692
return mountRefresh();
26752693
};
26762694
}
2695+
if (enableUseHook) {
2696+
(HooksDispatcherOnMountInDEV: Dispatcher).use = use;
2697+
}
26772698

26782699
HooksDispatcherOnMountWithHookTypesInDEV = {
26792700
readContext<T>(context: ReactContext<T>): T {
@@ -2816,6 +2837,9 @@ if (__DEV__) {
28162837
return mountRefresh();
28172838
};
28182839
}
2840+
if (enableUseHook) {
2841+
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).use = use;
2842+
}
28192843

28202844
HooksDispatcherOnUpdateInDEV = {
28212845
readContext<T>(context: ReactContext<T>): T {
@@ -2958,6 +2982,9 @@ if (__DEV__) {
29582982
return updateRefresh();
29592983
};
29602984
}
2985+
if (enableUseHook) {
2986+
(HooksDispatcherOnUpdateInDEV: Dispatcher).use = use;
2987+
}
29612988

29622989
HooksDispatcherOnRerenderInDEV = {
29632990
readContext<T>(context: ReactContext<T>): T {
@@ -3101,6 +3128,9 @@ if (__DEV__) {
31013128
return updateRefresh();
31023129
};
31033130
}
3131+
if (enableUseHook) {
3132+
(HooksDispatcherOnRerenderInDEV: Dispatcher).use = use;
3133+
}
31043134

31053135
InvalidNestedHooksDispatcherOnMountInDEV = {
31063136
readContext<T>(context: ReactContext<T>): T {
@@ -3260,6 +3290,14 @@ if (__DEV__) {
32603290
return mountRefresh();
32613291
};
32623292
}
3293+
if (enableUseHook) {
3294+
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).use = function<T>(
3295+
usable: Usable<T>,
3296+
): T {
3297+
warnInvalidHookAccess();
3298+
return use(usable);
3299+
};
3300+
}
32633301

32643302
InvalidNestedHooksDispatcherOnUpdateInDEV = {
32653303
readContext<T>(context: ReactContext<T>): T {
@@ -3419,6 +3457,14 @@ if (__DEV__) {
34193457
return updateRefresh();
34203458
};
34213459
}
3460+
if (enableUseHook) {
3461+
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).use = function<T>(
3462+
usable: Usable<T>,
3463+
): T {
3464+
warnInvalidHookAccess();
3465+
return use(usable);
3466+
};
3467+
}
34223468

34233469
InvalidNestedHooksDispatcherOnRerenderInDEV = {
34243470
readContext<T>(context: ReactContext<T>): T {
@@ -3579,4 +3625,12 @@ if (__DEV__) {
35793625
return updateRefresh();
35803626
};
35813627
}
3628+
if (enableUseHook) {
3629+
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).use = function<T>(
3630+
usable: Usable<T>,
3631+
): T {
3632+
warnInvalidHookAccess();
3633+
return use(usable);
3634+
};
3635+
}
35823636
}

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
MutableSource,
1818
StartTransitionOptions,
1919
Wakeable,
20+
Usable,
2021
} from 'shared/ReactTypes';
2122
import type {SuspenseInstance} from './ReactFiberHostConfig';
2223
import type {WorkTag} from './ReactWorkTags';
@@ -355,6 +356,7 @@ type BasicStateAction<S> = (S => S) | S;
355356
type Dispatch<A> = A => void;
356357

357358
export type Dispatcher = {|
359+
use?: <T>(Usable<T>) => T,
358360
getCacheSignal?: () => AbortSignal,
359361
getCacheForType?: <T>(resourceType: () => T) => T,
360362
readContext<T>(context: ReactContext<T>): T,

packages/react/index.classic.fb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export {
2727
createMutableSource as unstable_createMutableSource,
2828
createRef,
2929
createServerContext,
30+
experimental_use,
3031
forwardRef,
3132
isValidElement,
3233
lazy,

packages/react/index.experimental.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export {
2424
createFactory,
2525
createRef,
2626
createServerContext,
27+
experimental_use,
2728
forwardRef,
2829
isValidElement,
2930
lazy,

packages/react/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export {
4949
createMutableSource,
5050
createRef,
5151
createServerContext,
52+
experimental_use,
5253
forwardRef,
5354
isValidElement,
5455
lazy,

packages/react/index.modern.fb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export {
2626
createMutableSource as unstable_createMutableSource,
2727
createRef,
2828
createServerContext,
29+
experimental_use,
2930
forwardRef,
3031
isValidElement,
3132
lazy,

packages/react/src/React.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
useDeferredValue,
5656
useId,
5757
useCacheRefresh,
58+
use,
5859
} from './ReactHooks';
5960
import {
6061
createElementWithValidation,
@@ -127,6 +128,7 @@ export {
127128
getCacheForType as unstable_getCacheForType,
128129
useCacheRefresh as unstable_useCacheRefresh,
129130
REACT_CACHE_TYPE as unstable_Cache,
131+
use as experimental_use,
130132
// enableScopeAPI
131133
REACT_SCOPE_TYPE as unstable_Scope,
132134
// enableTransitionTracing

packages/react/src/ReactHooks.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
MutableSourceSubscribeFn,
1515
ReactContext,
1616
StartTransitionOptions,
17+
Usable,
1718
} from 'shared/ReactTypes';
1819

1920
import ReactCurrentDispatcher from './ReactCurrentDispatcher';
@@ -204,3 +205,9 @@ export function useCacheRefresh(): <T>(?() => T, ?T) => void {
204205
// $FlowFixMe This is unstable, thus optional
205206
return dispatcher.useCacheRefresh();
206207
}
208+
209+
export function use<T>(usable: Usable<T>): T {
210+
const dispatcher = resolveDispatcher();
211+
// $FlowFixMe This is unstable, thus optional
212+
return dispatcher.use(usable);
213+
}

packages/shared/ReactFeatureFlags.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export const enableCPUSuspense = __EXPERIMENTAL__;
117117
export const deletedTreeCleanUpLevel = 3;
118118

119119
export const enableFloat = __EXPERIMENTAL__;
120+
export const enableUseHook = __EXPERIMENTAL__;
120121

121122
// -----------------------------------------------------------------------------
122123
// Chopping Block

packages/shared/ReactTypes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,6 @@ export type OffscreenMode =
212212
export type StartTransitionOptions = {
213213
name?: string,
214214
};
215+
216+
// TODO: Add Context support
217+
export type Usable<T> = Thenable<T>;

0 commit comments

Comments
 (0)