Skip to content

fix: properly track query instances inside and outside of reactive contexts#15353

Open
dummdidumm wants to merge 10 commits intomainfrom
query-weakref
Open

fix: properly track query instances inside and outside of reactive contexts#15353
dummdidumm wants to merge 10 commits intomainfrom
query-weakref

Conversation

@dummdidumm
Copy link
Member

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

…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-bot
Copy link

changeset-bot bot commented Feb 19, 2026

🦋 Changeset detected

Latest commit: 97aaf8e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

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

@dummdidumm dummdidumm marked this pull request as ready for review February 19, 2026 21:24
@svelte-docs-bot
Copy link

/**
* Use this to iterate the query_map. Will clean up dereferenced resources as a side effect.
*/
export function get_query_array() {

Choose a reason for hiding this comment

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

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

I doubt that it matters. I would guess we talk about a thousand entries at most

@Rich-Harris
Copy link
Member

I really worry about the non-determinism of this (as well as the big 'Avoid where possible' heading on MDN). I think it's really weird if the behaviour of something like this...

<script lang="ts">
	import { now } from './test.remote';

	async function onclick() {
		console.log(await now());
	}
</script>

<button {onclick}>click me</button>

...has the potential to differ between browsers or devices or whatever.

I don't have the answer to #14559 off the top of my head — possibly something about warning if a remote function is created/read in an unowned derived — but unfortunately I don't think this is it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remote Functions: Queries require explicit call in a method for Single Flight mutations to work in .svelte.ts/js files

4 participants