hydrogen-web/src/observable/list/SortedMapList.js

272 lines
9.1 KiB
JavaScript
Raw Normal View History

2020-08-05 22:08:55 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {BaseObservableList} from "./BaseObservableList";
2021-11-16 15:52:42 +05:30
import {sortedIndex} from "../../utils/sortedIndex";
2019-02-22 03:38:23 +05:30
/*
when a value changes, it sorting order can change. It would still be at the old index prior to firing an onUpdate event.
2019-02-22 03:38:23 +05:30
So how do you know where it was before it changed, if not by going over all values?
how to make this fast?
seems hard to solve with an array, because you need to map the key to it's previous location somehow, to efficiently find it,
and move it.
I wonder if we could do better with a binary search tree (BST).
The tree has a value with {key, value}. There is a plain Map mapping keys to this tuple,
for easy lookup. Now how do we find the index of this tuple in the BST?
either we store in every node the amount of nodes on the left and right, or we decend into the part
of the tree preceding the node we want to know about. Updating the counts upwards would probably be fine as this is log2 of
the size of the container.
to be able to go from a key to an index, the value would have the have a link with the tree node though
so key -> Map<key,value> -> value -> node -> *parentNode -> rootNode
with a node containing {value, leftCount, rightCount, leftNode, rightNode, parentNode}
*/
// does not assume whether or not the values are reference
// types modified outside of the collection (and affecting sort order) or not
2019-02-22 03:38:23 +05:30
// no duplicates allowed for now
export class SortedMapList extends BaseObservableList {
constructor(sourceMap, comparator) {
2019-02-22 03:38:23 +05:30
super();
this._sourceMap = sourceMap;
this._comparator = (a, b) => comparator(a.value, b.value);
this._sortedPairs = null;
this._mapSubscription = null;
2019-02-21 04:18:16 +05:30
}
onAdd(key, value) {
const pair = {key, value};
const idx = sortedIndex(this._sortedPairs, pair, this._comparator);
this._sortedPairs.splice(idx, 0, pair);
2019-02-21 04:18:16 +05:30
this.emitAdd(idx, value);
}
2019-02-22 03:38:23 +05:30
onRemove(key, value) {
const pair = {key, value};
const idx = sortedIndex(this._sortedPairs, pair, this._comparator);
// assert key === this._sortedPairs[idx].key;
this._sortedPairs.splice(idx, 1);
2019-02-22 03:38:23 +05:30
this.emitRemove(idx, value);
2019-02-21 04:18:16 +05:30
}
onUpdate(key, value, params) {
guard against updates emitted while populating during first subscription This came up now because Timeline uses a MappedList to map PendingEvents to PendingEventEntries. In the map function, we setup links between entries to support local echo for relations. When opening a timeline that has unsent relations, the initial populating of the MappedList will try to emit an update for the target entry in remoteEntries. This all happens while the ListView of the timeline is calling subscribe and all collections in the chain are populating themselves based on their sources. This usually entails calling subscribe on the source, and now you are subscribed, iterate over the source (as you're not allowed to query an unsubscribed observable collection, as it might not be populated yet, and even if it did, it wouldn't be guaranteed to be up to date as events aren't flowing yet). So in this concrete example, TilesCollection hadn't populated its tiles yet and when the update to the target of the unsent relation reached TilesCollection, the tiles array was still null and it crashed. I thought what would be the best way to fix this and have a solid model for observable collections to ensure they are always compatible with each other. I considered splitting up the subscription process in two steps where you'd first populate the source and then explicitly start events flowing. I didn't go with this way because it's really only updates that make sense to be emitted during setup. A missed update wouldn't usually bring the collections out of sync like a missed add or remove would. It would just mean the UI isn't updated (or any subsequent filtered collections are not updated), but this should be fine to ignore during setup, as you can rely on the subscribing collections down the chain picking up the update while populating. If we ever want to support add or remove events during setup, we would have to explicitly support them, but for now they are correct to throw. So for now, just ignore update events that happen during setup where needed.
2021-05-27 13:32:05 +05:30
// if an update is emitted while calling source.subscribe() from onSubscribeFirst, ignore it
if (!this._sortedPairs) {
return;
}
// TODO: suboptimal for performance, see above for idea with BST to speed this up if we need to
const oldIdx = this._sortedPairs.findIndex(p => p.key === key);
// neccesary to remove pair from array before
// doing sortedIndex as it relies on being sorted
this._sortedPairs.splice(oldIdx, 1);
const pair = {key, value};
const newIdx = sortedIndex(this._sortedPairs, pair, this._comparator);
this._sortedPairs.splice(newIdx, 0, pair);
if (oldIdx !== newIdx) {
2019-02-22 03:38:23 +05:30
this.emitMove(oldIdx, newIdx, value);
}
this.emitUpdate(newIdx, value, params);
2019-02-22 03:38:23 +05:30
}
2019-02-21 04:18:16 +05:30
2019-02-22 03:38:23 +05:30
onReset() {
this._sortedPairs = [];
2019-02-22 03:38:23 +05:30
this.emitReset();
2019-02-21 04:18:16 +05:30
}
onSubscribeFirst() {
2019-02-22 03:38:23 +05:30
this._mapSubscription = this._sourceMap.subscribe(this);
this._sortedPairs = new Array(this._sourceMap.size);
2019-02-21 04:18:16 +05:30
let i = 0;
for (let [key, value] of this._sourceMap) {
this._sortedPairs[i] = {key, value};
2019-02-21 04:18:16 +05:30
++i;
}
this._sortedPairs.sort(this._comparator);
2019-02-21 04:18:16 +05:30
super.onSubscribeFirst();
}
onUnsubscribeLast() {
super.onUnsubscribeLast();
this._sortedPairs = null;
2019-02-22 03:38:23 +05:30
this._mapSubscription = this._mapSubscription();
2019-02-21 04:18:16 +05:30
}
get(index) {
return this._sortedPairs[index].value;
}
2019-02-21 04:18:16 +05:30
get length() {
2019-02-22 03:38:23 +05:30
return this._sourceMap.size;
2019-02-21 04:18:16 +05:30
}
[Symbol.iterator]() {
const it = this._sortedPairs.values();
return {
next() {
const v = it.next();
if (v.value) {
v.value = v.value.value;
}
return v;
}
}
2019-02-21 04:18:16 +05:30
}
}
import {ObservableMap} from "../map/ObservableMap";
2019-02-21 04:18:16 +05:30
export function tests() {
return {
test_sortIndex(assert) {
const a = [1, 5, 6, 8];
const cmp = (a, b) => a - b;
let idx = sortedIndex(a, 0, cmp);
2019-02-21 04:18:16 +05:30
assert.equal(idx, 0);
idx = sortedIndex(a, 3, cmp);
assert.equal(idx, 1);
idx = sortedIndex(a, 5, cmp);
2019-02-21 04:18:16 +05:30
assert.equal(idx, 1);
idx = sortedIndex(a, 8, cmp);
2019-02-21 04:18:16 +05:30
assert.equal(idx, 3);
},
test_sortIndex_reverse(assert) {
let idx = sortedIndex([8, 6, 5, 1], 6, (a, b) => b - a);
assert.equal(idx, 1);
},
test_sortIndex_comparator_Array_compatible(assert) {
2019-02-21 04:18:16 +05:30
const a = [5, 1, 8, 2];
const cmp = (a, b) => a - b;
a.sort(cmp);
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]);
assert.equal(list.length, 3);
},
test_add(assert) {
const map = new ObservableMap([["a", 50], ["b", 6], ["c", -5]]);
const list = new SortedMapList(map, (a, b) => a - b);
let fired = 0;
list.subscribe({
onAdd(idx, value) {
fired++;
assert.equal(idx, 2);
assert.equal(value, 20);
}
});
map.add("d", 20);
assert.equal(fired, 1);
assert.equal(list.length, 4);
},
test_remove(assert) {
const map = new ObservableMap([["a", 50], ["b", 6], ["c", -5]]);
const list = new SortedMapList(map, (a, b) => a - b);
let fired = 0;
list.subscribe({
onRemove(idx, value) {
fired++;
assert.equal(idx, 2);
assert.equal(value, 50);
}
});
map.remove("a");
assert.equal(fired, 1);
assert.equal(list.length, 2);
},
test_move_reference(assert) {
const a = {number: 3};
const map = new ObservableMap([
["a", a],
["b", {number: 11}],
["c", {number: 1}],
]);
const list = new SortedMapList(map, (a, b) => a.number - b.number);
let updateFired = 0, moveFired = 0;
list.subscribe({
onUpdate(idx, value, param) {
updateFired++;
assert.equal(idx, 2);
assert.equal(value, a);
assert.equal(param, "number");
},
onMove(oldIdx, newIdx, value) {
moveFired++;
assert.equal(oldIdx, 1);
assert.equal(newIdx, 2);
assert.equal(value, a);
}
});
a.number = 111;
map.update("a", "number");
assert.equal(moveFired, 1);
assert.equal(updateFired, 1);
},
test_update_without_move(assert) {
const a = {number: 3};
const map = new ObservableMap([
["a", a],
["b", {number: 11}],
["c", {number: 1}],
]);
const list = new SortedMapList(map, (a, b) => a.number - b.number);
let updateFired = 0, moveFired = 0;
list.subscribe({
onUpdate(idx, value, param) {
updateFired++;
assert.equal(idx, 1);
assert.equal(value, a);
assert.equal(param, "number");
},
onMove() {
moveFired++;
}
});
assert.deepEqual(Array.from(list).map(v => v.number), [1, 3, 11]);
// asume some part of a that doesn't affect
// sorting order has changed here
map.update("a", "number");
assert.equal(moveFired, 0);
assert.equal(updateFired, 1);
assert.deepEqual(Array.from(list).map(v => v.number), [1, 3, 11]);
},
2019-02-21 04:18:16 +05:30
}
}