fix and test subscription and initial values for sort

This commit is contained in:
Bruno Windels 2019-02-26 21:13:43 +01:00
parent 21d93a5893
commit 290886a5eb
2 changed files with 17 additions and 4 deletions

View file

@ -13,13 +13,13 @@ export default class BaseObservableCollection {
subscribe(handler) { subscribe(handler) {
this._handlers.add(handler); this._handlers.add(handler);
if (this._handlers.length === 1) { if (this._handlers.size === 1) {
this.onSubscribeFirst(); this.onSubscribeFirst();
} }
return () => { return () => {
if (handler) { if (handler) {
this._handlers.delete(this._handler); this._handlers.delete(this._handler);
if (this._handlers.length === 0) { if (this._handlers.size === 0) {
this.onUnsubscribeLast(); this.onUnsubscribeLast();
} }
handler = null; handler = null;

View file

@ -118,7 +118,7 @@ export default class SortedMapList extends BaseObservableList {
} }
get(index) { get(index) {
return this._sourceMap[index]; return this._sortedPairs[index];
} }
get length() { get length() {
@ -126,11 +126,13 @@ export default class SortedMapList extends BaseObservableList {
} }
[Symbol.iterator]() { [Symbol.iterator]() {
return this._sortedPairs; return this._sortedPairs.values();
} }
} }
//#ifdef TESTS //#ifdef TESTS
import ObservableMap from "../map/ObservableMap.js";
export function tests() { export function tests() {
return { return {
test_sortIndex(assert) { test_sortIndex(assert) {
@ -154,6 +156,17 @@ export function tests() {
assert.deepEqual(a, [1, 2, 5, 8]); assert.deepEqual(a, [1, 2, 5, 8]);
let idx = sortedIndex(a, 2, cmp); let idx = sortedIndex(a, 2, cmp);
assert.equal(idx, 1); 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]);
} }
} }
} }