From d5b88cbbe6a7f80de762debb69edd2f28538af6b Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sun, 24 Feb 2019 19:24:28 +0100 Subject: [PATCH] finish ObservableMap and add tests --- src/observable/map.js | 99 +++++++++++++++++++++++-- src/observable/map/BaseObservableMap.js | 18 ++--- 2 files changed, 102 insertions(+), 15 deletions(-) diff --git a/src/observable/map.js b/src/observable/map.js index e2044410..66d6616c 100644 --- a/src/observable/map.js +++ b/src/observable/map.js @@ -1,16 +1,19 @@ import BaseObservableMap from "./map/BaseObservableMap.js"; export default class ObservableMap extends BaseObservableMap { - constructor(initialValues) { + constructor(keyFn, initialValues) { super(); + this._keyFn = keyFn; this._values = new Map(initialValues); } update(key, params) { const value = this._values.get(key); if (value !== undefined) { - this._values.add(key, value); - this.emitChange(key, value, params); + // could be the same value, so it's already updated + // but we don't assume this here + this._values.set(key, value); + this.emitUpdate(key, value, params); return true; } return false; // or return existing value? @@ -18,7 +21,7 @@ export default class ObservableMap extends BaseObservableMap { add(key, value) { if (!this._values.has(key)) { - this._values.add(key, value); + this._values.set(key, value); this.emitAdd(key, value); return true; } @@ -50,6 +53,92 @@ export default class ObservableMap extends BaseObservableMap { } [Symbol.iterator]() { - return this._values.entries()[Symbol.iterator]; + return this._values.entries(); + } +} + +export function tests() { + return { + test_add(assert) { + let fired = 0; + const map = new ObservableMap(); + map.subscribe({ + onAdd(key, value) { + fired += 1; + assert.equal(key, 1); + assert.deepEqual(value, {value: 5}); + } + }); + map.add(1, {value: 5}); + assert.equal(map.size, 1); + assert.equal(fired, 1); + }, + + test_update(assert) { + let fired = 0; + const map = new ObservableMap(); + const value = {number: 5}; + map.add(1, value); + map.subscribe({ + onUpdate(key, value, params) { + fired += 1; + assert.equal(key, 1); + assert.deepEqual(value, {number: 6}); + assert.equal(params, "test"); + } + }); + value.number = 6; + map.update(1, "test"); + assert.equal(fired, 1); + }, + + test_update_unknown(assert) { + let fired = 0; + const map = new ObservableMap(v => v.id); + map.subscribe({ + onUpdate() { fired += 1; } + }); + const result = map.update(1); + assert.equal(fired, 0); + assert.equal(result, false); + }, + + test_remove(assert) { + let fired = 0; + const map = new ObservableMap(); + const value = {value: 5}; + map.add(1, value); + map.subscribe({ + onRemove(key, value) { + fired += 1; + assert.equal(key, 1); + assert.deepEqual(value, {value: 5}); + } + }); + map.remove(1); + assert.equal(map.size, 0); + assert.equal(fired, 1); + }, + + test_iterate(assert) { + const results = []; + const map = new ObservableMap(); + map.add(1, {number: 5}); + map.add(2, {number: 6}); + map.add(3, {number: 7}); + for (let e of map) { + results.push(e); + } + assert.equal(results.length, 3); + assert.equal(results.find(([key]) => key === 1)[1].number, 5); + assert.equal(results.find(([key]) => key === 2)[1].number, 6); + assert.equal(results.find(([key]) => key === 3)[1].number, 7); + }, + test_size(assert) { + const map = new ObservableMap(); + map.add(1, {number: 5}); + map.add(2, {number: 6}); + assert.equal(map.size, 2); + }, } } diff --git a/src/observable/map/BaseObservableMap.js b/src/observable/map/BaseObservableMap.js index 7d4f0ce1..f9ac296f 100644 --- a/src/observable/map/BaseObservableMap.js +++ b/src/observable/map/BaseObservableMap.js @@ -1,5 +1,3 @@ -import MapOperator from "./operators/MapOperator.js"; -import SortOperator from "./operators/SortOperator.js"; import BaseObservableCollection from "../BaseObservableCollection.js"; export default class BaseObservableMap extends BaseObservableCollection { @@ -16,9 +14,9 @@ export default class BaseObservableMap extends BaseObservableCollection { } } - emitChange(key, value, ...params) { + emitUpdate(key, value, ...params) { for(let h of this._handlers) { - h.onChange(key, value, ...params); + h.onUpdate(key, value, ...params); } } @@ -28,11 +26,11 @@ export default class BaseObservableMap extends BaseObservableCollection { } } - map(mapper, updater) { - return new MapOperator(this, mapper, updater); - } + // map(mapper, updater) { + // return new MapOperator(this, mapper, updater); + // } - sort(comparator) { - return new SortOperator(this, comparator); - } + // sort(comparator) { + // return new SortOperator(this, comparator); + // } }