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

72 lines
2.2 KiB
JavaScript
Raw Normal View History

import { SortedArray } from "../../../observable/index.js";
import Direction from "./Direction.js";
import GapWriter from "./persistence/GapWriter.js";
2019-05-20 00:19:46 +05:30
import TimelineReader from "./persistence/TimelineReader.js";
export default class Timeline {
2019-06-02 19:16:24 +05:30
constructor({roomId, storage, closeCallback, fragmentIdComparer, hsApi}) {
this._roomId = roomId;
this._storage = storage;
this._closeCallback = closeCallback;
this._fragmentIdComparer = fragmentIdComparer;
2019-06-02 19:16:24 +05:30
this._hsApi = hsApi;
this._entriesList = 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
});
}
/** @package */
async load() {
2019-06-16 20:59:33 +05:30
const entries = await this._timelineReader.readFromEnd(50);
this._entriesList.setManySorted(entries);
}
/** @package */
appendLiveEntries(newEntries) {
this._entriesList.setManySorted(newEntries);
}
/** @public */
async fillGap(fragmentEntry, amount) {
2019-03-09 05:11:06 +05:30
const response = await this._hsApi.messages(this._roomId, {
from: fragmentEntry.token,
dir: fragmentEntry.direction.asApiString(),
limit: amount
2019-06-02 19:16:24 +05:30
}).response();
const gapWriter = new GapWriter({
roomId: this._roomId,
storage: this._storage,
fragmentIdComparer: this._fragmentIdComparer
});
2019-06-02 19:16:24 +05:30
const newEntries = await gapWriter.writeFragmentFill(fragmentEntry, response);
this._entriesList.setManySorted(newEntries);
2019-03-09 05:11:06 +05:30
}
// tries to prepend `amount` entries to the `entries` list.
2019-03-09 05:11:06 +05:30
async loadAtTop(amount) {
2019-06-16 20:59:33 +05:30
const firstEventEntry = this._entriesList.array.find(e => !!e.event);
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._entriesList.setManySorted(entries);
}
/** @public */
get entries() {
return this._entriesList;
}
/** @public */
close() {
this._closeCallback();
}
}