finished observable SortedArray to something useable

although not as performant as it could be
This commit is contained in:
Bruno Windels 2019-06-01 17:38:23 +02:00
parent f8fbfbff9a
commit 843c94b750
2 changed files with 21 additions and 8 deletions

View file

@ -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

View file

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