-
Notifications
You must be signed in to change notification settings - Fork 92
Fix/Ensure the new selector execution #735
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
Open
leshniak
wants to merge
7
commits into
Expensify:main
Choose a base branch
from
callstack-internal:fix/selector-on-dependency-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+84
−10
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
415b4d4
add a test case for a selector on the dependency list
leshniak 75f9bd1
fixes memoizedSelector reference
leshniak b9e24a2
allow to recompute the sleected value if selector changes
leshniak cad13f2
properly expose hasComputed flag
leshniak b6319e0
update unit test for sync/async selector change
leshniak 11c8b3a
implements epoch approach for tracking selector freshness
leshniak 3ac96c9
Merge branch 'main' of github.com:callstack-internal/react-native-ony…
leshniak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import {useCallback, useRef} from 'react'; | ||
| import usePrevious from './usePrevious'; | ||
|
|
||
| type UseSelectorEpochResult = { | ||
| hasSelectorComputedForCurrentEpoch: boolean; | ||
| markSelectorComputedForCurrentEpoch: () => void; | ||
| }; | ||
|
|
||
| /** | ||
| * Tracks selector freshness across async interleavings using generation epochs. | ||
| * | ||
| * Why: | ||
| * - Selector reference can change while external-store callbacks are still in flight. | ||
| * - Snapshot cache may otherwise return a value computed by an older selector generation. | ||
| * | ||
| * How: | ||
| * - Increment epoch whenever selector reference changes. | ||
| * - Mark epoch as computed only after caller evaluates selector for current snapshot input. | ||
| * - Expose a boolean that tells caller whether current selector generation has already been computed. | ||
| * | ||
| * Usage pattern: | ||
| * 1) If `hasSelectorComputedForCurrentEpoch` is false, bypass cache and recompute. | ||
| * 2) After recompute, call `markSelectorComputedForCurrentEpoch()`. | ||
| */ | ||
| function useSelectorEpoch<TSelector>(selector: TSelector | null): UseSelectorEpochResult { | ||
| const selectorEpochRef = useRef(0); | ||
| const computedSelectorEpochRef = useRef(-1); | ||
| const previousSelector = usePrevious(selector); | ||
|
|
||
| if (previousSelector !== selector) { | ||
| selectorEpochRef.current += 1; | ||
| } | ||
|
|
||
| const markSelectorComputedForCurrentEpoch = useCallback(() => { | ||
| computedSelectorEpochRef.current = selectorEpochRef.current; | ||
| }, []); | ||
|
|
||
| return { | ||
| hasSelectorComputedForCurrentEpoch: !selector || computedSelectorEpochRef.current === selectorEpochRef.current, | ||
| markSelectorComputedForCurrentEpoch, | ||
| }; | ||
| } | ||
|
|
||
| export default useSelectorEpoch; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you think of a way to add a failing test for this first? seems like we had this bug in place because this scenario was not covered by unit tests. For onyx I believe we should try to always create the failing test first and then work on the fix cc @fabioh8010
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can see that there is an update in unit tests within this PR. Adding
act()makes its behavior closer to how React works in the browser, like in few other tests here, breaking the scenario (while it shouldn't). The test in its previous form was too loose.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I saw that change, but it was not clear to me if that is all that we could / should cover here. Could you create a specific test for this behaviour that was broken?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I'll do it.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mountiny I've updated the tests and also dug deeper into the root cause.
On the main branch
rerender()is called synchronously, so updates scheduled asynchonously withprepareSubscriberUpdate()don't have time to run. As a result, the store subscriber callbackdoes not get a chance to toggle the
isFirstConnectionandshouldGetCachedValueflags, which allows the new callback to execute.On the other hand, if we call the next
useOnyxin an asynchronous workflow, those flags are toggled tofalse, effectively preventing the new selector from executing.The updated unit test ensures that replacing the selector works predictably in both scenarios.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice thanks for digging into that