Skip to content

Commit

Permalink
gate servercontext
Browse files Browse the repository at this point in the history
  • Loading branch information
salazarm committed Feb 14, 2022
1 parent bbd52a4 commit 57b7597
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 243 deletions.
359 changes: 186 additions & 173 deletions packages/react-client/src/__tests__/ReactFlight-test.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/react-is/src/ReactIs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import {
REACT_CONTEXT_TYPE,
REACT_SERVER_CONTEXT_TYPE,
REACT_ELEMENT_TYPE,
REACT_FORWARD_REF_TYPE,
REACT_FRAGMENT_TYPE,
Expand Down Expand Up @@ -43,6 +44,7 @@ export function typeOf(object: any) {
const $$typeofType = type && type.$$typeof;

switch ($$typeofType) {
case REACT_SERVER_CONTEXT_TYPE:
case REACT_CONTEXT_TYPE:
case REACT_FORWARD_REF_TYPE:
case REACT_LAZY_TYPE:
Expand Down
153 changes: 97 additions & 56 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
enableLazyContextPropagation,
enableSuspenseLayoutEffectSemantics,
enableUseMutableSource,
enableServerContext,
} from 'shared/ReactFeatureFlags';

import {
Expand Down Expand Up @@ -2372,12 +2373,33 @@ function getCacheForType<T>(resourceType: () => T): T {
return cacheForType;
}

function mountServerContext<T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T {
if (!enableServerContext) {
throw new Error('Not implemented.');
}
currentHookNameInDev = 'useServerContext';
mountHookTypesDev();
return readContext(context);
}

function updateServerContext<T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T {
if (!enableServerContext) {
throw new Error('Not implemented.');
}
currentHookNameInDev = 'useServerContext';
updateHookTypesDev();
return readContext(context);
}

export const ContextOnlyDispatcher: Dispatcher = {
readContext,

useCallback: throwInvalidHookError,
useContext: throwInvalidHookError,
useServerContext: throwInvalidHookError,
useEffect: throwInvalidHookError,
useImperativeHandle: throwInvalidHookError,
useInsertionEffect: throwInvalidHookError,
Expand All @@ -2401,12 +2423,15 @@ if (enableCache) {
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
}

if (enableServerContext) {
ContextOnlyDispatcher.useServerContext = throwInvalidHookError;
}

const HooksDispatcherOnMount: Dispatcher = {
readContext,

useCallback: mountCallback,
useContext: readContext,
useServerContext: readContext,
useEffect: mountEffect,
useImperativeHandle: mountImperativeHandle,
useLayoutEffect: mountLayoutEffect,
Expand All @@ -2429,13 +2454,15 @@ if (enableCache) {
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
}
if (enableServerContext) {
HooksDispatcherOnMount.useServerContext = readContext;
}

const HooksDispatcherOnUpdate: Dispatcher = {
readContext,

useCallback: updateCallback,
useContext: readContext,
useServerContext: readContext,
useEffect: updateEffect,
useImperativeHandle: updateImperativeHandle,
useInsertionEffect: updateInsertionEffect,
Expand All @@ -2458,13 +2485,15 @@ if (enableCache) {
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableServerContext) {
HooksDispatcherOnUpdate.useServerContext = readContext;
}

const HooksDispatcherOnRerender: Dispatcher = {
readContext,

useCallback: updateCallback,
useContext: readContext,
useServerContext: readContext,
useEffect: updateEffect,
useImperativeHandle: updateImperativeHandle,
useInsertionEffect: updateInsertionEffect,
Expand All @@ -2487,6 +2516,9 @@ if (enableCache) {
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
}
if (enableServerContext) {
HooksDispatcherOnRerender.useServerContext = readContext;
}

let HooksDispatcherOnMountInDEV: Dispatcher | null = null;
let HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher | null = null;
Expand Down Expand Up @@ -2530,13 +2562,6 @@ if (__DEV__) {
mountHookTypesDev();
return readContext(context);
},
useServerContext<T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T {
currentHookNameInDev = 'useServerContext';
mountHookTypesDev();
return readContext(context);
},
useEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
Expand Down Expand Up @@ -2669,6 +2694,9 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableServerContext) {
HooksDispatcherOnMountInDEV.useServerContext = mountServerContext;
}

HooksDispatcherOnMountWithHookTypesInDEV = {
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
Expand All @@ -2684,13 +2712,6 @@ if (__DEV__) {
updateHookTypesDev();
return readContext(context);
},
useServerContext<T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T {
currentHookNameInDev = 'useServerContext';
updateHookTypesDev();
return readContext(context);
},
useEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
Expand Down Expand Up @@ -2818,6 +2839,9 @@ if (__DEV__) {
return mountRefresh();
};
}
if (enableServerContext) {
HooksDispatcherOnMountWithHookTypesInDEV.useServerContext = updateServerContext;
}

HooksDispatcherOnUpdateInDEV = {
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
Expand All @@ -2833,13 +2857,6 @@ if (__DEV__) {
updateHookTypesDev();
return readContext(context);
},
useServerContext<T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T {
currentHookNameInDev = 'useServerContext';
updateHookTypesDev();
return readContext(context);
},
useEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
Expand Down Expand Up @@ -2967,6 +2984,12 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableServerContext) {
HooksDispatcherOnUpdateInDEV.useServerContext = updateServerContext;
}

if (!enableServerContext) {
}

HooksDispatcherOnRerenderInDEV = {
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
Expand All @@ -2983,13 +3006,6 @@ if (__DEV__) {
updateHookTypesDev();
return readContext(context);
},
useServerContext<T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T {
currentHookNameInDev = 'useServerContext';
updateHookTypesDev();
return readContext(context);
},
useEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
Expand Down Expand Up @@ -3117,6 +3133,9 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableServerContext) {
HooksDispatcherOnRerenderInDEV.useServerContext = updateServerContext;
}

InvalidNestedHooksDispatcherOnMountInDEV = {
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
Expand All @@ -3135,14 +3154,6 @@ if (__DEV__) {
mountHookTypesDev();
return readContext(context);
},
useServerContext<T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T {
currentHookNameInDev = 'useServerContext';
warnInvalidHookAccess();
mountHookTypesDev();
return readContext(context);
},
useEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
Expand Down Expand Up @@ -3285,6 +3296,22 @@ if (__DEV__) {
};
}

if (enableServerContext) {
InvalidNestedHooksDispatcherOnMountInDEV.useServerContext = <
T: ServerContextJSONValue,
>(
context: ReactServerContext<T>,
): T => {
if (!enableServerContext) {
throw new Error('Not implemented.');
}
currentHookNameInDev = 'useServerContext';
warnInvalidHookAccess();
mountHookTypesDev();
return readContext(context);
};
}

InvalidNestedHooksDispatcherOnUpdateInDEV = {
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
warnInvalidContextAccess();
Expand All @@ -3302,14 +3329,6 @@ if (__DEV__) {
updateHookTypesDev();
return readContext(context);
},
useServerContext<T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T {
currentHookNameInDev = 'useServerContext';
warnInvalidHookAccess();
updateHookTypesDev();
return readContext(context);
},
useEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
Expand Down Expand Up @@ -3451,6 +3470,21 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableServerContext) {
InvalidNestedHooksDispatcherOnUpdateInDEV.useServerContext = <
T: ServerContextJSONValue,
>(
context: ReactServerContext<T>,
): T => {
if (!enableServerContext) {
throw new Error('Not implemented.');
}
currentHookNameInDev = 'useServerContext';
warnInvalidHookAccess();
updateHookTypesDev();
return readContext(context);
};
}

InvalidNestedHooksDispatcherOnRerenderInDEV = {
readContext<T: any>(context: ReactContext<T> | ReactServerContext<T>): T {
Expand All @@ -3470,14 +3504,6 @@ if (__DEV__) {
updateHookTypesDev();
return readContext(context);
},
useServerContext<T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T {
currentHookNameInDev = 'useServerContext';
warnInvalidHookAccess();
updateHookTypesDev();
return readContext(context);
},
useEffect(
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
Expand Down Expand Up @@ -3619,4 +3645,19 @@ if (__DEV__) {
return updateRefresh();
};
}
if (enableServerContext) {
InvalidNestedHooksDispatcherOnRerenderInDEV.useServerContext = <
T: ServerContextJSONValue,
>(
context: ReactServerContext<T>,
): T => {
if (!enableServerContext) {
throw new Error('Not implemented.');
}
currentHookNameInDev = 'useServerContext';
warnInvalidHookAccess();
updateHookTypesDev();
return readContext(context);
};
}
}
8 changes: 4 additions & 4 deletions packages/react-reconciler/src/ReactFiberNewContext.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ export function pushProvider<T: any>(
}
}

export function popProvider<T: any>(
context: ReactContext<T> | ReactServerContext<T>,
export function popProvider(
context: ReactContext<any> | ReactServerContext<any>,
providerFiber: Fiber,
): void {
const currentValue = valueCursor.current;
pop(valueCursor, providerFiber);
if (isPrimaryRenderer) {
context._currentValue = (currentValue: any);
context._currentValue = currentValue;
} else {
context._currentValue2 = (currentValue: any);
context._currentValue2 = currentValue;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/react-reconciler/src/ReactInternalTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ export type Dispatcher = {|
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
): Snapshot,
useServerContext<T: ServerContextJSONValue>(
useServerContext?: <T: ServerContextJSONValue>(
context: ReactServerContext<T>,
): T,
) => T,
useSyncExternalStore<T>(
subscribe: (() => void) => () => void,
getSnapshot: () => T,
Expand Down
1 change: 0 additions & 1 deletion packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import {
REACT_LAZY_TYPE,
REACT_MEMO_TYPE,
REACT_PROVIDER_TYPE,
REACT_CONTEXT_TYPE,
REACT_SERVER_CONTEXT_TYPE,
} from 'shared/ReactSymbols';

Expand Down
Loading

0 comments on commit 57b7597

Please sign in to comment.