Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fork updateSyncExternalStore impl in update and rerender #25650

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1593,13 +1593,13 @@ function updateMutableSource<Source, Snapshot>(
return useMutableSource(hook, source, getSnapshot, subscribe);
}

function mountSyncExternalStore<T>(
function mountSyncExternalStoreImpl<T>(
hook: Hook,
subscribe: (() => void) => () => void,
getSnapshot: () => T,
getServerSnapshot?: () => T,
): T {
const fiber = currentlyRenderingFiber;
const hook = mountWorkInProgressHook();

let nextSnapshot;
const isHydrating = getIsHydrating();
Expand Down Expand Up @@ -1685,13 +1685,28 @@ function mountSyncExternalStore<T>(
return nextSnapshot;
}

function updateSyncExternalStore<T>(
function mountSyncExternalStore<T>(
subscribe: (() => void) => () => void,
getSnapshot: () => T,
getServerSnapshot?: () => T,
): T {
const hook = mountWorkInProgressHook();
return mountSyncExternalStoreImpl(
hook,
subscribe,
getSnapshot,
getServerSnapshot,
);
}

function updateSyncExternalStoreImpl<T>(
hook: Hook,
prevSnapshot: 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.
Expand All @@ -1707,7 +1722,6 @@ function updateSyncExternalStore<T>(
}
}
}
const prevSnapshot = (currentHook || hook).memoizedState;
const snapshotChanged = !is(prevSnapshot, nextSnapshot);
if (snapshotChanged) {
hook.memoizedState = nextSnapshot;
Expand Down Expand Up @@ -1758,6 +1772,49 @@ function updateSyncExternalStore<T>(
return nextSnapshot;
}

function updateSyncExternalStore<T>(
subscribe: (() => void) => () => void,
getSnapshot: () => T,
getServerSnapshot?: () => T,
): T {
const hook = updateWorkInProgressHook();
const prevSnapshot = hook.memoizedState;
return updateSyncExternalStoreImpl(
hook,
prevSnapshot,
subscribe,
getSnapshot,
getServerSnapshot,
);
}

function rerenderSyncExternalStore<T>(
subscribe: (() => void) => () => void,
getSnapshot: () => T,
getServerSnapshot?: () => T,
): T {
const hook = updateWorkInProgressHook();
if (currentHook === null) {
// This is a rerender during a mount.
return mountSyncExternalStoreImpl(
hook,
subscribe,
getSnapshot,
getServerSnapshot,
);
} else {
// This is a rerender during an update.
const prevSnapshot: T = currentHook.memoizedState;
return updateSyncExternalStoreImpl(
hook,
prevSnapshot,
subscribe,
getSnapshot,
getServerSnapshot,
);
}
}

function pushStoreConsistencyCheck<T>(
fiber: Fiber,
getSnapshot: () => T,
Expand Down Expand Up @@ -2894,7 +2951,7 @@ const HooksDispatcherOnRerender: Dispatcher = {
useDeferredValue: rerenderDeferredValue,
useTransition: rerenderTransition,
useMutableSource: updateMutableSource,
useSyncExternalStore: updateSyncExternalStore,
useSyncExternalStore: rerenderSyncExternalStore,
useId: updateId,
};
if (enableCache) {
Expand Down Expand Up @@ -3538,7 +3595,11 @@ if (__DEV__) {
): T {
currentHookNameInDev = 'useSyncExternalStore';
updateHookTypesDev();
return updateSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
return rerenderSyncExternalStore(
subscribe,
getSnapshot,
getServerSnapshot,
);
},
useId(): string {
currentHookNameInDev = 'useId';
Expand Down Expand Up @@ -4080,7 +4141,11 @@ if (__DEV__) {
currentHookNameInDev = 'useSyncExternalStore';
warnInvalidHookAccess();
updateHookTypesDev();
return updateSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
return rerenderSyncExternalStore(
subscribe,
getSnapshot,
getServerSnapshot,
);
},
useId(): string {
currentHookNameInDev = 'useId';
Expand Down