svecosystem / runed

Magical utilities for your Svelte applications (WIP)
https://runed.dev
MIT License
557 stars 28 forks source link

fix: Ensure watch() runs when object properties are mutated #120

Closed Not-Jayden closed 3 months ago

Not-Jayden commented 3 months ago

Ensure watch() runs when object properties are mutated

Closes #119

changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: 0d57b4c6d868074123982620922849bcfb224a0d

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

This PR includes changesets to release 1 package | Name | Type | | ----- | ----- | | runed | Minor |

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

github-actions[bot] commented 3 months ago
built with Refined Cloudflare Pages Action

âš¡ Cloudflare Pages Deployment

Name Status Preview Last Commit
runed ✅ Ready (View Log) Visit Preview 0d57b4c6d868074123982620922849bcfb224a0d
abdel-17 commented 3 months ago

This is not a bug. This is how Svelte's effects work. They don't trigger on object mutations. You can get around this by using $state.snapshot, which deeply copies the object.

watch(() => $state.snapshot(condition)
abdel-17 commented 3 months ago

I don't think we should use $state.snapshot internally. It should be opt-in.

huntabyte commented 3 months ago

I don't think we should use $state.snapshot internally. It should be opt-in.

Yeah I'm in agreeance with you on this one. Defeats the purpose of "deeply reactive" by default and would create unexpected outcomes (when compared with using, say, a regular $effect).

We should document this behavior just for future reference, but gonna close this one as it should be handled in user land on an opt-in basis. Thanks @Not-Jayden!