Skip to content

Warn if MutableSource snapshot is a function #18933

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

Merged
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
20 changes: 19 additions & 1 deletion packages/react-reconciler/src/ReactFiberHooks.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,16 @@ function readFromUnsubcribedMutableSource<Source, Snapshot>(
}

if (isSafeToReadFromSource) {
return getSnapshot(source._source);
const snapshot = getSnapshot(source._source);
if (__DEV__) {
if (typeof snapshot === 'function') {
console.error(
'Mutable source should not return a function as the snapshot value. ' +
'Functions may close over mutable values and cause tearing.',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message probably isn't great. Happy to change it.

);
}
}
return snapshot;
} else {
// This handles the special case of a mutable source being shared beween renderers.
// In that case, if the source is mutated between the first and second renderer,
Expand Down Expand Up @@ -992,6 +1001,15 @@ function useMutableSource<Source, Snapshot>(
const maybeNewVersion = getVersion(source._source);
if (!is(version, maybeNewVersion)) {
const maybeNewSnapshot = getSnapshot(source._source);
if (__DEV__) {
if (typeof maybeNewSnapshot === 'function') {
console.error(
'Mutable source should not return a function as the snapshot value. ' +
'Functions may close over mutable values and cause tearing.',
);
}
}

if (!is(snapshot, maybeNewSnapshot)) {
setSnapshot(maybeNewSnapshot);

Expand Down
20 changes: 19 additions & 1 deletion packages/react-reconciler/src/ReactFiberHooks.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,16 @@ function readFromUnsubcribedMutableSource<Source, Snapshot>(
}

if (isSafeToReadFromSource) {
return getSnapshot(source._source);
const snapshot = getSnapshot(source._source);
if (__DEV__) {
if (typeof snapshot === 'function') {
console.error(
'Mutable source should not return a function as the snapshot value. ' +
'Functions may close over mutable values and cause tearing.',
);
}
}
return snapshot;
} else {
// This handles the special case of a mutable source being shared beween renderers.
// In that case, if the source is mutated between the first and second renderer,
Expand Down Expand Up @@ -978,6 +987,15 @@ function useMutableSource<Source, Snapshot>(
const maybeNewVersion = getVersion(source._source);
if (!is(version, maybeNewVersion)) {
const maybeNewSnapshot = getSnapshot(source._source);
if (__DEV__) {
if (typeof maybeNewSnapshot === 'function') {
console.error(
'Mutable source should not return a function as the snapshot value. ' +
'Functions may close over mutable values and cause tearing.',
);
}
}

if (!is(snapshot, maybeNewSnapshot)) {
setSnapshot(maybeNewSnapshot);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,34 @@ describe('useMutableSource', () => {
},
);

// @gate experimental
it('warns about functions being used as snapshot values', async () => {
const source = createSource(() => 'a');
const mutableSource = createMutableSource(source);

const getSnapshot = () => source.value;

function Read() {
const fn = useMutableSource(mutableSource, getSnapshot, defaultSubscribe);
const value = fn();
Scheduler.unstable_yieldValue(value);
return value;
}

const root = ReactNoop.createRoot();
await act(async () => {
root.render(
<>
<Read />
</>,
);
expect(() => expect(Scheduler).toFlushAndYield(['a'])).toErrorDev(
'Mutable source should not return a function as the snapshot value.',
);
});
expect(root).toMatchRenderedOutput('a');
});

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