Implement set method to support update from value

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-06-22 13:33:26 +05:30
parent a1e3ff3ed8
commit 2e8c4563aa

View File

@ -54,6 +54,17 @@ export class ObservableMap extends BaseObservableMap {
} }
} }
set(key, value) {
if (this._values.has(key)) {
// We set the value here because update only supports inline updates
this._values.set(key, value);
return this.update(key);
}
else {
return this.add(key, value);
}
}
reset() { reset() {
this._values.clear(); this._values.clear();
this.emitReset(); this.emitReset();
@ -136,6 +147,31 @@ export function tests() {
assert.equal(result, false); assert.equal(result, false);
}, },
test_set(assert) {
let add_fired = 0, update_fired = 0;
const map = new ObservableMap();
map.subscribe({
onAdd(key, value) {
add_fired += 1;
assert.equal(key, 1);
assert.deepEqual(value, {value: 5});
},
onUpdate(key, value, params) {
update_fired += 1;
assert.equal(key, 1);
assert.deepEqual(value, {value: 7});
}
});
// Add
map.set(1, {value: 5});
assert.equal(map.size, 1);
assert.equal(add_fired, 1);
// Update
map.set(1, {value: 7});
assert.equal(map.size, 1);
assert.equal(update_fired, 1);
},
test_remove(assert) { test_remove(assert) {
let fired = 0; let fired = 0;
const map = new ObservableMap(); const map = new ObservableMap();