2019-06-01 21:09:23 +05:30
|
|
|
import { SortedArray } from "../../../observable/index.js";
|
|
|
|
import Direction from "./Direction.js";
|
2019-06-01 19:10:21 +05:30
|
|
|
import GapWriter from "./persistence/GapWriter.js";
|
2019-05-20 00:19:46 +05:30
|
|
|
import TimelineReader from "./persistence/TimelineReader.js";
|
2019-02-28 03:20:08 +05:30
|
|
|
|
|
|
|
export default class Timeline {
|
2019-06-02 19:16:24 +05:30
|
|
|
constructor({roomId, storage, closeCallback, fragmentIdComparer, hsApi}) {
|
2019-02-28 03:20:08 +05:30
|
|
|
this._roomId = roomId;
|
|
|
|
this._storage = storage;
|
|
|
|
this._closeCallback = closeCallback;
|
2019-05-12 23:56:03 +05:30
|
|
|
this._fragmentIdComparer = fragmentIdComparer;
|
2019-06-02 19:16:24 +05:30
|
|
|
this._hsApi = hsApi;
|
2019-06-01 21:09:23 +05:30
|
|
|
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
|
|
|
|
});
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
|
|
|
async load() {
|
2019-06-16 20:59:33 +05:30
|
|
|
const entries = await this._timelineReader.readFromEnd(50);
|
2019-06-01 21:09:23 +05:30
|
|
|
this._entriesList.setManySorted(entries);
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
|
|
|
appendLiveEntries(newEntries) {
|
2019-06-01 21:09:23 +05:30
|
|
|
this._entriesList.setManySorted(newEntries);
|
2019-02-28 03:20:08 +05:30
|
|
|
}
|
|
|
|
|
2019-03-09 00:33:18 +05:30
|
|
|
/** @public */
|
2019-05-12 23:56:03 +05:30
|
|
|
async fillGap(fragmentEntry, amount) {
|
2019-03-09 05:11:06 +05:30
|
|
|
const response = await this._hsApi.messages(this._roomId, {
|
2019-05-12 23:56:03 +05:30
|
|
|
from: fragmentEntry.token,
|
|
|
|
dir: fragmentEntry.direction.asApiString(),
|
2019-03-09 00:33:18 +05:30
|
|
|
limit: amount
|
2019-06-02 19:16:24 +05:30
|
|
|
}).response();
|
2019-06-01 19:10:21 +05:30
|
|
|
const gapWriter = new GapWriter({
|
2019-05-12 23:56:03 +05:30
|
|
|
roomId: this._roomId,
|
|
|
|
storage: this._storage,
|
|
|
|
fragmentIdComparer: this._fragmentIdComparer
|
|
|
|
});
|
2019-06-02 19:16:24 +05:30
|
|
|
const newEntries = await gapWriter.writeFragmentFill(fragmentEntry, response);
|
2019-06-01 21:09:23 +05:30
|
|
|
this._entriesList.setManySorted(newEntries);
|
2019-03-09 05:11:06 +05:30
|
|
|
}
|
2019-03-09 00:33:18 +05:30
|
|
|
|
2019-06-01 21:09:23 +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) {
|
2019-06-01 21:09:23 +05:30
|
|
|
return;
|
2019-03-09 05:11:06 +05:30
|
|
|
}
|
2019-06-02 18:45:26 +05:30
|
|
|
const entries = await this._timelineReader.readFrom(
|
2019-06-16 20:59:33 +05:30
|
|
|
firstEventEntry.asEventKey(),
|
2019-06-02 18:45:26 +05:30
|
|
|
Direction.Backward,
|
|
|
|
amount
|
|
|
|
);
|
2019-06-01 21:09:23 +05:30
|
|
|
this._entriesList.setManySorted(entries);
|
2019-03-09 00:33:18 +05:30
|
|
|
}
|
|
|
|
|
2019-02-28 03:20:08 +05:30
|
|
|
/** @public */
|
|
|
|
get entries() {
|
|
|
|
return this._entriesList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @public */
|
|
|
|
close() {
|
|
|
|
this._closeCallback();
|
|
|
|
}
|
|
|
|
}
|