2019-06-01 17:39:23 +02:00
|
|
|
import { SortedArray } from "../../../observable/index.js";
|
|
|
|
import Direction from "./Direction.js";
|
2019-06-01 15:40:21 +02:00
|
|
|
import GapWriter from "./persistence/GapWriter.js";
|
2019-05-19 20:49:46 +02:00
|
|
|
import TimelineReader from "./persistence/TimelineReader.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
|
|
|
export default class Timeline {
|
2019-06-02 15:46:24 +02:00
|
|
|
constructor({roomId, storage, closeCallback, fragmentIdComparer, hsApi}) {
|
2019-02-27 22:50:08 +01:00
|
|
|
this._roomId = roomId;
|
|
|
|
this._storage = storage;
|
|
|
|
this._closeCallback = closeCallback;
|
2019-05-12 20:26:03 +02:00
|
|
|
this._fragmentIdComparer = fragmentIdComparer;
|
2019-06-02 15:46:24 +02:00
|
|
|
this._hsApi = hsApi;
|
2019-06-01 17:39:23 +02:00
|
|
|
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
|
|
|
|
});
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
|
|
|
async load() {
|
2019-06-02 14:59:30 +02:00
|
|
|
const entries = await this._timelineReader.readFromEnd(100);
|
2019-06-01 17:39:23 +02:00
|
|
|
this._entriesList.setManySorted(entries);
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
|
|
|
appendLiveEntries(newEntries) {
|
2019-06-01 17:39:23 +02:00
|
|
|
this._entriesList.setManySorted(newEntries);
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:03:18 +01:00
|
|
|
/** @public */
|
2019-05-12 20:26:03 +02:00
|
|
|
async fillGap(fragmentEntry, amount) {
|
2019-03-09 00:41:06 +01:00
|
|
|
const response = await this._hsApi.messages(this._roomId, {
|
2019-05-12 20:26:03 +02:00
|
|
|
from: fragmentEntry.token,
|
|
|
|
dir: fragmentEntry.direction.asApiString(),
|
2019-03-08 20:03:18 +01:00
|
|
|
limit: amount
|
2019-06-02 15:46:24 +02:00
|
|
|
}).response();
|
2019-06-01 15:40:21 +02:00
|
|
|
const gapWriter = new GapWriter({
|
2019-05-12 20:26:03 +02:00
|
|
|
roomId: this._roomId,
|
|
|
|
storage: this._storage,
|
|
|
|
fragmentIdComparer: this._fragmentIdComparer
|
|
|
|
});
|
2019-06-02 15:46:24 +02:00
|
|
|
const newEntries = await gapWriter.writeFragmentFill(fragmentEntry, response);
|
2019-06-01 17:39:23 +02:00
|
|
|
this._entriesList.setManySorted(newEntries);
|
2019-03-09 00:41:06 +01:00
|
|
|
}
|
2019-03-08 20:03:18 +01:00
|
|
|
|
2019-06-01 17:39:23 +02:00
|
|
|
// tries to prepend `amount` entries to the `entries` list.
|
2019-03-09 00:41:06 +01:00
|
|
|
async loadAtTop(amount) {
|
2019-06-01 17:39:23 +02:00
|
|
|
if (this._entriesList.length() === 0) {
|
|
|
|
return;
|
2019-03-09 00:41:06 +01:00
|
|
|
}
|
2019-06-01 17:39:23 +02:00
|
|
|
const firstEntry = this._entriesList.array()[0];
|
2019-06-02 15:15:26 +02:00
|
|
|
const entries = await this._timelineReader.readFrom(
|
|
|
|
firstEntry.asEventKey(),
|
|
|
|
Direction.Backward,
|
|
|
|
amount
|
|
|
|
);
|
2019-06-01 17:39:23 +02:00
|
|
|
this._entriesList.setManySorted(entries);
|
2019-03-08 20:03:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
/** @public */
|
|
|
|
get entries() {
|
|
|
|
return this._entriesList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
close() {
|
|
|
|
this._closeCallback();
|
|
|
|
}
|
|
|
|
}
|