Skip to content

Commit 81e150e

Browse files
committed
fix bug
1 parent 74d18ad commit 81e150e

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

packages/svelte/src/reactivity/map.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ export class ReactiveMap extends Map {
7676
var s = this.#sources.get(key);
7777

7878
if (s === undefined) {
79-
// We should always track the version in case
80-
// the Set ever gets this value in the future.
81-
get(this.#version);
82-
83-
return undefined;
79+
// Re-use the has code-path to init the signal if needed and also
80+
// register to the version if needed.
81+
this.has(key);
82+
s = this.#sources.get(key);
83+
if (s === undefined) {
84+
return undefined;
85+
}
8486
}
8587

8688
get(s);

packages/svelte/src/reactivity/map.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,35 @@ test('map handling of undefined values', () => {
183183

184184
cleanup();
185185
});
186+
187+
test('not invoking reactivity when value is not in the map after changes', () => {
188+
const map = new ReactiveMap([[1, 1]]);
189+
190+
const log: any = [];
191+
192+
const cleanup = effect_root(() => {
193+
render_effect(() => {
194+
log.push(map.get(1));
195+
});
196+
197+
render_effect(() => {
198+
log.push(map.get(2));
199+
});
200+
201+
flushSync(() => {
202+
map.delete(1);
203+
});
204+
205+
flushSync(() => {
206+
map.set(1, 1);
207+
});
208+
});
209+
210+
assert.deepEqual(log, [1, undefined, undefined, 1]);
211+
212+
cleanup();
213+
});
214+
215+
test('Map.instanceOf', () => {
216+
assert.equal(new ReactiveMap() instanceof Map, true);
217+
});

0 commit comments

Comments
 (0)