Skip to content

Svelte 5: $derived reacitvity breaks for mapped arrays that are reusing class instances #13829

@ampatspell

Description

@ampatspell

Describe the bug

Looks like if models (Source) are wrapped in $derived which contains some kind of caching strategy to reuse wrappers (Target), wrappers looses reactivity.

let cache = new WeakMap<Source, Target>();

let targets = $derived.by(() => {
  return sources.map(source => {
    let target = cache.get(source);
    if(!target) {
      target = new Target(source);
      cache.set(source, target);
    }
    return target;
  });
});

Reproduction

https://svelte.dev/playground/151a2151934544288c5c5a793a09c27a?version=5.0.5

Click "Add source" few times then "Mark all as existing".

All rows should render target: {id} exists=true", instead only latest one (which hasn't been reused in $derived cache) properly updates.

Logs

No response

System Info

System:
  OS: macOS 14.6.1
  CPU: (8) arm64 Apple M1
  Memory: 142.06 MB / 8.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 18.19.0 - ~/.nvm/versions/node/v18.19.0/bin/node
  Yarn: 1.22.22 - ~/.nvm/versions/node/v18.19.0/bin/yarn
  npm: 10.2.3 - ~/.nvm/versions/node/v18.19.0/bin/npm
  pnpm: 8.15.5 - ~/.nvm/versions/node/v18.19.0/bin/pnpm
  bun: 1.0.15 - ~/.bun/bin/bun
  Watchman: 2023.12.04.00 - /opt/homebrew/bin/watchman
Browsers:
  Chrome: 130.0.6723.69
  Safari: 17.6
npmPackages:
  svelte: ^5.0.5 => 5.0.5

Severity

blocking all usage of svelte

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions