Skip to content

Commit ce51243

Browse files
committed
Merge rerenderSES and updateSES again
This reverts commit 1920123 and does a little cleanup.
1 parent 1920123 commit ce51243

File tree

1 file changed

+6
-87
lines changed

1 file changed

+6
-87
lines changed

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 6 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ function mountSyncExternalStore<T>(
17871787
return nextSnapshot;
17881788
}
17891789

1790-
function rerenderSyncExternalStore<T>(
1790+
function updateSyncExternalStore<T>(
17911791
subscribe: (() => void) => () => void,
17921792
getSnapshot: () => T,
17931793
getServerSnapshot?: () => T,
@@ -1824,7 +1824,7 @@ function rerenderSyncExternalStore<T>(
18241824
}
18251825
let snapshotChanged = true;
18261826
if (currentHook !== null) {
1827-
const prevSnapshot = currentHook.memoizedState;
1827+
const prevSnapshot = hook.memoizedState;
18281828
snapshotChanged = !is(prevSnapshot, nextSnapshot);
18291829
if (snapshotChanged) {
18301830
hook.memoizedState = nextSnapshot;
@@ -1842,81 +1842,8 @@ function rerenderSyncExternalStore<T>(
18421842
// this can happen all the time, but even in synchronous mode, an earlier
18431843
// effect may have mutated the store.
18441844
if (
1845-
inst.getSnapshot !== getSnapshot ||
18461845
snapshotChanged ||
1847-
// Check if the subscribe function changed. We can save some memory by
1848-
// checking whether we scheduled a subscription effect above.
1849-
(workInProgressHook !== null &&
1850-
workInProgressHook.memoizedState.tag & HookHasEffect)
1851-
) {
1852-
fiber.flags |= PassiveEffect;
1853-
pushEffect(
1854-
HookHasEffect | HookPassive,
1855-
updateStoreInstance.bind(null, fiber, inst, nextSnapshot, getSnapshot),
1856-
createEffectInstance(),
1857-
null,
1858-
);
1859-
1860-
// Unless we're rendering a blocking lane, schedule a consistency check.
1861-
// Right before committing, we will walk the tree and check if any of the
1862-
// stores were mutated.
1863-
const root: FiberRoot | null = getWorkInProgressRoot();
1864-
1865-
if (root === null) {
1866-
throw new Error(
1867-
'Expected a work-in-progress root. This is a bug in React. Please file an issue.',
1868-
);
1869-
}
1870-
1871-
if (!isHydrating && !includesBlockingLane(root, renderLanes)) {
1872-
pushStoreConsistencyCheck(fiber, getSnapshot, nextSnapshot);
1873-
}
1874-
}
1875-
1876-
return nextSnapshot;
1877-
}
1878-
1879-
function updateSyncExternalStore<T>(
1880-
subscribe: (() => void) => () => void,
1881-
getSnapshot: () => T,
1882-
getServerSnapshot?: () => T,
1883-
): T {
1884-
const fiber = currentlyRenderingFiber;
1885-
const hook = updateWorkInProgressHook();
1886-
// Read the current snapshot from the store on every render. This breaks the
1887-
// normal rules of React, and only works because store updates are
1888-
// always synchronous.
1889-
const nextSnapshot = getSnapshot();
1890-
if (__DEV__) {
1891-
if (!didWarnUncachedGetSnapshot) {
1892-
const cachedSnapshot = getSnapshot();
1893-
if (!is(nextSnapshot, cachedSnapshot)) {
1894-
console.error(
1895-
'The result of getSnapshot should be cached to avoid an infinite loop',
1896-
);
1897-
didWarnUncachedGetSnapshot = true;
1898-
}
1899-
}
1900-
}
1901-
const prevSnapshot = ((currentHook: any): Hook).memoizedState;
1902-
const snapshotChanged = !is(prevSnapshot, nextSnapshot);
1903-
if (snapshotChanged) {
1904-
hook.memoizedState = nextSnapshot;
1905-
markWorkInProgressReceivedUpdate();
1906-
}
1907-
const inst = hook.queue;
1908-
1909-
updateEffect(subscribeToStore.bind(null, fiber, inst, subscribe), [
1910-
subscribe,
1911-
]);
1912-
1913-
// Whenever getSnapshot or subscribe changes, we need to check in the
1914-
// commit phase if there was an interleaved mutation. In concurrent mode
1915-
// this can happen all the time, but even in synchronous mode, an earlier
1916-
// effect may have mutated the store.
1917-
if (
19181846
inst.getSnapshot !== getSnapshot ||
1919-
snapshotChanged ||
19201847
// Check if the subscribe function changed. We can save some memory by
19211848
// checking whether we scheduled a subscription effect above.
19221849
(workInProgressHook !== null &&
@@ -1941,7 +1868,7 @@ function updateSyncExternalStore<T>(
19411868
);
19421869
}
19431870

1944-
if (!includesBlockingLane(root, renderLanes)) {
1871+
if (!isHydrating && !includesBlockingLane(root, renderLanes)) {
19451872
pushStoreConsistencyCheck(fiber, getSnapshot, nextSnapshot);
19461873
}
19471874
}
@@ -3392,7 +3319,7 @@ const HooksDispatcherOnRerender: Dispatcher = {
33923319
useDeferredValue: rerenderDeferredValue,
33933320
useTransition: rerenderTransition,
33943321
useMutableSource: updateMutableSource,
3395-
useSyncExternalStore: rerenderSyncExternalStore,
3322+
useSyncExternalStore: updateSyncExternalStore,
33963323
useId: updateId,
33973324
};
33983325
if (enableCache) {
@@ -4079,11 +4006,7 @@ if (__DEV__) {
40794006
): T {
40804007
currentHookNameInDev = 'useSyncExternalStore';
40814008
updateHookTypesDev();
4082-
return rerenderSyncExternalStore(
4083-
subscribe,
4084-
getSnapshot,
4085-
getServerSnapshot,
4086-
);
4009+
return updateSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
40874010
},
40884011
useId(): string {
40894012
currentHookNameInDev = 'useId';
@@ -4664,11 +4587,7 @@ if (__DEV__) {
46644587
currentHookNameInDev = 'useSyncExternalStore';
46654588
warnInvalidHookAccess();
46664589
updateHookTypesDev();
4667-
return rerenderSyncExternalStore(
4668-
subscribe,
4669-
getSnapshot,
4670-
getServerSnapshot,
4671-
);
4590+
return updateSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
46724591
},
46734592
useId(): string {
46744593
currentHookNameInDev = 'useId';

0 commit comments

Comments
 (0)