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

86 lines
2.9 KiB
JavaScript
Raw Normal View History

import { SortedArray, MappedList, ConcatList } from "../../../observable/index.js";
import Direction from "./Direction.js";
import GapWriter from "./persistence/GapWriter.js";
2019-05-19 20:49:46 +02:00
import TimelineReader from "./persistence/TimelineReader.js";
import PendingEventEntry from "./entries/PendingEventEntry.js";
export default class Timeline {
constructor({roomId, storage, closeCallback, fragmentIdComparer, pendingEvents, user, hsApi}) {
this._roomId = roomId;
this._storage = storage;
this._closeCallback = closeCallback;
this._fragmentIdComparer = fragmentIdComparer;
2019-06-02 15:46:24 +02:00
this._hsApi = hsApi;
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
});
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 17:29:33 +02:00
const entries = await this._timelineReader.readFromEnd(50);
this._remoteEntries.setManySorted(entries);
}
/** @package */
appendLiveEntries(newEntries) {
try {
this._remoteEntries.setManySorted(newEntries);
} catch (err) {
console.error("error while appending live entries in roomId", this._roomId);
console.error(err.stack);
throw err;
}
}
/** @public */
async fillGap(fragmentEntry, amount) {
2019-03-09 00:41:06 +01:00
const response = await this._hsApi.messages(this._roomId, {
from: fragmentEntry.token,
dir: fragmentEntry.direction.asApiString(),
2019-10-12 20:24:09 +02:00
limit: amount,
filter: {lazy_load_members: true}
2019-06-02 15:46:24 +02:00
}).response();
const gapWriter = new GapWriter({
roomId: this._roomId,
storage: this._storage,
fragmentIdComparer: this._fragmentIdComparer
});
2019-06-02 15:46:24 +02:00
const newEntries = await gapWriter.writeFragmentFill(fragmentEntry, response);
this._remoteEntries.setManySorted(newEntries);
2019-03-09 00:41:06 +01:00
}
// tries to prepend `amount` entries to the `entries` list.
2019-03-09 00:41:06 +01:00
async loadAtTop(amount) {
const firstEventEntry = this._remoteEntries.array.find(e => !!e.eventType);
2019-06-16 17:29:33 +02:00
if (!firstEventEntry) {
return;
2019-03-09 00:41:06 +01:00
}
const entries = await this._timelineReader.readFrom(
2019-06-16 17:29:33 +02:00
firstEventEntry.asEventKey(),
Direction.Backward,
amount
);
this._remoteEntries.setManySorted(entries);
}
/** @public */
get entries() {
return this._allEntries;
}
/** @public */
close() {
this._closeCallback();
}
}