Skip to content

Commit 3a2a112

Browse files
author
Brian Vaughn
committed
Fixed high pri updates erasing pending lower pri subscription updates
1 parent 94faec2 commit 3a2a112

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,12 @@ function useMutableSourceImpl<S>(
900900
isSafeToReadFromSource =
901901
pendingExpirationTime === NoWork ||
902902
pendingExpirationTime >= expirationTime;
903+
904+
if (!isSafeToReadFromSource) {
905+
// Preserve the pending update if the current priority doesn't include it.
906+
currentlyRenderingFiber.expirationTime = pendingExpirationTime;
907+
markUnprocessedUpdateTime(pendingExpirationTime);
908+
}
903909
}
904910

905911
let prevMemoizedState = ((hook.memoizedState: any): ?MutableSourceState<S>);

packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.internal.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,6 @@ describe('ReactHooksWithNoopRenderer', () => {
20882088
() => Scheduler.unstable_yieldValue('Sync effect'),
20892089
);
20902090
expect(Scheduler).toFlushAndYield(['a:one', 'b:one', 'Sync effect']);
2091-
ReactNoop.flushPassiveEffects();
20922091

20932092
// Changing values should schedule an update with React.
20942093
// Start working on this update but don't finish it.
@@ -2108,9 +2107,7 @@ describe('ReactHooksWithNoopRenderer', () => {
21082107
});
21092108
expect(Scheduler).toHaveYielded(['a:one', 'b:one', 'Sync effect']);
21102109

2111-
// TODO (useMutableSource) Re-enable the assertion below; it fails now for reasons unknown.
2112-
// Once the update is processed, the new value should be used
2113-
// expect(Scheduler).toFlushAndYield(['a:two', 'b:two']);
2110+
expect(Scheduler).toFlushAndYield(['a:two', 'b:two']);
21142111
});
21152112

21162113
it('should read from source on newly mounted subtree if no pending updates are scheduled for source', () => {
@@ -2170,6 +2167,12 @@ describe('ReactHooksWithNoopRenderer', () => {
21702167
});
21712168
expect(Scheduler).toHaveYielded(['a:two', 'b:two', 'Sync effect']);
21722169
});
2170+
2171+
// TODO (useMutableSource) Test case for a scoped subscription,
2172+
// followed by a render that reads from a different part of the store,
2173+
// with a mutation between its first and second read,
2174+
// that isn't picked up on by the scoped subscription,
2175+
// to verify that we also use the version number to protect against this case.
21732176
});
21742177

21752178
describe('useCallback', () => {

0 commit comments

Comments
 (0)