Skip to content
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

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

Closed
FoHoOV opened this issue May 8, 2024 · 4 comments
Closed

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

FoHoOV opened this issue May 8, 2024 · 4 comments

Comments

@FoHoOV
Copy link
Contributor

FoHoOV commented May 8, 2024

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

@FoHoOV
Copy link
Contributor Author

FoHoOV commented May 8, 2024

If it doesn't matter can we consider #11504?

@Azarattum
Copy link
Contributor

#11287 also fixes the problem described here.

@FoHoOV
Copy link
Contributor Author

FoHoOV commented May 9, 2024

I want to expand on this issue.
see this REPL:

  1. press on delete 2 button - you will see has 2: false and has 3: false as currently mentioned has 3: false shouln't be logged
  2. press on add 3 button, you will see has 2: false and has 3: true- nowhas 2: false` is having the same problem explained earlier.

it seems like tracking version here is wrong.

@FoHoOV
Copy link
Contributor Author

FoHoOV commented May 22, 2024

a better description is provided in #11727

@FoHoOV FoHoOV closed this as completed May 22, 2024
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

No branches or pull requests

2 participants