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

Unexpected behaviour with Svelte 5, array mutation, and $inspect #13494

Open
andogq opened this issue Oct 4, 2024 · 0 comments
Open

Unexpected behaviour with Svelte 5, array mutation, and $inspect #13494

andogq opened this issue Oct 4, 2024 · 0 comments

Comments

@andogq
Copy link

andogq commented Oct 4, 2024

Describe the bug

Hi,

This issue has been created from some discussion on the Svelte Discord server.

Given an $state that contains an array, if a value computed from it is $inspect-ed and the array is mutated with an operation like splice or shift, the $inspect (and therefore value computation) will be re-triggered for each 'step' of the splice operation as implemented by the browser (move each value up one place, clear the last value, pop it). One of these steps results in undefined being present in the array, which can break code that otherwise wouldn't see an undefined.

I understand this isn't necessarily a bug in Svelte, but rather the culmination of how $inspect functions, the use of proxies for deep reactivity, and how the browser implements these array mutation operations. My problem is that this is unexpected behaviour, and developers (like me!) may spend time stuck on this problem trying to fix their code. Similarly to how adding console.log shouldn't cause code to break, adding $inspect shouldn't cause code to break.

It'd be great to see some kind of runtime warning to help direct users to understand why they're suddenly seeing an error when they use $inspect. I'm not sure what the conditions of this warning should be (computed array which is $inspected and mutated?).

I'd be more than happy to submit a PR if that's required, depending on what the outcome may be.

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE3VUTYvbMBD9K4NZiAOu0m0PhTQJFHYvPbSHQC_xgh17nKirSEYaJ4Tg_1592LG9u70Y2Xrz3rz58C2quEATLXe3SOYnjJbRj7qOkoiutXsxZxSE9t2oRhfuy8oUmte0SWVKVSML4kpCoTEn3JJ9xFxymsPN3ackkOCciwYNrOHBDIDv4T48NVKjZR-T0qGPiufDx5QWC3hSckZwySUBHREagxpIwUmVvLpCFqQyOPPc3nMDlolQTyieDwwynwnzcFY35hgzxuYZmKNqROkkOkZPovZ_saCBpEs32GFG5rWNoziI351Rm9jTyNFF6Vcs_3jUe2MNOiPOVPbApamtYpYE_QsXAvYIRS4ElnA5cmHobnZCY9F1rolb5PUTHbVqDsdAamrBC8xA1ahz17QEclmCkgiqcpAJT9ezXjlrZIkVl1hmbMAVSholkAl1iNNobC-Nkv-VZ6hPOiqpmxOJFx9tJ2X3MkJVSkPstLqhcPl2ZOMipsQrCCqwXq_hnvIbVEquLhcnB89aKx3P7lColD3PJjlSO4kenfuEwwR55XFk-25i-oAPR6RUW9-i6WgEoyx0L_6cwOObAeuF2sAU6nS6-l20lRxv5u4xgS8JfE3g24tnsfh-1uIuhI3b6ECrxbDwclWoEv3q9_Cu3nD7uf39ixnSXB7s4sST-3nribrg1V4vAtteQ39qiOx_RMnC-nxd32wR1pveBhtK03rxJ7Tzgo4yxG3sD8ovLMcyWpJusH1p_wFots5s2wQAAA==

Logs

No response

System Info

`svelte@5.0.0-next.260`

Severity

annoyance

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

1 participant