fix: ensure no data loss occurs when using reactive Set methods (#11385)

pull/11384/head
Dominic Gannaway 8 months ago committed by GitHub
parent 2754e4eb39
commit e01776401d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: ensure no data loss occurs when using reactive Set methods

@ -51,8 +51,11 @@ export class ReactiveSet extends Set {
// @ts-ignore
proto[method] = function (...v) {
get(this.#version);
// We don't populate the underlying Set, so we need to create a clone using
// our internal values and then pass that to the method.
var clone = new Set(this.values());
// @ts-ignore
return set_proto[method].apply(this, v);
return set_proto[method].apply(clone, v);
};
}
@ -60,9 +63,11 @@ export class ReactiveSet extends Set {
// @ts-ignore
proto[method] = function (...v) {
get(this.#version);
// We don't populate the underlying Set, so we need to create a clone using
// our internal values and then pass that to the method.
var clone = new Set(this.values());
// @ts-ignore
var set = /** @type {Set<T>} */ (set_proto[method].apply(this, v));
var set = /** @type {Set<T>} */ (set_proto[method].apply(clone, v));
return new ReactiveSet(set);
};
}

@ -86,3 +86,23 @@ test('set.delete(...)', () => {
assert.deepEqual(Array.from(set.values()), [1, 2]);
});
test('set.forEach()', () => {
const set = new ReactiveSet([1, 2, 3, 4, 5]);
const log: any = [];
const cleanup = effect_root(() => {
render_effect(() => {
set.forEach((v) => log.push(v));
});
});
flushSync(() => {
set.add(6);
});
assert.deepEqual(log, [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]);
cleanup();
});

Loading…
Cancel
Save