Skip to content

Commit ed015b0

Browse files
committed
[WIP] Delete useMutableSource implementation
1 parent 0fd195f commit ed015b0

35 files changed

+14
-1362
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@
77
* @flow
88
*/
99

10-
import type {
11-
MutableSource,
12-
MutableSourceGetSnapshotFn,
13-
MutableSourceSubscribeFn,
14-
ReactContext,
15-
ReactProviderType,
16-
} from 'shared/ReactTypes';
10+
import type {ReactContext, ReactProviderType} from 'shared/ReactTypes';
1711
import type {
1812
Fiber,
1913
Dispatcher as DispatcherType,
@@ -248,23 +242,6 @@ function useMemo<T>(
248242
return value;
249243
}
250244

251-
function useMutableSource<Source, Snapshot>(
252-
source: MutableSource<Source>,
253-
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
254-
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
255-
): Snapshot {
256-
// useMutableSource() composes multiple hooks internally.
257-
// Advance the current hook index the same number of times
258-
// so that subsequent hooks have the right memoized state.
259-
nextHook(); // MutableSource
260-
nextHook(); // State
261-
nextHook(); // Effect
262-
nextHook(); // Effect
263-
const value = getSnapshot(source._source);
264-
hookLog.push({primitive: 'MutableSource', stackError: new Error(), value});
265-
return value;
266-
}
267-
268245
function useSyncExternalStore<T>(
269246
subscribe: (() => void) => () => void,
270247
getSnapshot: () => T,
@@ -344,7 +321,6 @@ const Dispatcher: DispatcherType = {
344321
useRef,
345322
useState,
346323
useTransition,
347-
useMutableSource,
348324
useSyncExternalStore,
349325
useDeferredValue,
350326
useOpaqueIdentifier,

packages/react-debug-tools/src/__tests__/ReactHooksInspectionIntegration-test.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -842,43 +842,6 @@ describe('ReactHooksInspectionIntegration', () => {
842842
]);
843843
});
844844

845-
it('should support composite useMutableSource hook', () => {
846-
const createMutableSource =
847-
React.createMutableSource || React.unstable_createMutableSource;
848-
const useMutableSource =
849-
React.useMutableSource || React.unstable_useMutableSource;
850-
851-
const mutableSource = createMutableSource({}, () => 1);
852-
function Foo(props) {
853-
useMutableSource(
854-
mutableSource,
855-
() => 'snapshot',
856-
() => {},
857-
);
858-
React.useMemo(() => 'memo', []);
859-
return <div />;
860-
}
861-
const renderer = ReactTestRenderer.create(<Foo />);
862-
const childFiber = renderer.root.findByType(Foo)._currentFiber();
863-
const tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
864-
expect(tree).toEqual([
865-
{
866-
id: 0,
867-
isStateEditable: false,
868-
name: 'MutableSource',
869-
value: 'snapshot',
870-
subHooks: [],
871-
},
872-
{
873-
id: 1,
874-
isStateEditable: false,
875-
name: 'Memo',
876-
value: 'memo',
877-
subHooks: [],
878-
},
879-
]);
880-
});
881-
882845
// @gate experimental || www
883846
it('should support composite useSyncExternalStore hook', () => {
884847
const useSyncExternalStore = React.unstable_useSyncExternalStore;

packages/react-dom/src/client/ReactDOMRoot.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import type {Container} from './ReactDOMHostConfig';
11-
import type {MutableSource, ReactNodeList} from 'shared/ReactTypes';
11+
import type {ReactNodeList} from 'shared/ReactTypes';
1212
import type {FiberRoot} from 'react-reconciler/src/ReactInternalTypes';
1313

1414
export type RootType = {
@@ -24,7 +24,6 @@ export type CreateRootOptions = {
2424
hydrationOptions?: {
2525
onHydrated?: (suspenseNode: Comment) => void,
2626
onDeleted?: (suspenseNode: Comment) => void,
27-
mutableSources?: Array<MutableSource<any>>,
2827
...
2928
},
3029
// END OF TODO
@@ -35,7 +34,6 @@ export type CreateRootOptions = {
3534

3635
export type HydrateRootOptions = {
3736
// Hydration options
38-
hydratedSources?: Array<MutableSource<any>>,
3937
onHydrated?: (suspenseNode: Comment) => void,
4038
onDeleted?: (suspenseNode: Comment) => void,
4139
// Options for all roots
@@ -61,7 +59,6 @@ import {
6159
createContainer,
6260
updateContainer,
6361
findHostInstanceWithNoPortals,
64-
registerMutableSourceForHydration,
6562
} from 'react-reconciler/src/ReactFiberReconciler';
6663
import invariant from 'shared/invariant';
6764
import {ConcurrentRoot} from 'react-reconciler/src/ReactRootTags';
@@ -129,11 +126,6 @@ export function createRoot(
129126
const hydrate = options != null && options.hydrate === true;
130127
const hydrationCallbacks =
131128
(options != null && options.hydrationOptions) || null;
132-
const mutableSources =
133-
(options != null &&
134-
options.hydrationOptions != null &&
135-
options.hydrationOptions.mutableSources) ||
136-
null;
137129
// END TODO
138130

139131
const isStrictMode = options != null && options.unstable_strictMode === true;
@@ -159,15 +151,6 @@ export function createRoot(
159151
container.nodeType === COMMENT_NODE ? container.parentNode : container;
160152
listenToAllSupportedEvents(rootContainerElement);
161153

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-
171154
return new ReactDOMRoot(root);
172155
}
173156

@@ -185,7 +168,6 @@ export function hydrateRoot(
185168
// For now we reuse the whole bag of options since they contain
186169
// the hydration callbacks.
187170
const hydrationCallbacks = options != null ? options : null;
188-
const mutableSources = (options != null && options.hydratedSources) || null;
189171
const isStrictMode = options != null && options.unstable_strictMode === true;
190172

191173
let concurrentUpdatesByDefaultOverride = null;
@@ -208,13 +190,6 @@ export function hydrateRoot(
208190
// This can't be a comment node since hydration doesn't work on comment nodes anyway.
209191
listenToAllSupportedEvents(container);
210192

211-
if (mutableSources) {
212-
for (let i = 0; i < mutableSources.length; i++) {
213-
const mutableSource = mutableSources[i];
214-
registerMutableSourceForHydration(root, mutableSource);
215-
}
216-
}
217-
218193
// Render the initial children
219194
updateContainer(initialChildren, root, null, null);
220195

packages/react-dom/src/server/ReactPartialRendererHooks.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99

1010
import type {Dispatcher as DispatcherType} from 'react-reconciler/src/ReactInternalTypes';
1111

12-
import type {
13-
MutableSource,
14-
MutableSourceGetSnapshotFn,
15-
MutableSourceSubscribeFn,
16-
ReactContext,
17-
} from 'shared/ReactTypes';
12+
import type {ReactContext} from 'shared/ReactTypes';
1813
import type PartialRenderer from './ReactPartialRenderer';
1914

2015
import {validateContextBounds} from './ReactPartialRendererContext';
@@ -450,18 +445,6 @@ export function useCallback<T>(
450445
return useMemo(() => callback, deps);
451446
}
452447

453-
// TODO Decide on how to implement this hook for server rendering.
454-
// If a mutation occurs during render, consider triggering a Suspense boundary
455-
// and falling back to client rendering.
456-
function useMutableSource<Source, Snapshot>(
457-
source: MutableSource<Source>,
458-
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
459-
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
460-
): Snapshot {
461-
resolveCurrentlyRenderingComponent();
462-
return getSnapshot(source._source);
463-
}
464-
465448
function useSyncExternalStore<T>(
466449
subscribe: (() => void) => () => void,
467450
getSnapshot: () => T,
@@ -519,8 +502,6 @@ export const Dispatcher: DispatcherType = {
519502
useDeferredValue,
520503
useTransition,
521504
useOpaqueIdentifier,
522-
// Subscriptions are not setup in a server environment.
523-
useMutableSource,
524505
useSyncExternalStore,
525506
};
526507

packages/react-reconciler/src/ReactFiberBeginWork.new.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {LazyComponent as LazyComponentType} from 'react/src/ReactLazy';
1212
import type {Fiber, FiberRoot} from './ReactInternalTypes';
1313
import type {TypeOfMode} from './ReactTypeOfMode';
1414
import type {Lanes, Lane} from './ReactFiberLane.new';
15-
import type {MutableSource} from 'shared/ReactTypes';
1615
import type {
1716
SuspenseState,
1817
SuspenseListRenderState,
@@ -145,7 +144,6 @@ import {
145144
isSuspenseInstancePending,
146145
isSuspenseInstanceFallback,
147146
registerSuspenseInstanceRetry,
148-
supportsHydration,
149147
isPrimaryRenderer,
150148
supportsPersistence,
151149
getOffscreenContainerProps,
@@ -224,7 +222,6 @@ import {
224222
RetryAfterError,
225223
NoContext,
226224
} from './ReactFiberWorkLoop.new';
227-
import {setWorkInProgressVersion} from './ReactMutableSource.new';
228225
import {
229226
requestCacheFromPool,
230227
pushCacheProvider,
@@ -1303,21 +1300,6 @@ function updateHostRoot(current, workInProgress, renderLanes) {
13031300
// We always try to hydrate. If this isn't a hydration pass there won't
13041301
// be any children to hydrate which is effectively the same thing as
13051302
// not hydrating.
1306-
1307-
if (supportsHydration) {
1308-
const mutableSourceEagerHydrationData =
1309-
root.mutableSourceEagerHydrationData;
1310-
if (mutableSourceEagerHydrationData != null) {
1311-
for (let i = 0; i < mutableSourceEagerHydrationData.length; i += 2) {
1312-
const mutableSource = ((mutableSourceEagerHydrationData[
1313-
i
1314-
]: any): MutableSource<any>);
1315-
const version = mutableSourceEagerHydrationData[i + 1];
1316-
setWorkInProgressVersion(mutableSource, version);
1317-
}
1318-
}
1319-
}
1320-
13211303
const child = mountChildFibers(
13221304
workInProgress,
13231305
null,

packages/react-reconciler/src/ReactFiberBeginWork.old.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {LazyComponent as LazyComponentType} from 'react/src/ReactLazy';
1212
import type {Fiber, FiberRoot} from './ReactInternalTypes';
1313
import type {TypeOfMode} from './ReactTypeOfMode';
1414
import type {Lanes, Lane} from './ReactFiberLane.old';
15-
import type {MutableSource} from 'shared/ReactTypes';
1615
import type {
1716
SuspenseState,
1817
SuspenseListRenderState,
@@ -145,7 +144,6 @@ import {
145144
isSuspenseInstancePending,
146145
isSuspenseInstanceFallback,
147146
registerSuspenseInstanceRetry,
148-
supportsHydration,
149147
isPrimaryRenderer,
150148
supportsPersistence,
151149
getOffscreenContainerProps,
@@ -224,7 +222,6 @@ import {
224222
RetryAfterError,
225223
NoContext,
226224
} from './ReactFiberWorkLoop.old';
227-
import {setWorkInProgressVersion} from './ReactMutableSource.old';
228225
import {
229226
requestCacheFromPool,
230227
pushCacheProvider,
@@ -1303,21 +1300,6 @@ function updateHostRoot(current, workInProgress, renderLanes) {
13031300
// We always try to hydrate. If this isn't a hydration pass there won't
13041301
// be any children to hydrate which is effectively the same thing as
13051302
// not hydrating.
1306-
1307-
if (supportsHydration) {
1308-
const mutableSourceEagerHydrationData =
1309-
root.mutableSourceEagerHydrationData;
1310-
if (mutableSourceEagerHydrationData != null) {
1311-
for (let i = 0; i < mutableSourceEagerHydrationData.length; i += 2) {
1312-
const mutableSource = ((mutableSourceEagerHydrationData[
1313-
i
1314-
]: any): MutableSource<any>);
1315-
const version = mutableSourceEagerHydrationData[i + 1];
1316-
setWorkInProgressVersion(mutableSource, version);
1317-
}
1318-
}
1319-
}
1320-
13211303
const child = mountChildFibers(
13221304
workInProgress,
13231305
null,

packages/react-reconciler/src/ReactFiberCompleteWork.new.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import type {SuspenseContext} from './ReactFiberSuspenseContext.new';
3030
import type {OffscreenState} from './ReactFiberOffscreenComponent';
3131
import type {Cache, SpawnedCachePool} from './ReactFiberCacheComponent.new';
3232

33-
import {resetWorkInProgressVersions as resetMutableSourceWorkInProgressVersions} from './ReactMutableSource.new';
34-
3533
import {now} from './Scheduler';
3634

3735
import {
@@ -854,7 +852,6 @@ function completeWork(
854852
}
855853
popHostContainer(workInProgress);
856854
popTopLevelLegacyContextObject(workInProgress);
857-
resetMutableSourceWorkInProgressVersions();
858855
if (fiberRoot.pendingContext) {
859856
fiberRoot.context = fiberRoot.pendingContext;
860857
fiberRoot.pendingContext = null;

packages/react-reconciler/src/ReactFiberCompleteWork.old.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import type {SuspenseContext} from './ReactFiberSuspenseContext.old';
3030
import type {OffscreenState} from './ReactFiberOffscreenComponent';
3131
import type {Cache, SpawnedCachePool} from './ReactFiberCacheComponent.old';
3232

33-
import {resetWorkInProgressVersions as resetMutableSourceWorkInProgressVersions} from './ReactMutableSource.old';
34-
3533
import {now} from './Scheduler';
3634

3735
import {
@@ -854,7 +852,6 @@ function completeWork(
854852
}
855853
popHostContainer(workInProgress);
856854
popTopLevelLegacyContextObject(workInProgress);
857-
resetMutableSourceWorkInProgressVersions();
858855
if (fiberRoot.pendingContext) {
859856
fiberRoot.context = fiberRoot.pendingContext;
860857
fiberRoot.pendingContext = null;

0 commit comments

Comments
 (0)