File tree Expand file tree Collapse file tree 4 files changed +62
-1
lines changed
tests/runtime-runes/samples/derived-map Expand file tree Collapse file tree 4 files changed +62
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' svelte ' : patch
3
+ ---
4
+
5
+ fix: ensure SvelteMap reactivity persists through deriveds
Original file line number Diff line number Diff line change @@ -94,13 +94,22 @@ export class SvelteMap extends Map {
94
94
var s = sources . get ( key ) ;
95
95
var prev_res = super . get ( key ) ;
96
96
var res = super . set ( key , value ) ;
97
+ var version = this . #version;
97
98
98
99
if ( s === undefined ) {
99
100
sources . set ( key , source ( 0 ) ) ;
100
101
set ( this . #size, super . size ) ;
101
- increment ( this . # version) ;
102
+ increment ( version ) ;
102
103
} else if ( prev_res !== value ) {
103
104
increment ( s ) ;
105
+ // If no one listening to this property and is listening to the version, or
106
+ // the inverse, then we should increment the version to be safe
107
+ if (
108
+ ( s . reactions === null && version . reactions !== null ) ||
109
+ ( s . reactions !== null && version . reactions === null )
110
+ ) {
111
+ increment ( version ) ;
112
+ }
104
113
}
105
114
106
115
return res ;
Original file line number Diff line number Diff line change
1
+ import { flushSync } from 'svelte' ;
2
+ import { test } from '../../test' ;
3
+
4
+ export default test ( {
5
+ html : `Loading` ,
6
+
7
+ async test ( { assert, target } ) {
8
+ await Promise . resolve ( ) ;
9
+ flushSync ( ) ;
10
+
11
+ assert . htmlEqual ( target . innerHTML , `1` ) ;
12
+ }
13
+ } ) ;
Original file line number Diff line number Diff line change
1
+ <script >
2
+ import { untrack } from ' svelte' ;
3
+ import { SvelteMap } from ' svelte/reactivity' ;
4
+
5
+ const cache = new SvelteMap ();
6
+
7
+ function get_async (id ) {
8
+ const model = cache .get (id);
9
+
10
+ if (! model) {
11
+ const promise = new Promise (async () => {
12
+ await Promise .resolve ();
13
+ cache .set (id, id .toString ());
14
+ }).then (() => cache .get (id));
15
+
16
+ untrack (() => {
17
+ cache .set (id, promise);
18
+ });
19
+
20
+ return promise;
21
+ }
22
+
23
+ return model;
24
+ }
25
+
26
+ const value = $derived (get_async (1 ));
27
+ </script >
28
+
29
+ {#if value instanceof Promise }
30
+ Loading
31
+ {:else }
32
+ {value }
33
+ {/if }
34
+
You can’t perform that action at this time.
0 commit comments