fix: properly track query instances inside and outside of reactive contexts#15353
Open
dummdidumm wants to merge 6 commits intomainfrom
Open
fix: properly track query instances inside and outside of reactive contexts#15353dummdidumm wants to merge 6 commits intomainfrom
dummdidumm wants to merge 6 commits intomainfrom
Conversation
…ntexts This switches the approach we take to tracking the query/prerender instances across invocations. Instead of having a "are you in a reactive context"-counter, we use a WeakRef instead to keep a weak reference to the instance we create. This a) simplifies the code and b) allows people to create instances outside of reactive contexts, and have them share the same instance with other queries later invoked inside tracking contexts. Practically speaking this avoids surprises where your instance, created outside a reactive context, is not getting any updates from mutation refreshes. Fixes #14559
🦋 Changeset detectedLatest commit: 14428fb 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 |
| /** | ||
| * Use this to iterate the query_map. Will clean up dereferenced resources as a side effect. | ||
| */ | ||
| export function get_query_array() { |
Contributor
There was a problem hiding this comment.
would it be worth having this be a generator function so that it doesn't have to populate an intermediate array? tradeoff being it's probably slower but with lower memory usage?
Member
Author
There was a problem hiding this comment.
I doubt that it matters. I would guess we talk about a thousand entries at most
Comment on lines
+59
to
+64
| // Delete the hydrated response from the cache at this point: | ||
| // If this instance is no longer referenced anywhere in the app, | ||
| // a new instance should not use the cached response, as it may be stale. | ||
| delete remote_responses[cache_key]; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises |
Contributor
There was a problem hiding this comment.
Suggested change
| // Delete the hydrated response from the cache at this point: | |
| // If this instance is no longer referenced anywhere in the app, | |
| // a new instance should not use the cached response, as it may be stale. | |
| delete remote_responses[cache_key]; | |
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | |
| // Delete the hydrated response from the cache after the resource has settled. | |
| // We need to wait because prerender functions have an async initialization | |
| // (`await prerender_cache_ready`) before they check `remote_responses`. | |
| // If this instance is no longer referenced anywhere in the app, | |
| // a new instance should not use the cached response, as it may be stale. | |
| resource.finally(() => { | |
| delete remote_responses[cache_key]; | |
| }); | |
Prerender functions delete hydrated responses from remote_responses before they can be read, causing unnecessary network requests.
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
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.
This switches the approach we take to tracking the query/prerender instances across invocations. Instead of having a "are you in a reactive context"-counter, we use a WeakRef instead to keep a weak reference to the instance we create. This a) simplifies the code and b) allows people to create instances outside of reactive contexts, and have them share the same instance with other queries later invoked inside tracking contexts. Practically speaking this avoids surprises where your instance, created outside a reactive context, is not getting any updates from mutation refreshes. Fixes #14559