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

Re-land #22292 (Remove useMutableSource from open source build) #22664

Merged
merged 1 commit into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ describe('ReactHooksInspectionIntegration', () => {
]);
});

// @gate enableUseMutableSource
it('should support composite useMutableSource hook', () => {
const createMutableSource =
React.createMutableSource || React.unstable_createMutableSource;
Expand Down
13 changes: 13 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
enableStrictEffects,
enableLazyContextPropagation,
enableSuspenseLayoutEffectSemantics,
enableUseMutableSource,
} from 'shared/ReactFeatureFlags';

import {
Expand Down Expand Up @@ -1052,6 +1053,10 @@ function useMutableSource<Source, Snapshot>(
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
): Snapshot {
if (!enableUseMutableSource) {
return (undefined: any);
}

const root = ((getWorkInProgressRoot(): any): FiberRoot);

if (root === null) {
Expand Down Expand Up @@ -1213,6 +1218,10 @@ function mountMutableSource<Source, Snapshot>(
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
): Snapshot {
if (!enableUseMutableSource) {
return (undefined: any);
}

const hook = mountWorkInProgressHook();
hook.memoizedState = ({
refs: {
Expand All @@ -1230,6 +1239,10 @@ function updateMutableSource<Source, Snapshot>(
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
): Snapshot {
if (!enableUseMutableSource) {
return (undefined: any);
}

const hook = updateWorkInProgressHook();
return useMutableSource(hook, source, getSnapshot, subscribe);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
enableStrictEffects,
enableLazyContextPropagation,
enableSuspenseLayoutEffectSemantics,
enableUseMutableSource,
} from 'shared/ReactFeatureFlags';

import {
Expand Down Expand Up @@ -1052,6 +1053,10 @@ function useMutableSource<Source, Snapshot>(
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
): Snapshot {
if (!enableUseMutableSource) {
return (undefined: any);
}

const root = ((getWorkInProgressRoot(): any): FiberRoot);

if (root === null) {
Expand Down Expand Up @@ -1213,6 +1218,10 @@ function mountMutableSource<Source, Snapshot>(
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
): Snapshot {
if (!enableUseMutableSource) {
return (undefined: any);
}

const hook = mountWorkInProgressHook();
hook.memoizedState = ({
refs: {
Expand All @@ -1230,6 +1239,10 @@ function updateMutableSource<Source, Snapshot>(
getSnapshot: MutableSourceGetSnapshotFn<Source, Snapshot>,
subscribe: MutableSourceSubscribeFn<Source, Snapshot>,
): Snapshot {
if (!enableUseMutableSource) {
return (undefined: any);
}

const hook = updateWorkInProgressHook();
return useMutableSource(hook, source, getSnapshot, subscribe);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe('useMutableSource', () => {

beforeEach(loadModules);

// @gate enableUseMutableSource
it('should subscribe to a source and schedule updates when it changes', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -208,6 +209,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should restart work if a new source is mutated during render', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -263,6 +265,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should schedule an update if a new source is mutated between render and commit (subscription)', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -302,6 +305,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should unsubscribe and resubscribe if a new source is used', () => {
const sourceA = createSource('a-one');
const mutableSourceA = createMutableSource(
Expand Down Expand Up @@ -358,6 +362,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should unsubscribe and resubscribe if a new subscribe function is provided', () => {
const source = createSource('a-one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -422,6 +427,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should re-use previously read snapshot value when reading is unsafe', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -484,6 +490,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should read from source on newly mounted subtree if no pending updates are scheduled for source', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -523,6 +530,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should throw and restart render if source and snapshot are unavailable during an update', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -586,6 +594,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should throw and restart render if source and snapshot are unavailable during a sync update', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -649,6 +658,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should only update components whose subscriptions fire', () => {
const source = createComplexSource('a:one', 'b:one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -687,6 +697,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should detect tearing in part of the store not yet subscribed to', () => {
const source = createComplexSource('a:one', 'b:one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -779,6 +790,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('does not schedule an update for subscriptions that fire with an unchanged snapshot', () => {
const MockComponent = jest.fn(Component);

Expand All @@ -805,6 +817,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should throw and restart if getSnapshot changes between scheduled update and re-render', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -845,6 +858,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should recover from a mutation during yield when other work is scheduled', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -899,6 +913,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should not throw if the new getSnapshot returns the same snapshot value', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -953,6 +968,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should not throw if getSnapshot changes but the source can be safely read from anyway', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -992,6 +1008,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should still schedule an update if an eager selector throws after a mutation', () => {
const source = createSource({
friends: [
Expand Down Expand Up @@ -1058,6 +1075,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should not warn about updates that fire between unmount and passive unsubscribe', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -1094,6 +1112,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should support inline selectors and updates that are processed after selector change', async () => {
const source = createSource({
a: 'initial',
Expand Down Expand Up @@ -1138,6 +1157,7 @@ describe('useMutableSource', () => {
expect(root).toMatchRenderedOutput('Another update');
});

// @gate enableUseMutableSource
it('should clear the update queue when getSnapshot changes with pending lower priority updates', async () => {
const source = createSource({
a: 'initial',
Expand Down Expand Up @@ -1194,6 +1214,7 @@ describe('useMutableSource', () => {
expect(root).toMatchRenderedOutput('B: Update');
});

// @gate enableUseMutableSource
it('should clear the update queue when source changes with pending lower priority updates', async () => {
const sourceA = createSource('initial');
const sourceB = createSource('initial');
Expand Down Expand Up @@ -1238,6 +1259,7 @@ describe('useMutableSource', () => {
expect(root).toMatchRenderedOutput('B: Update');
});

// @gate enableUseMutableSource
it('should always treat reading as potentially unsafe when getSnapshot changes between renders', async () => {
const source = createSource({
a: 'foo',
Expand Down Expand Up @@ -1327,6 +1349,7 @@ describe('useMutableSource', () => {
expect(Scheduler).toHaveYielded(['x: bar, y: bar']);
});

// @gate enableUseMutableSource
it('getSnapshot changes and then source is mutated in between paint and passive effect phase', async () => {
const source = createSource({
a: 'foo',
Expand Down Expand Up @@ -1385,6 +1408,7 @@ describe('useMutableSource', () => {
expect(root).toMatchRenderedOutput('baz');
});

// @gate enableUseMutableSource
it('getSnapshot changes and then source is mutated in between paint and passive effect phase, case 2', async () => {
const source = createSource({
a: 'a0',
Expand Down Expand Up @@ -1455,6 +1479,7 @@ describe('useMutableSource', () => {
expect(root.getChildrenAsJSX()).toEqual('first: a1, second: a1');
});

// @gate enableUseMutableSource
it(
'if source is mutated after initial read but before subscription is set ' +
'up, should still entangle all pending mutations even if snapshot of ' +
Expand Down Expand Up @@ -1559,6 +1584,7 @@ describe('useMutableSource', () => {
},
);

// @gate enableUseMutableSource
it('warns about functions being used as snapshot values', async () => {
const source = createSource(() => 'a');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -1586,6 +1612,7 @@ describe('useMutableSource', () => {
expect(root).toMatchRenderedOutput('a');
});

// @gate enableUseMutableSource
it('getSnapshot changes and then source is mutated during interleaved event', async () => {
const {useEffect} = React;

Expand Down Expand Up @@ -1710,6 +1737,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should not tear with newly mounted component when updates were scheduled at a lower priority', async () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -1789,6 +1817,7 @@ describe('useMutableSource', () => {

if (__DEV__) {
describe('dev warnings', () => {
// @gate enableUseMutableSource
it('should warn if the subscribe function does not return an unsubscribe function', () => {
const source = createSource('one');
const mutableSource = createMutableSource(
Expand All @@ -1814,6 +1843,7 @@ describe('useMutableSource', () => {
);
});

// @gate enableUseMutableSource
it('should error if multiple renderers of the same type use a mutable source at the same time', () => {
const source = createSource('one');
const mutableSource = createMutableSource(
Expand Down Expand Up @@ -1894,6 +1924,7 @@ describe('useMutableSource', () => {
});
});

// @gate enableUseMutableSource
it('should error if multiple renderers of the same type use a mutable source at the same time with mutation between', () => {
const source = createSource('one');
const mutableSource = createMutableSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ describe('useMutableSourceHydration', () => {
return <div>{`${label}:${snapshot}`}</div>;
}

// @gate enableUseMutableSource
it('should render and hydrate', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -180,6 +181,7 @@ describe('useMutableSourceHydration', () => {
expect(source.listenerCount).toBe(1);
});

// @gate enableUseMutableSource
it('should detect a tear before hydrating a component', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -224,6 +226,7 @@ describe('useMutableSourceHydration', () => {
expect(source.listenerCount).toBe(1);
});

// @gate enableUseMutableSource
it('should detect a tear between hydrating components', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -282,6 +285,7 @@ describe('useMutableSourceHydration', () => {
expect(source.listenerCount).toBe(2);
});

// @gate enableUseMutableSource
it('should detect a tear between hydrating components reading from different parts of a source', () => {
const source = createComplexSource('a:one', 'b:one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down Expand Up @@ -371,6 +375,7 @@ describe('useMutableSourceHydration', () => {
});

// @gate !enableSyncDefaultUpdates
// @gate enableUseMutableSource
it('should detect a tear during a higher priority interruption', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source, param => param.version);
Expand Down
1 change: 0 additions & 1 deletion packages/react/index.experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export {
useInsertionEffect,
useLayoutEffect,
useMemo,
useMutableSource as unstable_useMutableSource,
useSyncExternalStore,
useReducer,
useRef,
Expand Down
1 change: 0 additions & 1 deletion packages/react/index.stable.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export {
useInsertionEffect,
useLayoutEffect,
useMemo,
useMutableSource as unstable_useMutableSource,
useSyncExternalStore,
useReducer,
useRef,
Expand Down
Loading