Skip to content

Commit 57b7597

Browse files
committed
gate servercontext
1 parent bbd52a4 commit 57b7597

16 files changed

Lines changed: 313 additions & 243 deletions

packages/react-client/src/__tests__/ReactFlight-test.js

Lines changed: 186 additions & 173 deletions
Large diffs are not rendered by default.

packages/react-is/src/ReactIs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import {
1313
REACT_CONTEXT_TYPE,
14+
REACT_SERVER_CONTEXT_TYPE,
1415
REACT_ELEMENT_TYPE,
1516
REACT_FORWARD_REF_TYPE,
1617
REACT_FRAGMENT_TYPE,
@@ -43,6 +44,7 @@ export function typeOf(object: any) {
4344
const $$typeofType = type && type.$$typeof;
4445

4546
switch ($$typeofType) {
47+
case REACT_SERVER_CONTEXT_TYPE:
4648
case REACT_CONTEXT_TYPE:
4749
case REACT_FORWARD_REF_TYPE:
4850
case REACT_LAZY_TYPE:

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

Lines changed: 97 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
enableLazyContextPropagation,
3434
enableSuspenseLayoutEffectSemantics,
3535
enableUseMutableSource,
36+
enableServerContext,
3637
} from 'shared/ReactFeatureFlags';
3738

3839
import {
@@ -2372,12 +2373,33 @@ function getCacheForType<T>(resourceType: () => T): T {
23722373
return cacheForType;
23732374
}
23742375

2376+
function mountServerContext<T: ServerContextJSONValue>(
2377+
context: ReactServerContext<T>,
2378+
): T {
2379+
if (!enableServerContext) {
2380+
throw new Error('Not implemented.');
2381+
}
2382+
currentHookNameInDev = 'useServerContext';
2383+
mountHookTypesDev();
2384+
return readContext(context);
2385+
}
2386+
2387+
function updateServerContext<T: ServerContextJSONValue>(
2388+
context: ReactServerContext<T>,
2389+
): T {
2390+
if (!enableServerContext) {
2391+
throw new Error('Not implemented.');
2392+
}
2393+
currentHookNameInDev = 'useServerContext';
2394+
updateHookTypesDev();
2395+
return readContext(context);
2396+
}
2397+
23752398
export const ContextOnlyDispatcher: Dispatcher = {
23762399
readContext,
23772400

23782401
useCallback: throwInvalidHookError,
23792402
useContext: throwInvalidHookError,
2380-
useServerContext: throwInvalidHookError,
23812403
useEffect: throwInvalidHookError,
23822404
useImperativeHandle: throwInvalidHookError,
23832405
useInsertionEffect: throwInvalidHookError,
@@ -2401,12 +2423,15 @@ if (enableCache) {
24012423
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
24022424
}
24032425

2426+
if (enableServerContext) {
2427+
ContextOnlyDispatcher.useServerContext = throwInvalidHookError;
2428+
}
2429+
24042430
const HooksDispatcherOnMount: Dispatcher = {
24052431
readContext,
24062432

24072433
useCallback: mountCallback,
24082434
useContext: readContext,
2409-
useServerContext: readContext,
24102435
useEffect: mountEffect,
24112436
useImperativeHandle: mountImperativeHandle,
24122437
useLayoutEffect: mountLayoutEffect,
@@ -2429,13 +2454,15 @@ if (enableCache) {
24292454
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
24302455
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
24312456
}
2457+
if (enableServerContext) {
2458+
HooksDispatcherOnMount.useServerContext = readContext;
2459+
}
24322460

24332461
const HooksDispatcherOnUpdate: Dispatcher = {
24342462
readContext,
24352463

24362464
useCallback: updateCallback,
24372465
useContext: readContext,
2438-
useServerContext: readContext,
24392466
useEffect: updateEffect,
24402467
useImperativeHandle: updateImperativeHandle,
24412468
useInsertionEffect: updateInsertionEffect,
@@ -2458,13 +2485,15 @@ if (enableCache) {
24582485
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
24592486
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
24602487
}
2488+
if (enableServerContext) {
2489+
HooksDispatcherOnUpdate.useServerContext = readContext;
2490+
}
24612491

24622492
const HooksDispatcherOnRerender: Dispatcher = {
24632493
readContext,
24642494

24652495
useCallback: updateCallback,
24662496
useContext: readContext,
2467-
useServerContext: readContext,
24682497
useEffect: updateEffect,
24692498
useImperativeHandle: updateImperativeHandle,
24702499
useInsertionEffect: updateInsertionEffect,
@@ -2487,6 +2516,9 @@ if (enableCache) {
24872516
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
24882517
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
24892518
}
2519+
if (enableServerContext) {
2520+
HooksDispatcherOnRerender.useServerContext = readContext;
2521+
}
24902522

24912523
let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
24922524
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
@@ -2530,13 +2562,6 @@ if (__DEV__) {
25302562
mountHookTypesDev();
25312563
return readContext(context);
25322564
},
2533-
useServerContext<T: ServerContextJSONValue>(
2534-
context: ReactServerContext<T>,
2535-
): T {
2536-
currentHookNameInDev = 'useServerContext';
2537-
mountHookTypesDev();
2538-
return readContext(context);
2539-
},
25402565
useEffect(
25412566
create: () => (() => void) | void,
25422567
deps: Array<mixed> | void | null,
@@ -2669,6 +2694,9 @@ if (__DEV__) {
26692694
return mountRefresh();
26702695
};
26712696
}
2697+
if (enableServerContext) {
2698+
HooksDispatcherOnMountInDEV.useServerContext = mountServerContext;
2699+
}
26722700

26732701
HooksDispatcherOnMountWithHookTypesInDEV = {
26742702
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
@@ -2684,13 +2712,6 @@ if (__DEV__) {
26842712
updateHookTypesDev();
26852713
return readContext(context);
26862714
},
2687-
useServerContext<T: ServerContextJSONValue>(
2688-
context: ReactServerContext<T>,
2689-
): T {
2690-
currentHookNameInDev = 'useServerContext';
2691-
updateHookTypesDev();
2692-
return readContext(context);
2693-
},
26942715
useEffect(
26952716
create: () => (() => void) | void,
26962717
deps: Array<mixed> | void | null,
@@ -2818,6 +2839,9 @@ if (__DEV__) {
28182839
return mountRefresh();
28192840
};
28202841
}
2842+
if (enableServerContext) {
2843+
HooksDispatcherOnMountWithHookTypesInDEV.useServerContext = updateServerContext;
2844+
}
28212845

28222846
HooksDispatcherOnUpdateInDEV = {
28232847
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
@@ -2833,13 +2857,6 @@ if (__DEV__) {
28332857
updateHookTypesDev();
28342858
return readContext(context);
28352859
},
2836-
useServerContext<T: ServerContextJSONValue>(
2837-
context: ReactServerContext<T>,
2838-
): T {
2839-
currentHookNameInDev = 'useServerContext';
2840-
updateHookTypesDev();
2841-
return readContext(context);
2842-
},
28432860
useEffect(
28442861
create: () => (() => void) | void,
28452862
deps: Array<mixed> | void | null,
@@ -2967,6 +2984,12 @@ if (__DEV__) {
29672984
return updateRefresh();
29682985
};
29692986
}
2987+
if (enableServerContext) {
2988+
HooksDispatcherOnUpdateInDEV.useServerContext = updateServerContext;
2989+
}
2990+
2991+
if (!enableServerContext) {
2992+
}
29702993

29712994
HooksDispatcherOnRerenderInDEV = {
29722995
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
@@ -2983,13 +3006,6 @@ if (__DEV__) {
29833006
updateHookTypesDev();
29843007
return readContext(context);
29853008
},
2986-
useServerContext<T: ServerContextJSONValue>(
2987-
context: ReactServerContext<T>,
2988-
): T {
2989-
currentHookNameInDev = 'useServerContext';
2990-
updateHookTypesDev();
2991-
return readContext(context);
2992-
},
29933009
useEffect(
29943010
create: () => (() => void) | void,
29953011
deps: Array<mixed> | void | null,
@@ -3117,6 +3133,9 @@ if (__DEV__) {
31173133
return updateRefresh();
31183134
};
31193135
}
3136+
if (enableServerContext) {
3137+
HooksDispatcherOnRerenderInDEV.useServerContext = updateServerContext;
3138+
}
31203139

31213140
InvalidNestedHooksDispatcherOnMountInDEV = {
31223141
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
@@ -3135,14 +3154,6 @@ if (__DEV__) {
31353154
mountHookTypesDev();
31363155
return readContext(context);
31373156
},
3138-
useServerContext<T: ServerContextJSONValue>(
3139-
context: ReactServerContext<T>,
3140-
): T {
3141-
currentHookNameInDev = 'useServerContext';
3142-
warnInvalidHookAccess();
3143-
mountHookTypesDev();
3144-
return readContext(context);
3145-
},
31463157
useEffect(
31473158
create: () => (() => void) | void,
31483159
deps: Array<mixed> | void | null,
@@ -3285,6 +3296,22 @@ if (__DEV__) {
32853296
};
32863297
}
32873298

3299+
if (enableServerContext) {
3300+
InvalidNestedHooksDispatcherOnMountInDEV.useServerContext = <
3301+
T: ServerContextJSONValue,
3302+
>(
3303+
context: ReactServerContext<T>,
3304+
): T => {
3305+
if (!enableServerContext) {
3306+
throw new Error('Not implemented.');
3307+
}
3308+
currentHookNameInDev = 'useServerContext';
3309+
warnInvalidHookAccess();
3310+
mountHookTypesDev();
3311+
return readContext(context);
3312+
};
3313+
}
3314+
32883315
InvalidNestedHooksDispatcherOnUpdateInDEV = {
32893316
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
32903317
warnInvalidContextAccess();
@@ -3302,14 +3329,6 @@ if (__DEV__) {
33023329
updateHookTypesDev();
33033330
return readContext(context);
33043331
},
3305-
useServerContext<T: ServerContextJSONValue>(
3306-
context: ReactServerContext<T>,
3307-
): T {
3308-
currentHookNameInDev = 'useServerContext';
3309-
warnInvalidHookAccess();
3310-
updateHookTypesDev();
3311-
return readContext(context);
3312-
},
33133332
useEffect(
33143333
create: () => (() => void) | void,
33153334
deps: Array<mixed> | void | null,
@@ -3451,6 +3470,21 @@ if (__DEV__) {
34513470
return updateRefresh();
34523471
};
34533472
}
3473+
if (enableServerContext) {
3474+
InvalidNestedHooksDispatcherOnUpdateInDEV.useServerContext = <
3475+
T: ServerContextJSONValue,
3476+
>(
3477+
context: ReactServerContext<T>,
3478+
): T => {
3479+
if (!enableServerContext) {
3480+
throw new Error('Not implemented.');
3481+
}
3482+
currentHookNameInDev = 'useServerContext';
3483+
warnInvalidHookAccess();
3484+
updateHookTypesDev();
3485+
return readContext(context);
3486+
};
3487+
}
34543488

34553489
InvalidNestedHooksDispatcherOnRerenderInDEV = {
34563490
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
@@ -3470,14 +3504,6 @@ if (__DEV__) {
34703504
updateHookTypesDev();
34713505
return readContext(context);
34723506
},
3473-
useServerContext<T: ServerContextJSONValue>(
3474-
context: ReactServerContext<T>,
3475-
): T {
3476-
currentHookNameInDev = 'useServerContext';
3477-
warnInvalidHookAccess();
3478-
updateHookTypesDev();
3479-
return readContext(context);
3480-
},
34813507
useEffect(
34823508
create: () => (() => void) | void,
34833509
deps: Array<mixed> | void | null,
@@ -3619,4 +3645,19 @@ if (__DEV__) {
36193645
return updateRefresh();
36203646
};
36213647
}
3648+
if (enableServerContext) {
3649+
InvalidNestedHooksDispatcherOnRerenderInDEV.useServerContext = <
3650+
T: ServerContextJSONValue,
3651+
>(
3652+
context: ReactServerContext<T>,
3653+
): T => {
3654+
if (!enableServerContext) {
3655+
throw new Error('Not implemented.');
3656+
}
3657+
currentHookNameInDev = 'useServerContext';
3658+
warnInvalidHookAccess();
3659+
updateHookTypesDev();
3660+
return readContext(context);
3661+
};
3662+
}
36223663
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,16 @@ export function pushProvider<T: any>(
129129
}
130130
}
131131

132-
export function popProvider<T: any>(
133-
context: ReactContext<T> | ReactServerContext<T>,
132+
export function popProvider(
133+
context: ReactContext<any> | ReactServerContext<any>,
134134
providerFiber: Fiber,
135135
): void {
136136
const currentValue = valueCursor.current;
137137
pop(valueCursor, providerFiber);
138138
if (isPrimaryRenderer) {
139-
context._currentValue = (currentValue: any);
139+
context._currentValue = currentValue;
140140
} else {
141-
context._currentValue2 = (currentValue: any);
141+
context._currentValue2 = currentValue;
142142
}
143143
}
144144

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ export type Dispatcher = {|
323323
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
324324
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
325325
): Snapshot,
326-
useServerContext<T: ServerContextJSONValue>(
326+
useServerContext?: <T: ServerContextJSONValue>(
327327
context: ReactServerContext<T>,
328-
): T,
328+
) => T,
329329
useSyncExternalStore<T>(
330330
subscribe: (() => void) => () => void,
331331
getSnapshot: () => T,

packages/react-server/src/ReactFlightServer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ import {
5959
REACT_LAZY_TYPE,
6060
REACT_MEMO_TYPE,
6161
REACT_PROVIDER_TYPE,
62-
REACT_CONTEXT_TYPE,
6362
REACT_SERVER_CONTEXT_TYPE,
6463
} from 'shared/ReactSymbols';
6564

0 commit comments

Comments
 (0)