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: Mutating promise based arrays do not trigger updates #12166

Closed
navorite opened this issue Jun 24, 2024 · 7 comments · Fixed by #12179
Closed

Svelte 5: Mutating promise based arrays do not trigger updates #12166

navorite opened this issue Jun 24, 2024 · 7 comments · Fixed by #12179
Assignees
Labels

Comments

@navorite
Copy link
Contributor

navorite commented Jun 24, 2024

Describe the bug

If an awaited promise based array gets mutated, it doesn't trigger updates. To get to the correct state, one must hide the array then show the array items again. As far as I've debugged, $inspect doesn't detect the changes as well.

Reproduction

Svelte 5 REPL

Expected: Svelte 4 REPL

Logs

No response

System Info

svelte 5.0.0-next.162

Severity

blocking all usage of svelte

@navorite navorite changed the title Mutating promise based arrays inside an each block do not trigger updates Svelte 5: Mutating promise based arrays inside an each block do not trigger updates Jun 24, 2024
@navorite navorite changed the title Svelte 5: Mutating promise based arrays inside an each block do not trigger updates Svelte 5: Mutating promise based arrays do not trigger updates Jun 24, 2024
@trueadm trueadm assigned trueadm and unassigned trueadm Jun 24, 2024
@trueadm
Copy link
Contributor

trueadm commented Jun 24, 2024

This is because the $state rune only applies deep reactivity to plain object literals and arrays. It doesn't apply to Promises and thus any values resolved from a promise won't be automatically reactive.

@navorite
Copy link
Contributor Author

This is because the $state rune only applies deep reactivity to plain object literals and arrays. It doesn't apply to Promises and thus any values resolved from a promise won't be automatically reactive.

Is this a limitation of proxies or is there some kind of technical limitation? And if so, shouldn't this be documented?

@trueadm
Copy link
Contributor

trueadm commented Jun 24, 2024

This is because the $state rune only applies deep reactivity to plain object literals and arrays. It doesn't apply to Promises and thus any values resolved from a promise won't be automatically reactive.

Is this a limitation of proxies or is there some kind of technical limitation? And if so, shouldn't this be documented?

This isn’t really technically possible without AsyncContext, which is just a TC39 proposal. You’re crossing the async boundary, so Svelte has no knowledge of the resolved value and its relationship to state. We already document that only a plain objects and arrays work with deep reactivity.

@navorite
Copy link
Contributor Author

navorite commented Jun 24, 2024

This isn’t really technically possible without AsyncContext, which is just a TC39 proposal. You’re crossing the async boundary, so Svelte has no knowledge of the resolved value and its relationship to state. We already document that only a plain objects and arrays work with deep reactivity.

If I'm not mistaken, this is the line you are referring to in the docs?

Objects and arrays are made deeply reactive by wrapping them with Proxies:

This doesn't state clearly that only POJOs and arrays are made deeply reactive.

Also this should be documented in the breaking changes because it also breaks whether runes are used or not.

@trueadm
Copy link
Contributor

trueadm commented Jun 24, 2024

Why is it a breaking change? Runes are only a 5.0 feature, they weren’t in 4. We can adjust the documentation to clarify.

@navorite
Copy link
Contributor Author

Why is it a breaking change? Runes are only a 5.0 feature, they weren’t in 4. We can adjust the documentation to clarify.

Because it breaks in non-runes mode.

@trueadm
Copy link
Contributor

trueadm commented Jun 25, 2024

@navorite Ah that's likely something we can fix in non-runes mode then. :)

@trueadm trueadm added the bug label Jun 25, 2024
@trueadm trueadm self-assigned this Jun 25, 2024
FoHoOV pushed a commit to FoHoOV/svelte that referenced this issue Jun 27, 2024
Fixes sveltejs#12166. Turns out that the recent refactors here negated the fact that non-runes mode uses a slightly different source signal that has different equality rules.

I also updated the docs in this PR too to reflect that only plain objects and arrays are proxied.
trueadm added a commit that referenced this issue Jun 27, 2024
Fixes #12166. Turns out that the recent refactors here negated the fact that non-runes mode uses a slightly different source signal that has different equality rules.

I also updated the docs in this PR too to reflect that only plain objects and arrays are proxied.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants