-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Description
Describe the bug
I have something like this in my app:
export class BrokenCounter {
#spring = new Spring({ value: 0 });
get count() { return this.#spring.current.value }
incr() { this.#spring.target.value++ }
decr() { this.#spring.target.value-- }
}Rather than re-assigning target to a new object, I directly mutate the target object.
Doing target.value++ instead of target = { value: target.value + 1 } causes the Spring to "miss" the update. It surprised me because I assumed target would be proxied by the Spring (to make it deeply reactive), just like $state. Plus, Spring already "looks deeply" into the target value (to animate individual properties and array items).
Creating a new object and assigning it to target makes for a quick workaround, but it is a bit inefficient and in some situations will lead to a lot of unnecessary garbage being created! This might be a regression from spring, which allowed mutating its target through the update method/function (I think?).
I see three potential solutions to this issue:
- Make
Spring(and maybe others?) deeply reactive just like$state - Warn against this in the documentation and hope people read it
- Somehow detect mutations to
targetand issue a warning in the console (probably just in development)
Reproduction
https://svelte.dev/playground/b9defdca3842490bb907fcfadaedfecc?version=5.16.0
Logs
No response
System Info
System:
OS: macOS 15.1.1
CPU: (8) arm64 Apple M2
Memory: 72.67 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.11.0 - ~/Library/pnpm/node
Yarn: 1.22.22 - ~/Library/pnpm/yarn
npm: 10.9.0 - ~/Library/pnpm/npm
pnpm: 9.14.2 - ~/Library/pnpm/pnpm
bun: 1.1.2 - ~/.bun/bin/bun
Browsers:
Safari: 18.1.1Severity
annoyance