File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed
packages/svelte/tests/runtime-runes/samples/untracked-derived-local Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ import { test } from '../../test' ;
2+
3+ export default test ( {
4+ html : `3`
5+ } ) ;
Original file line number Diff line number Diff line change 1+ <script module >
2+ import { untrack } from ' svelte'
3+ import { SvelteMap } from ' svelte/reactivity'
4+
5+ class Foo {
6+ id
7+ updateTime = $state (Date .now ())
8+
9+ constructor (id ) {
10+ this .id = id
11+ }
12+ }
13+
14+ class Store {
15+ cache = new SvelteMap ()
16+ ids = $state ([1 , 2 , 3 ])
17+
18+ getOrDefault (id ) {
19+ let ret = this .cache .get (id)
20+
21+ if (ret) {
22+ return ret
23+ }
24+
25+ ret = untrack (() => {
26+ ret = new Foo (id)
27+ this .cache .set (id, ret)
28+ return ret
29+ })
30+ this .cache .get (id)
31+
32+ return ret
33+ }
34+
35+ get values () {
36+ return this .ids .map (id => this .getOrDefault (id)).sort ((a , b ) => b .updateTime - a .updateTime )
37+ }
38+ }
39+
40+ const store = new Store ()
41+ </script >
42+
43+ <script >
44+ const test = $derived .by (() => store .values .length )
45+ </script >
46+
47+ {test }
You can’t perform that action at this time.
0 commit comments