Skip to content

Commit

Permalink
React.createGlobalState()
Browse files Browse the repository at this point in the history
Implements a special version of context whose default value (the value
given to provider that are rendered outside a consumer) can be updated.
  • Loading branch information
acdlite committed Jul 27, 2018
1 parent 2a2ef7e commit 1b8dab2
Show file tree
Hide file tree
Showing 13 changed files with 668 additions and 12 deletions.
14 changes: 9 additions & 5 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
readContext,
prepareToReadContext,
calculateChangedBits,
pushRootContexts,
} from './ReactFiberNewContext';
import {
markActualRenderTimeStarted,
Expand Down Expand Up @@ -412,7 +413,7 @@ function finishClassComponent(
return workInProgress.child;
}

function pushHostRootContext(workInProgress) {
function pushHostRootContext(workInProgress, renderExpirationTime) {
const root = (workInProgress.stateNode: FiberRoot);
if (root.pendingContext) {
pushTopLevelContextObject(
Expand All @@ -425,10 +426,10 @@ function pushHostRootContext(workInProgress) {
pushTopLevelContextObject(workInProgress, root.context, false);
}
pushHostContainer(workInProgress, root.containerInfo);
pushRootContexts(workInProgress, renderExpirationTime);
}

function updateHostRoot(current, workInProgress, renderExpirationTime) {
pushHostRootContext(workInProgress);
const updateQueue = workInProgress.updateQueue;
invariant(
updateQueue !== null,
Expand All @@ -438,14 +439,17 @@ function updateHostRoot(current, workInProgress, renderExpirationTime) {
);
const nextProps = workInProgress.pendingProps;
const prevState = workInProgress.memoizedState;
const prevChildren = prevState !== null ? prevState.element : null;
const prevChildren = prevState.element;
processUpdateQueue(
workInProgress,
updateQueue,
nextProps,
null,
renderExpirationTime,
);
// This must come *after* processing the update queue, in case any contexts
// were updated.
pushHostRootContext(workInProgress, renderExpirationTime);
const nextState = workInProgress.memoizedState;
// Caution: React DevTools currently depends on this property
// being called "element".
Expand Down Expand Up @@ -826,7 +830,7 @@ function updateContextProvider(current, workInProgress, renderExpirationTime) {
changedBits = MAX_SIGNED_31_BIT_INT;
} else {
const oldValue = oldProps.value;
changedBits = calculateChangedBits(context, newValue, oldValue);
changedBits = calculateChangedBits(context, oldValue, newValue);
if (changedBits === 0) {
// No change. Bailout early if children are the same.
if (
Expand Down Expand Up @@ -980,7 +984,7 @@ function beginWork(
// in this optimized path, mostly pushing stuff onto the stack.
switch (workInProgress.tag) {
case HostRoot:
pushHostRootContext(workInProgress);
pushHostRootContext(workInProgress, renderExpirationTime);
resetHydrationState();
break;
case HostComponent:
Expand Down
3 changes: 2 additions & 1 deletion packages/react-reconciler/src/ReactFiberCompleteWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import {
popContextProvider as popLegacyContextProvider,
popTopLevelContextObject as popTopLevelLegacyContextObject,
} from './ReactFiberContext';
import {popProvider} from './ReactFiberNewContext';
import {popProvider, popRootContexts} from './ReactFiberNewContext';
import {
prepareToHydrateHostInstance,
prepareToHydrateHostTextInstance,
Expand Down Expand Up @@ -325,6 +325,7 @@ function completeWork(
case HostRoot: {
popHostContainer(workInProgress);
popTopLevelLegacyContextObject(workInProgress);
popRootContexts(workInProgress);
const fiberRoot = (workInProgress.stateNode: FiberRoot);
if (fiberRoot.pendingContext) {
fiberRoot.context = fiberRoot.pendingContext;
Expand Down
Loading

0 comments on commit 1b8dab2

Please sign in to comment.