-
-
Notifications
You must be signed in to change notification settings - Fork 462
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
(react) - New MutableSource-like hooks implementation for suspense/concurrent #1335
Merged
Conversation
This file contains 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
🦋 Changeset detectedLatest commit: 46cbdf4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
JoviDeCroock
previously approved these changes
Jan 22, 2021
kitten
changed the title
WIP: (react) - New Suspense implementation
(react) - New MutableSource-like hooks implementation for suspense/concurrent
Jan 25, 2021
JoviDeCroock
approved these changes
Jan 26, 2021
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolve #1332
Resolve #1272
Resolve #1243 (Won't fix, but this stabilises the issue predictably)
Summary
This swaps out the
useQuery
implementation for aMutableSource
-like implementation, which has been proven to work byvaltio
(See: https://github.com/pmndrs/valtio/blob/ee4fee75da2054ddfef4cec826c0b9f2e95db4e9/src/useMutableSource.ts)In this implementation the general behaviour of
useQuery
does not change and preserves the logic as layed out by our previoususeSource
implementation. The hook will first start a temporary subscription toclient.executeQuery
which it'll discard after the mounting process completes. This is facilitated by a call togetSnapshot
which handles the suspense cache and throwing suspense promises.Additionally, if the hook uses suspense-mode this function may throw a suspense promise instead of returning a result. This function is used in the
useState
initialiser. The state, besides the currently usedsource
, the source's state (the result output), andgetSnapshot
function, also stores a list of dependencies. This list is compared to the state on every render. If it changes this signifies that the currentsource
should be swapped out and that the state should again be initialised usinggetSnapshot
.Additionally the results of sources is stored in a "Suspense cache" which is an ad-hoc cache layer that is instantiated by
getCacheForClient
. This imitates a primitive version of React's upcominggetCacheForType
function without any support for splitting the state or keeping it consistent across lanes, as the state is global.Instead we facilitate Concurrent Mode support by ensuring that all changes in the functionality of the hook work using state changes, which allow React to branch off lanes on any state change.
This is different from our previous
useSource
hook which kept closure state and effects across suspense triggers and lanes, which causes state to go out of sync.The suspense cache also protects itself from going stale. Each result in
useQuery
will update the stored state value in the cache. Additionally we can clear the result from the cache when the client tears down a query. This is done by tracking disposed querieson unmounts. Each time a result in the cache is updated a "dispose claim" for this operation is cleared. Each time auseQuery
hook unsubscribes inuseEffect
it creates a "dispose claim". When theClient
sends ateardown
operation and a "dispose claim" exists then the cache is cleared of the stale result.This effectively prevents cached values from being cleared inbetween a mount and the first effect run.
Set of changes
useSource
useQuery
withMutableSource
-like logic, only branch on stateuseSubscription
withoutuseSource
similarlygetCacheForClient