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/matrix/storage/idb/stores/RoomTimelineStore.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-02-07 05:50:27 +05:30
import SortKey from "../../sortkey.js";
2019-02-11 01:55:29 +05:30
export default class RoomTimelineStore {
2019-02-04 02:47:24 +05:30
constructor(timelineStore) {
this._timelineStore = timelineStore;
}
2019-02-04 02:47:24 +05:30
async lastEvents(roomId, amount) {
2019-02-11 01:55:29 +05:30
return this.eventsBefore(roomId, SortKey.maxKey, amount);
}
2019-02-04 02:47:24 +05:30
async firstEvents(roomId, amount) {
2019-02-11 01:55:29 +05:30
return this.eventsAfter(roomId, SortKey.minKey, amount);
}
2019-02-04 02:47:24 +05:30
eventsAfter(roomId, sortKey, amount) {
const range = IDBKeyRange.bound([roomId, sortKey.buffer], [roomId, SortKey.maxKey.buffer], true, false);
2019-02-04 02:47:24 +05:30
return this._timelineStore.selectLimit(range, amount);
}
2019-02-04 02:47:24 +05:30
async eventsBefore(roomId, sortKey, amount) {
const range = IDBKeyRange.bound([roomId, SortKey.minKey.buffer], [roomId, sortKey.buffer], false, true);
2019-02-04 02:47:24 +05:30
const events = await this._timelineStore.selectLimitReverse(range, amount);
events.reverse(); // because we fetched them backwards
return events;
}
// should this happen as part of a transaction that stores all synced in changes?
// e.g.:
// - timeline events for all rooms
// - latest sync token
// - new members
// - new room state
// - updated/new account data
2019-02-04 02:47:24 +05:30
appendGap(roomId, sortKey, gap) {
this._timelineStore.add({
roomId: roomId,
2019-02-11 01:55:29 +05:30
sortKey: sortKey.buffer,
2019-02-11 02:32:42 +05:30
event: null,
gap: gap,
2019-02-04 02:47:24 +05:30
});
}
appendEvent(roomId, sortKey, event) {
2019-02-11 01:55:29 +05:30
console.info(`appending event for room ${roomId} with key ${sortKey}`);
2019-02-04 02:47:24 +05:30
this._timelineStore.add({
roomId: roomId,
2019-02-11 01:55:29 +05:30
sortKey: sortKey.buffer,
2019-02-11 02:32:42 +05:30
event: event,
gap: null,
2019-02-04 02:47:24 +05:30
});
}
2019-02-18 04:28:01 +05:30
// could be gap or event
async removeEntry(roomId, sortKey) {
2019-02-11 01:55:29 +05:30
this._timelineStore.delete([roomId, sortKey.buffer]);
}
}