2019-02-27 22:50:08 +01:00
|
|
|
import { ObservableArray } from "../../observable/index.js";
|
2019-03-09 00:41:06 +01:00
|
|
|
import sortedIndex from "../../utils/sortedIndex.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
|
|
|
export default class Timeline {
|
|
|
|
constructor({roomId, storage, closeCallback}) {
|
|
|
|
this._roomId = roomId;
|
|
|
|
this._storage = storage;
|
|
|
|
this._closeCallback = closeCallback;
|
|
|
|
this._entriesList = new ObservableArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
|
|
|
async load() {
|
|
|
|
const txn = await this._storage.readTxn([this._storage.storeNames.roomTimeline]);
|
|
|
|
const entries = await txn.roomTimeline.lastEvents(this._roomId, 100);
|
|
|
|
for (const entry of entries) {
|
|
|
|
this._entriesList.append(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @package */
|
|
|
|
appendLiveEntries(newEntries) {
|
|
|
|
for (const entry of newEntries) {
|
|
|
|
this._entriesList.append(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 20:03:18 +01:00
|
|
|
/** @public */
|
|
|
|
async fillGap(gapEntry, amount) {
|
|
|
|
const gap = gapEntry.gap;
|
|
|
|
let direction;
|
|
|
|
if (gap.prev_batch) {
|
|
|
|
direction = "b";
|
|
|
|
} else if (gap.next_batch) {
|
|
|
|
direction = "f";
|
|
|
|
} else {
|
|
|
|
throw new Error("Invalid gap, no prev_batch or next_batch field: " + JSON.stringify(gapEntry.gap));
|
|
|
|
}
|
|
|
|
const token = gap.prev_batch || gap.next_batch;
|
|
|
|
|
2019-03-09 00:41:06 +01:00
|
|
|
const response = await this._hsApi.messages(this._roomId, {
|
2019-03-08 20:03:18 +01:00
|
|
|
from: token,
|
|
|
|
dir: direction,
|
|
|
|
limit: amount
|
|
|
|
});
|
2019-03-09 00:41:06 +01:00
|
|
|
|
2019-03-08 20:03:18 +01:00
|
|
|
const newEntries = await this._persister.persistGapFill(gapEntry, response);
|
|
|
|
// find where to replace existing gap with newEntries by doing binary search
|
2019-03-09 00:41:06 +01:00
|
|
|
const gapIdx = sortedIndex(this._entriesList.array, gapEntry.sortKey, (key, entry) => {
|
|
|
|
return key.compare(entry.sortKey);
|
|
|
|
});
|
|
|
|
// only replace the gap if it's currently in the timeline
|
|
|
|
if (this._entriesList.at(gapIdx) === gapEntry) {
|
|
|
|
this._entriesList.removeAt(gapIdx);
|
|
|
|
this._entriesList.insertMany(gapIdx, newEntries);
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 20:03:18 +01:00
|
|
|
|
2019-03-09 00:41:06 +01:00
|
|
|
async loadAtTop(amount) {
|
|
|
|
const firstEntry = this._entriesList.at(0);
|
|
|
|
if (firstEntry) {
|
|
|
|
const txn = await this._storage.readTxn([this._storage.storeNames.roomTimeline]);
|
|
|
|
const topEntries = await txn.roomTimeline.eventsBefore(this._roomId, firstEntry.sortKey, amount);
|
|
|
|
this._entriesList.insertMany(0, topEntries);
|
|
|
|
return topEntries.length;
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
}
|