Skip to content

svelte 5 : with reactive set has invokes in effects where it shouldn't  #11515

Closed
@FoHoOV

Description

@FoHoOV

Describe the bug

With ReactiveSet everything is fine grained, for instance doing this:

<script>
	import {Set} from "svelte/reactivity";	

	const data = [1,2];
	const reactiveSet = new Set(data);
	
	$effect(() => {
		console.log("has 1", reactiveSet.has(1));
	});

	$effect(() => {
		console.log("has 2", reactiveSet.has(2));
	});
	
</script>

<button onclick={()=>{
	reactiveSet.delete(2);
}}>
	delete 2
</button>

<button onclick={()=>{
	reactiveSet.add(2);
}}>
	add 2
</button>

If you press the "delete 2" button, it only logs "has 2: false". But if the value is not there to begin with?

<script>
	import {Set} from "svelte/reactivity";	

	const data = [1,2];
	const reactiveSet = new Set(data);
	
	$effect(() => {
		console.log("has 1", reactiveSet.has(1));
	});

	$effect(() => {
		console.log("has 2", reactiveSet.has(2));
	});

	$effect(() => {
		console.log("has 3", reactiveSet.has(3));
	});

	$inspect(reactiveSet.has(2));
</script>

<button onclick={()=>{
	reactiveSet.delete(2);
}}>
	delete 2
</button>

<button onclick={()=>{
	reactiveSet.add(2);
}}>
	add 2
</button>

now if you press the "delete 2" button, both "has 2: false" AND "has 3: false" will get logged, it also happens when you add something new.

It might be because of these lines? When a change occures we increase the version, which makes the call to "has 3: false" to be invoked.

Reproduction

1- go this REPL
2- press on "delete 2" button
3- see the logs which also logs "has 3: false"
4- press on "add 2" button
5- see the logs which also logs "has 3: false"

  • preferably you expect "has 3: false" be logged once until we actually add it or remove it afterwards.

Logs

No response

System Info

REPL (svelte5.next123)

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions