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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
import {BaseObservableList} from "../../../../observable/list/BaseObservableList.js";
|
|
|
|
import {sortedIndex} from "../../../../utils/sortedIndex.js";
|
2019-03-09 00:34:56 +05:30
|
|
|
|
2019-06-01 21:59:02 +05:30
|
|
|
// maps 1..n entries to 0..1 tile. Entries are what is stored in the timeline, either an event or fragmentboundary
|
2020-03-21 18:58:09 +05:30
|
|
|
// for now, tileCreator should be stable in whether it returns a tile or not.
|
|
|
|
// e.g. the decision to create a tile or not should be based on properties
|
|
|
|
// not updated later on (e.g. event type)
|
|
|
|
// also see big comment in onUpdate
|
2020-04-21 00:56:39 +05:30
|
|
|
export class TilesCollection extends BaseObservableList {
|
2019-03-09 00:34:56 +05:30
|
|
|
constructor(entries, tileCreator) {
|
|
|
|
super();
|
|
|
|
this._entries = entries;
|
|
|
|
this._tiles = null;
|
|
|
|
this._entrySubscription = null;
|
|
|
|
this._tileCreator = tileCreator;
|
2019-06-13 01:27:13 +05:30
|
|
|
this._emitSpontanousUpdate = this._emitSpontanousUpdate.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_emitSpontanousUpdate(tile, params) {
|
|
|
|
const entry = tile.lowerEntry;
|
|
|
|
const tileIdx = this._findTileIdx(entry);
|
|
|
|
this.emitUpdate(tileIdx, tile, params);
|
2019-03-09 00:34:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
onSubscribeFirst() {
|
|
|
|
this._entrySubscription = this._entries.subscribe(this);
|
|
|
|
this._populateTiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
_populateTiles() {
|
|
|
|
this._tiles = [];
|
|
|
|
let currentTile = null;
|
|
|
|
for (let entry of this._entries) {
|
|
|
|
if (!currentTile || !currentTile.tryIncludeEntry(entry)) {
|
2020-03-21 18:58:09 +05:30
|
|
|
currentTile = this._tileCreator(entry);
|
2019-06-02 18:29:30 +05:30
|
|
|
if (currentTile) {
|
|
|
|
this._tiles.push(currentTile);
|
|
|
|
}
|
2019-03-09 00:34:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
let prevTile = null;
|
|
|
|
for (let tile of this._tiles) {
|
|
|
|
if (prevTile) {
|
|
|
|
prevTile.updateNextSibling(tile);
|
|
|
|
}
|
|
|
|
tile.updatePreviousSibling(prevTile);
|
|
|
|
prevTile = tile;
|
|
|
|
}
|
|
|
|
if (prevTile) {
|
|
|
|
prevTile.updateNextSibling(null);
|
|
|
|
}
|
2020-03-21 18:58:09 +05:30
|
|
|
// now everything is wired up,
|
|
|
|
// allow tiles to emit updates
|
|
|
|
for (const tile of this._tiles) {
|
|
|
|
tile.setUpdateEmit(this._emitSpontanousUpdate);
|
|
|
|
}
|
2019-03-09 00:34:56 +05:30
|
|
|
}
|
|
|
|
|
2019-06-01 21:59:02 +05:30
|
|
|
_findTileIdx(entry) {
|
|
|
|
return sortedIndex(this._tiles, entry, (entry, tile) => {
|
2019-03-09 05:10:17 +05:30
|
|
|
// negate result because we're switching the order of the params
|
2019-06-01 21:59:02 +05:30
|
|
|
return -tile.compareEntry(entry);
|
2019-03-09 00:34:56 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:59:02 +05:30
|
|
|
_findTileAtIdx(entry, idx) {
|
2019-03-09 05:10:17 +05:30
|
|
|
const tile = this._getTileAtIdx(idx);
|
2019-06-01 21:59:02 +05:30
|
|
|
if (tile && tile.compareEntry(entry) === 0) {
|
2019-03-09 05:10:17 +05:30
|
|
|
return tile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_getTileAtIdx(tileIdx) {
|
|
|
|
if (tileIdx >= 0 && tileIdx < this._tiles.length) {
|
|
|
|
return this._tiles[tileIdx];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-03-09 00:34:56 +05:30
|
|
|
onUnsubscribeLast() {
|
|
|
|
this._entrySubscription = this._entrySubscription();
|
2020-10-23 20:48:11 +05:30
|
|
|
for(let i = 0; i < this._tiles.length; i+= 1) {
|
|
|
|
this._tiles[i].dispose();
|
|
|
|
}
|
2019-03-09 00:34:56 +05:30
|
|
|
this._tiles = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
onReset() {
|
|
|
|
// if TileViewModel were disposable, dispose here, or is that for views to do? views I suppose ...
|
|
|
|
this._buildInitialTiles();
|
|
|
|
this.emitReset();
|
|
|
|
}
|
|
|
|
|
2019-03-09 05:10:17 +05:30
|
|
|
onAdd(index, entry) {
|
2019-06-01 21:59:02 +05:30
|
|
|
const tileIdx = this._findTileIdx(entry);
|
2019-03-09 05:10:17 +05:30
|
|
|
const prevTile = this._getTileAtIdx(tileIdx - 1);
|
|
|
|
if (prevTile && prevTile.tryIncludeEntry(entry)) {
|
|
|
|
this.emitUpdate(tileIdx - 1, prevTile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// not + 1 because this entry hasn't been added yet
|
|
|
|
const nextTile = this._getTileAtIdx(tileIdx);
|
|
|
|
if (nextTile && nextTile.tryIncludeEntry(entry)) {
|
|
|
|
this.emitUpdate(tileIdx, nextTile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-21 18:58:09 +05:30
|
|
|
const newTile = this._tileCreator(entry);
|
2019-03-09 05:10:17 +05:30
|
|
|
if (newTile) {
|
2019-06-16 19:09:24 +05:30
|
|
|
if (prevTile) {
|
|
|
|
prevTile.updateNextSibling(newTile);
|
2020-03-21 18:58:09 +05:30
|
|
|
// this emits an update while the add hasn't been emitted yet
|
2019-06-16 19:09:24 +05:30
|
|
|
newTile.updatePreviousSibling(prevTile);
|
|
|
|
}
|
|
|
|
if (nextTile) {
|
|
|
|
newTile.updateNextSibling(nextTile);
|
|
|
|
nextTile.updatePreviousSibling(newTile);
|
|
|
|
}
|
2019-03-09 05:10:17 +05:30
|
|
|
this._tiles.splice(tileIdx, 0, newTile);
|
|
|
|
this.emitAdd(tileIdx, newTile);
|
2020-03-21 18:58:09 +05:30
|
|
|
// add event is emitted, now the tile
|
|
|
|
// can emit updates
|
|
|
|
newTile.setUpdateEmit(this._emitSpontanousUpdate);
|
2019-03-09 05:10:17 +05:30
|
|
|
}
|
2019-03-09 00:34:56 +05:30
|
|
|
// find position by sort key
|
|
|
|
// ask siblings to be included? both? yes, twice: a (insert c here) b, ask a(c), if yes ask b(a), else ask b(c)? if yes then b(a)?
|
|
|
|
}
|
|
|
|
|
2019-03-09 05:10:17 +05:30
|
|
|
onUpdate(index, entry, 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._tiles) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-01 21:59:02 +05:30
|
|
|
const tileIdx = this._findTileIdx(entry);
|
|
|
|
const tile = this._findTileAtIdx(entry, tileIdx);
|
2019-03-09 05:10:17 +05:30
|
|
|
if (tile) {
|
2019-06-13 01:27:13 +05:30
|
|
|
const action = tile.updateEntry(entry, params);
|
2020-09-11 21:17:35 +05:30
|
|
|
if (action.shouldReplace) {
|
2020-09-18 17:06:16 +05:30
|
|
|
const newTile = this._tileCreator(entry);
|
|
|
|
if (newTile) {
|
2020-10-27 20:49:36 +05:30
|
|
|
this._replaceTile(tileIdx, tile, newTile, action.updateParams);
|
|
|
|
newTile.setUpdateEmit(this._emitSpontanousUpdate);
|
2020-09-18 17:06:16 +05:30
|
|
|
} else {
|
|
|
|
this._removeTile(tileIdx, tile);
|
|
|
|
}
|
2020-09-11 21:17:35 +05:30
|
|
|
}
|
2019-06-13 01:27:13 +05:30
|
|
|
if (action.shouldRemove) {
|
|
|
|
this._removeTile(tileIdx, tile);
|
|
|
|
}
|
|
|
|
if (action.shouldUpdate) {
|
|
|
|
this.emitUpdate(tileIdx, tile, action.updateParams);
|
2019-03-09 05:10:17 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
// technically we should handle adding a tile here as well
|
|
|
|
// in case before we didn't have a tile for it but now we do
|
|
|
|
// but in reality we don't have this use case as the type and msgtype
|
|
|
|
// doesn't change. Decryption maybe is the exception?
|
|
|
|
|
|
|
|
|
2019-03-09 00:34:56 +05:30
|
|
|
// outcomes here can be
|
|
|
|
// tiles should be removed (got redacted and we don't want it in the timeline)
|
|
|
|
// tile should be added where there was none before ... ?
|
|
|
|
// entry should get it's own tile now
|
2019-03-09 05:10:17 +05:30
|
|
|
// merge with neighbours? ... hard to imagine use case for this ...
|
2019-03-09 00:34:56 +05:30
|
|
|
}
|
|
|
|
|
2020-10-27 20:49:36 +05:30
|
|
|
_replaceTile(tileIdx, existingTile, newTile, updateParams) {
|
2020-09-14 21:14:22 +05:30
|
|
|
existingTile.dispose();
|
2020-09-11 21:17:35 +05:30
|
|
|
const prevTile = this._getTileAtIdx(tileIdx - 1);
|
|
|
|
const nextTile = this._getTileAtIdx(tileIdx + 1);
|
|
|
|
this._tiles[tileIdx] = newTile;
|
|
|
|
prevTile?.updateNextSibling(newTile);
|
|
|
|
newTile.updatePreviousSibling(prevTile);
|
|
|
|
newTile.updateNextSibling(nextTile);
|
|
|
|
nextTile?.updatePreviousSibling(newTile);
|
2020-10-27 20:49:36 +05:30
|
|
|
this.emitUpdate(tileIdx, newTile, updateParams);
|
2020-09-11 21:17:35 +05:30
|
|
|
}
|
|
|
|
|
2019-06-13 01:27:13 +05:30
|
|
|
_removeTile(tileIdx, tile) {
|
|
|
|
const prevTile = this._getTileAtIdx(tileIdx - 1);
|
|
|
|
const nextTile = this._getTileAtIdx(tileIdx + 1);
|
2021-05-31 15:26:41 +05:30
|
|
|
// applying and emitting the remove should happen
|
|
|
|
// atomically, as updateNext/PreviousSibling might
|
|
|
|
// emit an update with the wrong index otherwise
|
2019-06-13 01:27:13 +05:30
|
|
|
this._tiles.splice(tileIdx, 1);
|
2020-09-14 21:14:22 +05:30
|
|
|
tile.dispose();
|
2019-06-13 01:27:13 +05:30
|
|
|
this.emitRemove(tileIdx, tile);
|
2021-05-31 15:26:41 +05:30
|
|
|
prevTile?.updateNextSibling(nextTile);
|
|
|
|
nextTile?.updatePreviousSibling(prevTile);
|
2019-06-13 01:27:13 +05:30
|
|
|
}
|
|
|
|
|
2019-03-09 05:10:17 +05:30
|
|
|
// would also be called when unloading a part of the timeline
|
|
|
|
onRemove(index, entry) {
|
2019-06-01 21:59:02 +05:30
|
|
|
const tileIdx = this._findTileIdx(entry);
|
|
|
|
const tile = this._findTileAtIdx(entry, tileIdx);
|
2019-03-09 05:10:17 +05:30
|
|
|
if (tile) {
|
|
|
|
const removeTile = tile.removeEntry(entry);
|
|
|
|
if (removeTile) {
|
2019-06-13 01:27:13 +05:30
|
|
|
this._removeTile(tileIdx, tile);
|
2019-03-09 05:10:17 +05:30
|
|
|
} else {
|
|
|
|
this.emitUpdate(tileIdx, tile);
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 00:34:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
onMove(fromIdx, toIdx, value) {
|
|
|
|
// this ... cannot happen in the timeline?
|
2019-06-13 01:27:13 +05:30
|
|
|
// perhaps we can use this event to support a local echo (in a different fragment)
|
|
|
|
// to be moved to the key of the remote echo, so we don't loose state ... ?
|
2019-03-09 00:34:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
[Symbol.iterator]() {
|
|
|
|
return this._tiles.values();
|
|
|
|
}
|
2019-06-02 18:29:30 +05:30
|
|
|
|
|
|
|
get length() {
|
|
|
|
return this._tiles.length;
|
|
|
|
}
|
2020-08-17 18:43:12 +05:30
|
|
|
|
|
|
|
getFirst() {
|
|
|
|
return this._tiles[0];
|
|
|
|
}
|
2019-03-09 00:34:56 +05:30
|
|
|
}
|
2020-03-21 18:27:41 +05:30
|
|
|
|
2020-04-21 00:56:39 +05:30
|
|
|
import {ObservableArray} from "../../../../observable/list/ObservableArray.js";
|
|
|
|
import {UpdateAction} from "./UpdateAction.js";
|
2020-03-21 18:27:41 +05:30
|
|
|
|
|
|
|
export function tests() {
|
|
|
|
class TestTile {
|
2020-03-21 18:58:09 +05:30
|
|
|
constructor(entry) {
|
2020-03-21 18:27:41 +05:30
|
|
|
this.entry = entry;
|
2020-03-21 18:58:09 +05:30
|
|
|
this.update = null;
|
|
|
|
}
|
|
|
|
setUpdateEmit(update) {
|
2020-03-21 18:27:41 +05:30
|
|
|
this.update = update;
|
|
|
|
}
|
|
|
|
tryIncludeEntry() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
compareEntry(b) {
|
|
|
|
return this.entry.n - b.n;
|
|
|
|
}
|
|
|
|
removeEntry() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
get upperEntry() {
|
|
|
|
return this.entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
get lowerEntry() {
|
|
|
|
return this.entry;
|
|
|
|
}
|
|
|
|
updateNextSibling() {}
|
|
|
|
updatePreviousSibling() {}
|
|
|
|
updateEntry() {
|
|
|
|
return UpdateAction.Nothing;
|
|
|
|
}
|
2020-09-14 21:14:22 +05:30
|
|
|
|
|
|
|
dispose() {}
|
2020-03-21 18:27:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"don't emit update before add": assert => {
|
|
|
|
class UpdateOnSiblingTile extends TestTile {
|
|
|
|
updateNextSibling() {
|
|
|
|
// this happens with isContinuation
|
2020-03-21 18:58:09 +05:30
|
|
|
this.update && this.update(this, "next");
|
2020-03-21 18:27:41 +05:30
|
|
|
}
|
|
|
|
updatePreviousSibling() {
|
|
|
|
// this happens with isContinuation
|
2020-03-21 18:58:09 +05:30
|
|
|
this.update && this.update(this, "previous");
|
2020-03-21 18:27:41 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
const entries = new ObservableArray([{n: 5}, {n: 10}]);
|
2020-03-21 18:58:09 +05:30
|
|
|
const tiles = new TilesCollection(entries, entry => new UpdateOnSiblingTile(entry));
|
2020-03-21 18:27:41 +05:30
|
|
|
let receivedAdd = false;
|
|
|
|
tiles.subscribe({
|
|
|
|
onAdd(idx, tile) {
|
|
|
|
assert(tile.entry.n, 7);
|
|
|
|
receivedAdd = true;
|
|
|
|
},
|
|
|
|
onUpdate(idx, tile) {
|
2020-03-21 18:58:09 +05:30
|
|
|
if (tile.entry.n === 7) {
|
|
|
|
assert(!receivedAdd, "receiving update before add");
|
|
|
|
}
|
2020-03-21 18:27:41 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
entries.insert(1, {n: 7});
|
|
|
|
assert(receivedAdd);
|
|
|
|
},
|
2021-05-31 15:26:41 +05:30
|
|
|
"emit update with correct index in updatePreviousSibling during remove": assert => {
|
|
|
|
class UpdateOnSiblingTile extends TestTile {
|
|
|
|
updatePreviousSibling() {
|
|
|
|
this.update?.(this, "previous");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const entries = new ObservableArray([{n: 5}, {n: 10}, {n: 15}]);
|
|
|
|
const tiles = new TilesCollection(entries, entry => new UpdateOnSiblingTile(entry));
|
|
|
|
const events = [];
|
|
|
|
tiles.subscribe({
|
|
|
|
onUpdate(idx, tile) {
|
|
|
|
assert.equal(idx, 1);
|
|
|
|
assert.equal(tile.entry.n, 15);
|
|
|
|
events.push("update");
|
|
|
|
},
|
|
|
|
onRemove(idx, tile) {
|
|
|
|
assert.equal(idx, 1);
|
|
|
|
assert.equal(tile.entry.n, 10);
|
|
|
|
events.push("remove");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
entries.remove(1);
|
|
|
|
assert.deepEqual(events, ["remove", "update"]);
|
|
|
|
}
|
2020-03-21 18:27:41 +05:30
|
|
|
}
|
|
|
|
}
|