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; }