hydrogen-web/src/matrix/room/timeline/Timeline.js

69 lines
2.2 KiB
JavaScript
Raw Normal View History

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";
export class Timeline {
2020-03-22 04:10:40 +05:30
constructor({roomId, storage, closeCallback, fragmentIdComparer, pendingEvents, user}) {
this._roomId = roomId;
this._storage = storage;
this._closeCallback = closeCallback;
this._fragmentIdComparer = fragmentIdComparer;
this._remoteEntries = new SortedArray((a, b) => a.compare(b));
2019-05-20 00:19:46 +05:30
this._timelineReader = new TimelineReader({
roomId: this._roomId,
storage: this._storage,
fragmentIdComparer: this._fragmentIdComparer
});
const localEntries = new MappedList(pendingEvents, pe => {
return new PendingEventEntry({pendingEvent: pe, user});
}, (pee, params) => {
pee.notifyUpdate(params);
});
this._allEntries = new ConcatList(this._remoteEntries, localEntries);
}
/** @package */
async load() {
2019-06-16 20:59:33 +05:30
const entries = await this._timelineReader.readFromEnd(50);
this._remoteEntries.setManySorted(entries);
}
/** @package */
appendLiveEntries(newEntries) {
this._remoteEntries.setManySorted(newEntries);
}
2020-03-22 04:10:40 +05:30
/** @package */
addGapEntries(newEntries) {
this._remoteEntries.setManySorted(newEntries);
2019-03-09 05:11:06 +05:30
}
2020-03-22 04:10:40 +05:30
// tries to prepend `amount` entries to the `entries` list.
2019-03-09 05:11:06 +05:30
async loadAtTop(amount) {
const firstEventEntry = this._remoteEntries.array.find(e => !!e.eventType);
2019-06-16 20:59:33 +05:30
if (!firstEventEntry) {
return;
2019-03-09 05:11:06 +05:30
}
const entries = await this._timelineReader.readFrom(
2019-06-16 20:59:33 +05:30
firstEventEntry.asEventKey(),
Direction.Backward,
amount
);
this._remoteEntries.setManySorted(entries);
}
/** @public */
get entries() {
return this._allEntries;
}
/** @public */
close() {
2020-05-05 01:51:56 +05:30
if (this._closeCallback) {
this._closeCallback();
this._closeCallback = null;
}
}
}