This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/room/persister.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-02-11 01:55:29 +05:30
import SortKey from "../storage/sortkey.js";
export default class RoomPersister {
2019-02-04 02:47:24 +05:30
constructor(roomId) {
this._roomId = roomId;
2019-02-11 01:55:29 +05:30
this._lastSortKey = new SortKey();
2019-02-04 02:47:24 +05:30
}
2019-02-11 01:55:29 +05:30
async load(txn) {
//fetch key here instead?
const [lastEvent] = await txn.roomTimeline.lastEvents(this._roomId, 1);
2019-02-04 02:47:24 +05:30
if (lastEvent) {
2019-02-11 01:55:29 +05:30
console.log("room persister load", this._roomId, lastEvent);
this._lastSortKey = new SortKey(lastEvent.sortKey);
2019-02-04 02:47:24 +05:30
}
}
2019-02-11 01:55:29 +05:30
// async persistGapFill(...) {
2019-02-04 02:47:24 +05:30
2019-02-11 01:55:29 +05:30
// }
2019-02-04 02:47:24 +05:30
async persistSync(roomResponse, txn) {
2019-02-11 01:55:29 +05:30
let nextKey = this._lastSortKey;
2019-02-04 02:47:24 +05:30
const timeline = roomResponse.timeline;
// is limited true for initial sync???? or do we need to handle that as a special case?
2019-02-11 01:55:29 +05:30
// I suppose it will, yes
2019-02-04 02:47:24 +05:30
if (timeline.limited) {
2019-02-11 01:55:29 +05:30
nextKey = nextKey.nextKeyWithGap();
txn.roomTimeline.appendGap(this._roomId, nextKey, {prev_batch: timeline.prev_batch});
2019-02-04 02:47:24 +05:30
}
2019-02-11 01:55:29 +05:30
// const startOfChunkSortKey = nextKey;
2019-02-04 02:47:24 +05:30
if (timeline.events) {
for(const event of timeline.events) {
nextKey = nextKey.nextKey();
2019-02-11 01:55:29 +05:30
txn.roomTimeline.appendEvent(this._roomId, nextKey, event);
2019-02-04 02:47:24 +05:30
}
}
2019-02-11 01:55:29 +05:30
// right thing to do? if the txn fails, not sure we'll continue anyways ...
// only advance the key once the transaction has
// succeeded
txn.complete().then(() => {
console.log("txn complete, setting key");
this._lastSortKey = nextKey;
});
2019-02-07 03:36:47 +05:30
// persist state
const state = roomResponse.state;
if (state.events) {
2019-02-11 01:55:29 +05:30
for (const event of state.events) {
txn.roomState.setStateEvent(this._roomId, event)
}
2019-02-07 03:36:47 +05:30
}
2019-02-04 02:47:24 +05:30
}
}