From 843c94b75090479b80dcc9ad2f0501ea74a74adb Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sat, 1 Jun 2019 17:38:23 +0200 Subject: [PATCH] finished observable SortedArray to something useable although not as performant as it could be --- src/observable/index.js | 3 ++- src/observable/list/SortedArray.js | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/observable/index.js b/src/observable/index.js index 36e037cd..828a6aea 100644 --- a/src/observable/index.js +++ b/src/observable/index.js @@ -3,7 +3,8 @@ import FilteredMap from "./map/FilteredMap.js"; import MappedMap from "./map/MappedMap.js"; import BaseObservableMap from "./map/BaseObservableMap.js"; // re-export "root" (of chain) collections -export { default as ObservableArray} from "./list/ObservableArray.js"; +export { default as ObservableArray } from "./list/ObservableArray.js"; +export { default as SortedArray } from "./list/SortedArray.js"; export { default as ObservableMap } from "./map/ObservableMap.js"; // avoid circular dependency between these classes diff --git a/src/observable/list/SortedArray.js b/src/observable/list/SortedArray.js index e01bae4f..b18b7250 100644 --- a/src/observable/list/SortedArray.js +++ b/src/observable/list/SortedArray.js @@ -8,22 +8,34 @@ export default class SortedArray extends BaseObservableList { this._items = []; } - setSortedMany(items) { - + setManySorted(items) { + // TODO: we can make this way faster by only looking up the first and last key, + // and merging whatever is inbetween with items + // if items is not sorted, 💩🌀 will follow! + // should we check? + // Also, once bulk events are supported in collections, + // we can do a bulk add event here probably if there are no updates + // BAD CODE! + for(let item of items) { + this.set(item); + } } - set(item) { + set(item, updateParams = null) { const idx = sortedIndex(this._items, item, this._comparator); - if (idx < this._items.length || this._comparator(this._items[idx], item) !== 0) { + if (idx >= this._items.length || this._comparator(this._items[idx], item) !== 0) { this._items.splice(idx, 0, item); - //emitAdd + this.emitAdd(idx, item) } else { this._items[idx] = item; - //emitRemove - //emitAdd + this.emitUpdate(idx, item, updateParams); } } + remove(item) { + throw new Error("unimplemented"); + } + get array() { return this._items; }