Skip to content

Commit c42f193

Browse files
committed
fix: get rid of data duplication in reactive map
1 parent 9aebae8 commit c42f193

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

packages/svelte/src/reactivity/map.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export class ReactiveMap extends Map {
2828

2929
for (var [key, v] of value) {
3030
sources.set(key, source(v));
31-
super.set(key, v);
3231
}
3332

3433
this.#size.v = sources.size;
@@ -62,7 +61,8 @@ export class ReactiveMap extends Map {
6261
forEach(callbackfn, this_arg) {
6362
get(this.#version);
6463

65-
return super.forEach(callbackfn, this_arg);
64+
var bound_callbackfn = callbackfn.bind(this_arg);
65+
this.#sources.forEach((s, key) => bound_callbackfn(s.v, key, this));
6666
}
6767

6868
/** @param {K} key */
@@ -96,7 +96,7 @@ export class ReactiveMap extends Map {
9696
set(s, value);
9797
}
9898

99-
return super.set(key, value);
99+
return this;
100100
}
101101

102102
/** @param {K} key */
@@ -105,13 +105,14 @@ export class ReactiveMap extends Map {
105105
var s = sources.get(key);
106106

107107
if (s !== undefined) {
108-
sources.delete(key);
108+
var removed = sources.delete(key);
109109
set(this.#size, sources.size);
110110
set(s, /** @type {V} */ (UNINITIALIZED));
111111
this.#increment_version();
112+
return removed;
112113
}
113114

114-
return super.delete(key);
115+
return false;
115116
}
116117

117118
clear() {
@@ -126,7 +127,6 @@ export class ReactiveMap extends Map {
126127
}
127128

128129
sources.clear();
129-
super.clear();
130130
}
131131

132132
keys() {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,40 @@ test('map.has(...)', () => {
124124
cleanup();
125125
});
126126

127+
test('map.forEach(...)', () => {
128+
const map = new ReactiveMap([
129+
[1, 1],
130+
[2, 2],
131+
[3, 3]
132+
]);
133+
134+
const log: any = [];
135+
const this_arg = {};
136+
137+
map.forEach(function (this: unknown, ...args) {
138+
log.push([...args, this]);
139+
}, this_arg);
140+
141+
assert.deepEqual(log, [
142+
[1, 1, map, this_arg],
143+
[2, 2, map, this_arg],
144+
[3, 3, map, this_arg]
145+
]);
146+
});
147+
148+
test('map.delete(...)', () => {
149+
const map = new ReactiveMap([
150+
[1, 1],
151+
[2, 2],
152+
[3, 3]
153+
]);
154+
155+
assert.equal(map.delete(3), true);
156+
assert.equal(map.delete(3), false);
157+
158+
assert.deepEqual(Array.from(map.values()), [1, 2]);
159+
});
160+
127161
test('map handling of undefined values', () => {
128162
const map = new ReactiveMap();
129163

0 commit comments

Comments
 (0)