fix: get rid of data duplication in reactive map

pull/11200/head
Azarattum 2 years ago
parent 9aebae83a5
commit c42f193a62

@ -28,7 +28,6 @@ export class ReactiveMap extends Map {
for (var [key, v] of value) {
sources.set(key, source(v));
super.set(key, v);
}
this.#size.v = sources.size;
@ -62,7 +61,8 @@ export class ReactiveMap extends Map {
forEach(callbackfn, this_arg) {
get(this.#version);
return super.forEach(callbackfn, this_arg);
var bound_callbackfn = callbackfn.bind(this_arg);
this.#sources.forEach((s, key) => bound_callbackfn(s.v, key, this));
}
/** @param {K} key */
@ -96,7 +96,7 @@ export class ReactiveMap extends Map {
set(s, value);
}
return super.set(key, value);
return this;
}
/** @param {K} key */
@ -105,13 +105,14 @@ export class ReactiveMap extends Map {
var s = sources.get(key);
if (s !== undefined) {
sources.delete(key);
var removed = sources.delete(key);
set(this.#size, sources.size);
set(s, /** @type {V} */ (UNINITIALIZED));
this.#increment_version();
return removed;
}
return super.delete(key);
return false;
}
clear() {
@ -126,7 +127,6 @@ export class ReactiveMap extends Map {
}
sources.clear();
super.clear();
}
keys() {

@ -124,6 +124,40 @@ test('map.has(...)', () => {
cleanup();
});
test('map.forEach(...)', () => {
const map = new ReactiveMap([
[1, 1],
[2, 2],
[3, 3]
]);
const log: any = [];
const this_arg = {};
map.forEach(function (this: unknown, ...args) {
log.push([...args, this]);
}, this_arg);
assert.deepEqual(log, [
[1, 1, map, this_arg],
[2, 2, map, this_arg],
[3, 3, map, this_arg]
]);
});
test('map.delete(...)', () => {
const map = new ReactiveMap([
[1, 1],
[2, 2],
[3, 3]
]);
assert.equal(map.delete(3), true);
assert.equal(map.delete(3), false);
assert.deepEqual(Array.from(map.values()), [1, 2]);
});
test('map handling of undefined values', () => {
const map = new ReactiveMap();

Loading…
Cancel
Save