From 290886a5eb82073174598a5e28234320db54aa4a Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 26 Feb 2019 21:13:43 +0100 Subject: [PATCH] fix and test subscription and initial values for sort --- src/observable/BaseObservableCollection.js | 4 ++-- src/observable/list/SortedMapList.js | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/observable/BaseObservableCollection.js b/src/observable/BaseObservableCollection.js index 1bdc9504..17ea3c82 100644 --- a/src/observable/BaseObservableCollection.js +++ b/src/observable/BaseObservableCollection.js @@ -13,13 +13,13 @@ export default class BaseObservableCollection { subscribe(handler) { this._handlers.add(handler); - if (this._handlers.length === 1) { + if (this._handlers.size === 1) { this.onSubscribeFirst(); } return () => { if (handler) { this._handlers.delete(this._handler); - if (this._handlers.length === 0) { + if (this._handlers.size === 0) { this.onUnsubscribeLast(); } handler = null; diff --git a/src/observable/list/SortedMapList.js b/src/observable/list/SortedMapList.js index 14ff076d..2c92cdf1 100644 --- a/src/observable/list/SortedMapList.js +++ b/src/observable/list/SortedMapList.js @@ -118,7 +118,7 @@ export default class SortedMapList extends BaseObservableList { } get(index) { - return this._sourceMap[index]; + return this._sortedPairs[index]; } get length() { @@ -126,11 +126,13 @@ export default class SortedMapList extends BaseObservableList { } [Symbol.iterator]() { - return this._sortedPairs; + return this._sortedPairs.values(); } } //#ifdef TESTS +import ObservableMap from "../map/ObservableMap.js"; + export function tests() { return { test_sortIndex(assert) { @@ -154,6 +156,17 @@ export function tests() { assert.deepEqual(a, [1, 2, 5, 8]); let idx = sortedIndex(a, 2, cmp); assert.equal(idx, 1); + }, + + test_initial_values(assert) { + const map = new ObservableMap([ + ["a", 50], + ["b", 6], + ["c", -5], + ]); + const list = new SortedMapList(map, (a, b) => a - b); + list.subscribe({}); //needed to populate iterator + assert.deepEqual(Array.from(list), [-5, 6, 50]); } } }