2020-04-20 21:26:39 +02:00
|
|
|
import {SortedArray, MappedList, ConcatList} from "../../../observable/index.js";
|
|
|
|
import {Direction} from "./Direction.js";
|
|
|
|
import {TimelineReader} from "./persistence/TimelineReader.js";
|
|
|
|
import {PendingEventEntry} from "./entries/PendingEventEntry.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
2020-04-20 21:26:39 +02:00
|
|
|
export class Timeline {
|
2020-03-21 23:40:40 +01:00
|
|
|
constructor({roomId, storage, closeCallback, fragmentIdComparer, pendingEvents, user}) {
|
2019-02-27 22:50:08 +01:00
|
|
|
this._roomId = roomId;
|
|
|
|
this._storage = storage;
|
|
|
|
this._closeCallback = closeCallback;
|
2019-05-12 20:26:03 +02:00
|
|
|
this._fragmentIdComparer = fragmentIdComparer;
|
2019-07-29 19:53:58 +02:00
|
|
|
this._remoteEntries = new SortedArray((a, b) => a.compare(b));
|
2019-05-19 20:49:46 +02:00
|
|
|
this._timelineReader = new TimelineReader({
|
|
|
|
roomId: this._roomId,
|
|
|
|
storage: this._storage,
|
|
|
|
fragmentIdComparer: this._fragmentIdComparer
|
|
|
|
});
|
2019-07-29 19:53:58 +02:00
|
|
|
const localEntries = new MappedList(pendingEvents, pe => {
|
|
|
|
return new PendingEventEntry({pendingEvent: pe, user});
|
|
|
|
}, (pee, params) => {
|
|
|
|
pee.notifyUpdate(params);
|
|
|
|
});
|
|
|
|
this._allEntries = new ConcatList(this._remoteEntries, localEntries);
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
|
|
|
async load() {
|
2019-06-16 17:29:33 +02:00
|
|
|
const entries = await this._timelineReader.readFromEnd(50);
|
2019-07-29 19:53:58 +02:00
|
|
|
this._remoteEntries.setManySorted(entries);
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2020-05-07 19:14:53 +02:00
|
|
|
// TODO: should we rather have generic methods for
|
|
|
|
// - adding new entries
|
|
|
|
// - updating existing entries (redaction, relations)
|
2019-02-27 22:50:08 +01:00
|
|
|
/** @package */
|
|
|
|
appendLiveEntries(newEntries) {
|
2020-03-21 10:47:35 +01:00
|
|
|
this._remoteEntries.setManySorted(newEntries);
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:40:40 +01:00
|
|
|
/** @package */
|
|
|
|
addGapEntries(newEntries) {
|
2019-07-29 19:53:58 +02:00
|
|
|
this._remoteEntries.setManySorted(newEntries);
|
2019-03-09 00:41:06 +01:00
|
|
|
}
|
2020-03-21 23:40:40 +01:00
|
|
|
|
2019-06-01 17:39:23 +02:00
|
|
|
// tries to prepend `amount` entries to the `entries` list.
|
2019-03-09 00:41:06 +01:00
|
|
|
async loadAtTop(amount) {
|
2019-07-29 19:58:35 +02:00
|
|
|
const firstEventEntry = this._remoteEntries.array.find(e => !!e.eventType);
|
2019-06-16 17:29:33 +02:00
|
|
|
if (!firstEventEntry) {
|
2019-06-01 17:39:23 +02:00
|
|
|
return;
|
2019-03-09 00:41:06 +01:00
|
|
|
}
|
2019-06-02 15:15:26 +02:00
|
|
|
const entries = await this._timelineReader.readFrom(
|
2019-06-16 17:29:33 +02:00
|
|
|
firstEventEntry.asEventKey(),
|
2019-06-02 15:15:26 +02:00
|
|
|
Direction.Backward,
|
|
|
|
amount
|
|
|
|
);
|
2019-07-29 19:53:58 +02:00
|
|
|
this._remoteEntries.setManySorted(entries);
|
2019-03-08 20:03:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
/** @public */
|
|
|
|
get entries() {
|
2019-07-29 19:53:58 +02:00
|
|
|
return this._allEntries;
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
close() {
|
2020-05-04 22:21:56 +02:00
|
|
|
if (this._closeCallback) {
|
|
|
|
this._closeCallback();
|
|
|
|
this._closeCallback = null;
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
}
|