This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
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-02 18:29:30 +05:30
const entries = await this._timelineReader.readFromEnd(100);
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) {
if (this._entriesList.length() === 0) {
return;
2019-03-09 05:11:06 +05:30
}
const firstEntry = this._entriesList.array()[0];
const entries = await this._timelineReader.readFrom(
firstEntry.asEventKey(),
Direction.Backward,
amount
);
this._entriesList.setManySorted(entries);
}
/** @public */
get entries() {
return this._entriesList;
}
/** @public */
close() {
this._closeCallback();
}
}