Skip to content

Commit

Permalink
Merge rerenderSES and updateSES again
Browse files Browse the repository at this point in the history
This reverts commit 1920123 and does a little cleanup.
  • Loading branch information
sophiebits committed May 12, 2023
1 parent 1920123 commit ce51243
Showing 1 changed file with 6 additions and 87 deletions.
93 changes: 6 additions & 87 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ function mountSyncExternalStore<T>(
return nextSnapshot;
}

function rerenderSyncExternalStore<T>(
function updateSyncExternalStore<T>(
subscribe: (() => void) => () => void,
getSnapshot: () => T,
getServerSnapshot?: () => T,
Expand Down Expand Up @@ -1824,7 +1824,7 @@ function rerenderSyncExternalStore<T>(
}
let snapshotChanged = true;
if (currentHook !== null) {
const prevSnapshot = currentHook.memoizedState;
const prevSnapshot = hook.memoizedState;
snapshotChanged = !is(prevSnapshot, nextSnapshot);
if (snapshotChanged) {
hook.memoizedState = nextSnapshot;
Expand All @@ -1842,81 +1842,8 @@ function rerenderSyncExternalStore<T>(
// this can happen all the time, but even in synchronous mode, an earlier
// effect may have mutated the store.
if (
inst.getSnapshot !== getSnapshot ||
snapshotChanged ||
// Check if the subscribe function changed. We can save some memory by
// checking whether we scheduled a subscription effect above.
(workInProgressHook !== null &&
workInProgressHook.memoizedState.tag & HookHasEffect)
) {
fiber.flags |= PassiveEffect;
pushEffect(
HookHasEffect | HookPassive,
updateStoreInstance.bind(null, fiber, inst, nextSnapshot, getSnapshot),
createEffectInstance(),
null,
);

// Unless we're rendering a blocking lane, schedule a consistency check.
// Right before committing, we will walk the tree and check if any of the
// stores were mutated.
const root: FiberRoot | null = getWorkInProgressRoot();

if (root === null) {
throw new Error(
'Expected a work-in-progress root. This is a bug in React. Please file an issue.',
);
}

if (!isHydrating && !includesBlockingLane(root, renderLanes)) {
pushStoreConsistencyCheck(fiber, getSnapshot, nextSnapshot);
}
}

return nextSnapshot;
}

function updateSyncExternalStore<T>(
subscribe: (() => void) => () => void,
getSnapshot: () => T,
getServerSnapshot?: () => T,
): T {
const fiber = currentlyRenderingFiber;
const hook = updateWorkInProgressHook();
// Read the current snapshot from the store on every render. This breaks the
// normal rules of React, and only works because store updates are
// always synchronous.
const nextSnapshot = getSnapshot();
if (__DEV__) {
if (!didWarnUncachedGetSnapshot) {
const cachedSnapshot = getSnapshot();
if (!is(nextSnapshot, cachedSnapshot)) {
console.error(
'The result of getSnapshot should be cached to avoid an infinite loop',
);
didWarnUncachedGetSnapshot = true;
}
}
}
const prevSnapshot = ((currentHook: any): Hook).memoizedState;
const snapshotChanged = !is(prevSnapshot, nextSnapshot);
if (snapshotChanged) {
hook.memoizedState = nextSnapshot;
markWorkInProgressReceivedUpdate();
}
const inst = hook.queue;

updateEffect(subscribeToStore.bind(null, fiber, inst, subscribe), [
subscribe,
]);

// Whenever getSnapshot or subscribe changes, we need to check in the
// commit phase if there was an interleaved mutation. In concurrent mode
// this can happen all the time, but even in synchronous mode, an earlier
// effect may have mutated the store.
if (
inst.getSnapshot !== getSnapshot ||
snapshotChanged ||
// Check if the subscribe function changed. We can save some memory by
// checking whether we scheduled a subscription effect above.
(workInProgressHook !== null &&
Expand All @@ -1941,7 +1868,7 @@ function updateSyncExternalStore<T>(
);
}

if (!includesBlockingLane(root, renderLanes)) {
if (!isHydrating && !includesBlockingLane(root, renderLanes)) {
pushStoreConsistencyCheck(fiber, getSnapshot, nextSnapshot);
}
}
Expand Down Expand Up @@ -3392,7 +3319,7 @@ const HooksDispatcherOnRerender: Dispatcher = {
useDeferredValue: rerenderDeferredValue,
useTransition: rerenderTransition,
useMutableSource: updateMutableSource,
useSyncExternalStore: rerenderSyncExternalStore,
useSyncExternalStore: updateSyncExternalStore,
useId: updateId,
};
if (enableCache) {
Expand Down Expand Up @@ -4079,11 +4006,7 @@ if (__DEV__) {
): T {
currentHookNameInDev = 'useSyncExternalStore';
updateHookTypesDev();
return rerenderSyncExternalStore(
subscribe,
getSnapshot,
getServerSnapshot,
);
return updateSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
},
useId(): string {
currentHookNameInDev = 'useId';
Expand Down Expand Up @@ -4664,11 +4587,7 @@ if (__DEV__) {
currentHookNameInDev = 'useSyncExternalStore';
warnInvalidHookAccess();
updateHookTypesDev();
return rerenderSyncExternalStore(
subscribe,
getSnapshot,
getServerSnapshot,
);
return updateSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
},
useId(): string {
currentHookNameInDev = 'useId';
Expand Down

0 comments on commit ce51243

Please sign in to comment.