hydrogen-web/src/storage/idb/stores/timeline.js

62 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-02-07 05:50:27 +05:30
import SortKey from "../../sortkey.js";
class TimelineStore {
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) {
return this.eventsBefore(roomId, GapSortKey.maxKey());
}
2019-02-04 02:47:24 +05:30
async firstEvents(roomId, amount) {
return this.eventsAfter(roomId, GapSortKey.minKey());
}
2019-02-04 02:47:24 +05:30
eventsAfter(roomId, sortKey, amount) {
const range = IDBKeyRange.lowerBound([roomId, sortKey], true);
return this._timelineStore.selectLimit(range, amount);
}
2019-02-04 02:47:24 +05:30
async eventsBefore(roomId, sortKey, amount) {
const range = IDBKeyRange.upperBound([roomId, sortKey], true);
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,
sortKey: sortKey,
2019-02-04 02:47:24 +05:30
content: {
event: null,
gap: gap,
},
});
}
appendEvent(roomId, sortKey, event) {
this._timelineStore.add({
roomId: roomId,
sortKey: sortKey,
2019-02-04 02:47:24 +05:30
content: {
event: event,
gap: null,
},
});
}
async removeEvent(roomId, sortKey) {
this._timelineStore.delete([roomId, sortKey]);
}
}