pull/18532/merge
Nic Polumeyv 3 days ago committed by GitHub
commit 4df69d7b0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: don't notify `SvelteMap` key trackers when an existing key's value is set

@ -161,19 +161,23 @@ export class SvelteMap extends Map {
var sources = this.#sources;
var s = sources.get(key);
var prev_res = super.get(key);
var existed = super.has(key);
var res = super.set(key, value);
var version = this.#version;
if (s === undefined) {
s = this.#source(0);
// if the key existed but was never read individually, nothing tracks it and the key set is unchanged
if (!existed) {
s = this.#source(0);
if (DEV) {
tag(s, `SvelteMap get(${label(key)})`);
}
if (DEV) {
tag(s, `SvelteMap get(${label(key)})`);
}
sources.set(key, s);
set(this.#size, super.size);
increment(version);
sources.set(key, s);
set(this.#size, super.size);
increment(version);
}
} else if (prev_res !== value) {
increment(s);

@ -307,3 +307,33 @@ test('not invoking reactivity when value is not in the map after changes', () =>
test('Map.instanceOf', () => {
assert.equal(new SvelteMap() instanceof Map, true);
});
test('map.keys() is not notified when an existing unread key is set to a new value', () => {
const map = new SvelteMap([
[1, 'a'],
[2, 'b']
]);
const log: any = [];
const cleanup = effect_root(() => {
render_effect(() => {
log.push(Array.from(map.keys()));
});
});
flushSync(() => {
map.set(1, 'c');
});
flushSync(() => {
map.set(3, 'd');
});
assert.deepEqual(log, [
[1, 2],
[1, 2, 3]
]);
cleanup();
});

Loading…
Cancel
Save