From 2e8c4563aa3e31bd9a5c6386ee59d45c5d67b432 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 22 Jun 2021 13:33:26 +0530 Subject: [PATCH] Implement set method to support update from value Signed-off-by: RMidhunSuresh --- src/observable/map/ObservableMap.js | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/observable/map/ObservableMap.js b/src/observable/map/ObservableMap.js index 4e9df5bb..b72cd039 100644 --- a/src/observable/map/ObservableMap.js +++ b/src/observable/map/ObservableMap.js @@ -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() { this._values.clear(); this.emitReset(); @@ -136,6 +147,31 @@ export function tests() { 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) { let fired = 0; const map = new ObservableMap();