vector-im-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-19 20:49:46 +02:00
import TimelineReader from "./persistence/TimelineReader.js";
export default class Timeline {
2019-06-02 15:46:24 +02:00
constructor({roomId, storage, closeCallback, fragmentIdComparer, 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._entriesList = 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
});
}
/** @package */
async load() {
2019-06-16 17:29:33 +02:00
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 00:41:06 +01:00
const response = await this._hsApi.messages(this._roomId, {
from: fragmentEntry.token,
dir: fragmentEntry.direction.asApiString(),
limit: amount
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._entriesList.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) {
2019-06-16 17:29:33 +02:00
const firstEventEntry = this._entriesList.array.find(e => !!e.event);
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._entriesList.setManySorted(entries);
}
/** @public */
get entries() {
return this._entriesList;
}
/** @public */
close() {
this._closeCallback();
}
}