GeezerVison is showing github.com — Add back useMutableSource temporarily (#22396) · react/react@82c8fa9
File treeExpand file treeCollapse file tree Open diff view settings react-suspense-test-utils/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/ReactDebugHooks.js +25-1Lines changed: 25 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| 10 | | -import type {ReactContext, ReactProviderType} from 'shared/ReactTypes'; | | 10 | +import type { | | 11 | + MutableSource, | | 12 | + MutableSourceGetSnapshotFn, | | 13 | + MutableSourceSubscribeFn, | | 14 | + ReactContext, | | 15 | + ReactProviderType, | | 16 | +} from 'shared/ReactTypes'; | 11 | 17 | import type { | 12 | 18 | Fiber, | 13 | 19 | Dispatcher as DispatcherType, | @@ -255,6 +261,23 @@ function useMemo<T>( | 255 | 261 | return value; | 256 | 262 | } | 257 | 263 |
| | 264 | +function useMutableSource<Source, Snapshot>( | | 265 | + source: MutableSource<Source>, | | 266 | + getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>, | | 267 | + subscribe: MutableSourceSubscribeFn<Source, Snapshot>, | | 268 | +): Snapshot { | | 269 | + // useMutableSource() composes multiple hooks internally. | | 270 | + // Advance the current hook index the same number of times | | 271 | + // so that subsequent hooks have the right memoized state. | | 272 | + nextHook(); // MutableSource | | 273 | + nextHook(); // State | | 274 | + nextHook(); // Effect | | 275 | + nextHook(); // Effect | | 276 | + const value = getSnapshot(source._source); | | 277 | + hookLog.push({primitive: 'MutableSource', stackError: new Error(), value}); | | 278 | + return value; | | 279 | +} | | 280 | + | 258 | 281 | function useSyncExternalStore<T>( | 259 | 282 | subscribe: (() => void) => () => void, | 260 | 283 | getSnapshot: () => T, | @@ -335,6 +358,7 @@ const Dispatcher: DispatcherType = { | 335 | 358 | useRef, | 336 | 359 | useState, | 337 | 360 | useTransition, | | 361 | + useMutableSource, | 338 | 362 | useSyncExternalStore, | 339 | 363 | useDeferredValue, | 340 | 364 | useOpaqueIdentifier, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1019,6 +1019,43 @@ describe('ReactHooksInspectionIntegration', () => { | 1019 | 1019 | ]); | 1020 | 1020 | }); | 1021 | 1021 |
| | 1022 | + it('should support composite useMutableSource hook', () => { | | 1023 | + const createMutableSource = | | 1024 | + React.createMutableSource || React.unstable_createMutableSource; | | 1025 | + const useMutableSource = | | 1026 | + React.useMutableSource || React.unstable_useMutableSource; | | 1027 | + | | 1028 | + const mutableSource = createMutableSource({}, () => 1); | | 1029 | + function Foo(props) { | | 1030 | + useMutableSource( | | 1031 | + mutableSource, | | 1032 | + () => 'snapshot', | | 1033 | + () => {}, | | 1034 | + ); | | 1035 | + React.useMemo(() => 'memo', []); | | 1036 | + return <div />; | | 1037 | + } | | 1038 | + const renderer = ReactTestRenderer.create(<Foo />); | | 1039 | + const childFiber = renderer.root.findByType(Foo)._currentFiber(); | | 1040 | + const tree = ReactDebugTools.inspectHooksOfFiber(childFiber); | | 1041 | + expect(tree).toEqual([ | | 1042 | + { | | 1043 | + id: 0, | | 1044 | + isStateEditable: false, | | 1045 | + name: 'MutableSource', | | 1046 | + value: 'snapshot', | | 1047 | + subHooks: [], | | 1048 | + }, | | 1049 | + { | | 1050 | + id: 1, | | 1051 | + isStateEditable: false, | | 1052 | + name: 'Memo', | | 1053 | + value: 'memo', | | 1054 | + subHooks: [], | | 1055 | + }, | | 1056 | + ]); | | 1057 | + }); | | 1058 | + | 1022 | 1059 | // @gate experimental || www | 1023 | 1060 | it('should support composite useSyncExternalStore hook', () => { | 1024 | 1061 | const useSyncExternalStore = React.unstable_useSyncExternalStore; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMRoot.js +26-1Lines changed: 26 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | */ | 9 | 9 |
| 10 | 10 | import type {Container} from './ReactDOMHostConfig'; | 11 | | -import type {ReactNodeList} from 'shared/ReactTypes'; | | 11 | +import type {MutableSource, ReactNodeList} from 'shared/ReactTypes'; | 12 | 12 | import type {FiberRoot} from 'react-reconciler/src/ReactInternalTypes'; | 13 | 13 |
| 14 | 14 | export type RootType = { | @@ -24,6 +24,7 @@ export type CreateRootOptions = { | 24 | 24 | hydrationOptions?: { | 25 | 25 | onHydrated?: (suspenseNode: Comment) => void, | 26 | 26 | onDeleted?: (suspenseNode: Comment) => void, | | 27 | + mutableSources?: Array<MutableSource<any>>, | 27 | 28 | ... | 28 | 29 | }, | 29 | 30 | // END OF TODO | @@ -34,6 +35,7 @@ export type CreateRootOptions = { | 34 | 35 |
| 35 | 36 | export type HydrateRootOptions = { | 36 | 37 | // Hydration options | | 38 | + hydratedSources?: Array<MutableSource<any>>, | 37 | 39 | onHydrated?: (suspenseNode: Comment) => void, | 38 | 40 | onDeleted?: (suspenseNode: Comment) => void, | 39 | 41 | // Options for all roots | @@ -59,6 +61,7 @@ import { | 59 | 61 | createContainer, | 60 | 62 | updateContainer, | 61 | 63 | findHostInstanceWithNoPortals, | | 64 | + registerMutableSourceForHydration, | 62 | 65 | } from 'react-reconciler/src/ReactFiberReconciler'; | 63 | 66 | import invariant from 'shared/invariant'; | 64 | 67 | import {ConcurrentRoot} from 'react-reconciler/src/ReactRootTags'; | @@ -126,6 +129,11 @@ export function createRoot( | 126 | 129 | const hydrate = options != null && options.hydrate === true; | 127 | 130 | const hydrationCallbacks = | 128 | 131 | (options != null && options.hydrationOptions) || null; | | 132 | + const mutableSources = | | 133 | + (options != null && | | 134 | + options.hydrationOptions != null && | | 135 | + options.hydrationOptions.mutableSources) || | | 136 | + null; | 129 | 137 | // END TODO | 130 | 138 |
| 131 | 139 | const isStrictMode = options != null && options.unstable_strictMode === true; | @@ -151,6 +159,15 @@ export function createRoot( | 151 | 159 | container.nodeType === COMMENT_NODE ? container.parentNode : container; | 152 | 160 | listenToAllSupportedEvents(rootContainerElement); | 153 | 161 |
| | 162 | + // TODO: Delete this path | | 163 | + if (mutableSources) { | | 164 | + for (let i = 0; i < mutableSources.length; i++) { | | 165 | + const mutableSource = mutableSources[i]; | | 166 | + registerMutableSourceForHydration(root, mutableSource); | | 167 | + } | | 168 | + } | | 169 | + // END TODO | | 170 | + | 154 | 171 | return new ReactDOMRoot(root); | 155 | 172 | } | 156 | 173 |
| @@ -168,6 +185,7 @@ export function hydrateRoot( | 168 | 185 | // For now we reuse the whole bag of options since they contain | 169 | 186 | // the hydration callbacks. | 170 | 187 | const hydrationCallbacks = options != null ? options : null; | | 188 | + const mutableSources = (options != null && options.hydratedSources) || null; | 171 | 189 | const isStrictMode = options != null && options.unstable_strictMode === true; | 172 | 190 |
| 173 | 191 | let concurrentUpdatesByDefaultOverride = null; | @@ -190,6 +208,13 @@ export function hydrateRoot( | 190 | 208 | // This can't be a comment node since hydration doesn't work on comment nodes anyway. | 191 | 209 | listenToAllSupportedEvents(container); | 192 | 210 |
| | 211 | + if (mutableSources) { | | 212 | + for (let i = 0; i < mutableSources.length; i++) { | | 213 | + const mutableSource = mutableSources[i]; | | 214 | + registerMutableSourceForHydration(root, mutableSource); | | 215 | + } | | 216 | + } | | 217 | + | 193 | 218 | // Render the initial children | 194 | 219 | updateContainer(initialChildren, root, null, null); | 195 | 220 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/server/ReactPartialRendererHooks.js +20-1Lines changed: 20 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | import type {Dispatcher as DispatcherType} from 'react-reconciler/src/ReactInternalTypes'; | 11 | 11 |
| 12 | | -import type {ReactContext} from 'shared/ReactTypes'; | | 12 | +import type { | | 13 | + MutableSource, | | 14 | + MutableSourceGetSnapshotFn, | | 15 | + MutableSourceSubscribeFn, | | 16 | + ReactContext, | | 17 | +} from 'shared/ReactTypes'; | 13 | 18 | import type PartialRenderer from './ReactPartialRenderer'; | 14 | 19 |
| 15 | 20 | import {validateContextBounds} from './ReactPartialRendererContext'; | @@ -461,6 +466,18 @@ export function useCallback<T>( | 461 | 466 | return useMemo(() => callback, deps); | 462 | 467 | } | 463 | 468 |
| | 469 | +// TODO Decide on how to implement this hook for server rendering. | | 470 | +// If a mutation occurs during render, consider triggering a Suspense boundary | | 471 | +// and falling back to client rendering. | | 472 | +function useMutableSource<Source, Snapshot>( | | 473 | + source: MutableSource<Source>, | | 474 | + getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>, | | 475 | + subscribe: MutableSourceSubscribeFn<Source, Snapshot>, | | 476 | +): Snapshot { | | 477 | + resolveCurrentlyRenderingComponent(); | | 478 | + return getSnapshot(source._source); | | 479 | +} | | 480 | + | 464 | 481 | function useSyncExternalStore<T>( | 465 | 482 | subscribe: (() => void) => () => void, | 466 | 483 | getSnapshot: () => T, | @@ -527,6 +544,8 @@ export const Dispatcher: DispatcherType = { | 527 | 544 | useDeferredValue, | 528 | 545 | useTransition, | 529 | 546 | useOpaqueIdentifier, | | 547 | + // Subscriptions are not setup in a server environment. | | 548 | + useMutableSource, | 530 | 549 | useSyncExternalStore, | 531 | 550 | }; | 532 | 551 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.new.js +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import type {LazyComponent as LazyComponentType} from 'react/src/ReactLazy'; | 12 | 12 | import type {Fiber, FiberRoot} from './ReactInternalTypes'; | 13 | 13 | import type {TypeOfMode} from './ReactTypeOfMode'; | 14 | 14 | import type {Lanes, Lane} from './ReactFiberLane.new'; | | 15 | +import type {MutableSource} from 'shared/ReactTypes'; | 15 | 16 | import type { | 16 | 17 | SuspenseState, | 17 | 18 | SuspenseListRenderState, | @@ -144,6 +145,7 @@ import { | 144 | 145 | isSuspenseInstancePending, | 145 | 146 | isSuspenseInstanceFallback, | 146 | 147 | registerSuspenseInstanceRetry, | | 148 | + supportsHydration, | 147 | 149 | isPrimaryRenderer, | 148 | 150 | supportsPersistence, | 149 | 151 | getOffscreenContainerProps, | @@ -218,6 +220,7 @@ import { | 218 | 220 | RetryAfterError, | 219 | 221 | NoContext, | 220 | 222 | } from './ReactFiberWorkLoop.new'; | | 223 | +import {setWorkInProgressVersion} from './ReactMutableSource.new'; | 221 | 224 | import { | 222 | 225 | requestCacheFromPool, | 223 | 226 | pushCacheProvider, | @@ -1297,6 +1300,21 @@ function updateHostRoot(current, workInProgress, renderLanes) { | 1297 | 1300 | // We always try to hydrate. If this isn't a hydration pass there won't | 1298 | 1301 | // be any children to hydrate which is effectively the same thing as | 1299 | 1302 | // not hydrating. | | 1303 | + | | 1304 | + if (supportsHydration) { | | 1305 | + const mutableSourceEagerHydrationData = | | 1306 | + root.mutableSourceEagerHydrationData; | | 1307 | + if (mutableSourceEagerHydrationData != null) { | | 1308 | + for (let i = 0; i < mutableSourceEagerHydrationData.length; i += 2) { | | 1309 | + const mutableSource = ((mutableSourceEagerHydrationData[ | | 1310 | + i | | 1311 | + ]: any): MutableSource<any>); | | 1312 | + const version = mutableSourceEagerHydrationData[i + 1]; | | 1313 | + setWorkInProgressVersion(mutableSource, version); | | 1314 | + } | | 1315 | + } | | 1316 | + } | | 1317 | + | 1300 | 1318 | const child = mountChildFibers( | 1301 | 1319 | workInProgress, | 1302 | 1320 | null, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberBeginWork.old.js +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import type {LazyComponent as LazyComponentType} from 'react/src/ReactLazy'; | 12 | 12 | import type {Fiber, FiberRoot} from './ReactInternalTypes'; | 13 | 13 | import type {TypeOfMode} from './ReactTypeOfMode'; | 14 | 14 | import type {Lanes, Lane} from './ReactFiberLane.old'; | | 15 | +import type {MutableSource} from 'shared/ReactTypes'; | 15 | 16 | import type { | 16 | 17 | SuspenseState, | 17 | 18 | SuspenseListRenderState, | @@ -144,6 +145,7 @@ import { | 144 | 145 | isSuspenseInstancePending, | 145 | 146 | isSuspenseInstanceFallback, | 146 | 147 | registerSuspenseInstanceRetry, | | 148 | + supportsHydration, | 147 | 149 | isPrimaryRenderer, | 148 | 150 | supportsPersistence, | 149 | 151 | getOffscreenContainerProps, | @@ -218,6 +220,7 @@ import { | 218 | 220 | RetryAfterError, | 219 | 221 | NoContext, | 220 | 222 | } from './ReactFiberWorkLoop.old'; | | 223 | +import {setWorkInProgressVersion} from './ReactMutableSource.old'; | 221 | 224 | import { | 222 | 225 | requestCacheFromPool, | 223 | 226 | pushCacheProvider, | @@ -1297,6 +1300,21 @@ function updateHostRoot(current, workInProgress, renderLanes) { | 1297 | 1300 | // We always try to hydrate. If this isn't a hydration pass there won't | 1298 | 1301 | // be any children to hydrate which is effectively the same thing as | 1299 | 1302 | // not hydrating. | | 1303 | + | | 1304 | + if (supportsHydration) { | | 1305 | + const mutableSourceEagerHydrationData = | | 1306 | + root.mutableSourceEagerHydrationData; | | 1307 | + if (mutableSourceEagerHydrationData != null) { | | 1308 | + for (let i = 0; i < mutableSourceEagerHydrationData.length; i += 2) { | | 1309 | + const mutableSource = ((mutableSourceEagerHydrationData[ | | 1310 | + i | | 1311 | + ]: any): MutableSource<any>); | | 1312 | + const version = mutableSourceEagerHydrationData[i + 1]; | | 1313 | + setWorkInProgressVersion(mutableSource, version); | | 1314 | + } | | 1315 | + } | | 1316 | + } | | 1317 | + | 1300 | 1318 | const child = mountChildFibers( | 1301 | 1319 | workInProgress, | 1302 | 1320 | null, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCompleteWork.new.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,8 @@ import type {SuspenseContext} from './ReactFiberSuspenseContext.new'; | 30 | 30 | import type {OffscreenState} from './ReactFiberOffscreenComponent'; | 31 | 31 | import type {Cache, SpawnedCachePool} from './ReactFiberCacheComponent.new'; | 32 | 32 |
| | 33 | +import {resetWorkInProgressVersions as resetMutableSourceWorkInProgressVersions} from './ReactMutableSource.new'; | | 34 | + | 33 | 35 | import {now} from './Scheduler'; | 34 | 36 |
| 35 | 37 | import { | @@ -852,6 +854,7 @@ function completeWork( | 852 | 854 | } | 853 | 855 | popHostContainer(workInProgress); | 854 | 856 | popTopLevelLegacyContextObject(workInProgress); | | 857 | + resetMutableSourceWorkInProgressVersions(); | 855 | 858 | if (fiberRoot.pendingContext) { | 856 | 859 | fiberRoot.context = fiberRoot.pendingContext; | 857 | 860 | fiberRoot.pendingContext = null; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCompleteWork.old.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,8 @@ import type {SuspenseContext} from './ReactFiberSuspenseContext.old'; | 30 | 30 | import type {OffscreenState} from './ReactFiberOffscreenComponent'; | 31 | 31 | import type {Cache, SpawnedCachePool} from './ReactFiberCacheComponent.old'; | 32 | 32 |
| | 33 | +import {resetWorkInProgressVersions as resetMutableSourceWorkInProgressVersions} from './ReactMutableSource.old'; | | 34 | + | 33 | 35 | import {now} from './Scheduler'; | 34 | 36 |
| 35 | 37 | import { | @@ -852,6 +854,7 @@ function completeWork( | 852 | 854 | } | 853 | 855 | popHostContainer(workInProgress); | 854 | 856 | popTopLevelLegacyContextObject(workInProgress); | | 857 | + resetMutableSourceWorkInProgressVersions(); | 855 | 858 | if (fiberRoot.pendingContext) { | 856 | 859 | fiberRoot.context = fiberRoot.pendingContext; | 857 | 860 | fiberRoot.pendingContext = null; | |
|