draft of how to implement filling a timeline gap

This commit is contained in:
Bruno Windels 2019-03-08 20:03:18 +01:00
parent 1757a27475
commit ec925d7c49
4 changed files with 42 additions and 8 deletions

View file

@ -17,9 +17,9 @@ export default class RoomPersister {
}
}
// async persistGapFill(...) {
// }
async persistGapFill(gapEntry, response) {
throw new Error("not yet implemented");
}
persistSync(roomResponse, txn) {
let nextKey = this._lastSortKey;

View file

@ -4,10 +4,11 @@ import RoomPersister from "./persister.js";
import Timeline from "./timeline.js";
export default class Room extends EventEmitter {
constructor(roomId, storage, emitCollectionChange) {
constructor({roomId, storage, hsApi, emitCollectionChange}) {
super();
this._roomId = roomId;
this._storage = storage;
this._hsApi = hsApi;
this._summary = new RoomSummary(roomId);
this._persister = new RoomPersister(roomId);
this._emitCollectionChange = emitCollectionChange;
@ -50,6 +51,8 @@ export default class Room extends EventEmitter {
this._timeline = new Timeline({
roomId: this.id,
storage: this._storage,
persister: this._persister,
hsApi: this._hsApi,
closeCallback: () => this._timeline = null,
});
await this._timeline.load();

View file

@ -24,6 +24,30 @@ export default class Timeline {
}
}
/** @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;
const response = await this._hsApi.messages({
roomId: this._roomId,
from: token,
dir: direction,
limit: amount
});
const newEntries = await this._persister.persistGapFill(gapEntry, response);
// find where to replace existing gap with newEntries by doing binary search
}
/** @public */
get entries() {
return this._entriesList;

View file

@ -2,8 +2,10 @@ import Room from "./room/room.js";
import { ObservableMap } from "../observable/index.js";
export default class Session {
constructor({storage, sessionInfo}) {
// sessionInfo contains deviceId, userId and homeServer
constructor({storage, hsApi, sessionInfo}) {
this._storage = storage;
this._hsApi = hsApi;
this._session = null;
this._sessionInfo = sessionInfo;
this._rooms = new ObservableMap();
@ -36,7 +38,12 @@ export default class Session {
}
createRoom(roomId) {
const room = new Room(roomId, this._storage, this._roomUpdateCallback);
const room = new Room({
roomId,
storage: this._storage,
emitCollectionChange: this._roomUpdateCallback,
hsApi: this._hsApi,
});
this._rooms.add(roomId, room);
return room;
}