hydrogen-web/src/storage/idb/transaction.js

40 lines
980 B
JavaScript
Raw Normal View History

2019-02-05 04:51:50 +05:30
import {txnAsPromise} from "./utils";
import Store from "./store.js";
import TimelineStore from "./stores/timeline.js";
export default class Transaction {
constructor(txn, allowedStoreNames) {
this._txn = txn;
this._allowedStoreNames = allowedStoreNames;
this._stores = {
2019-02-07 04:49:14 +05:30
session: null,
roomSummary: null,
roomTimeline: null,
roomState: null,
2019-02-05 04:51:50 +05:30
};
}
2019-02-07 04:49:14 +05:30
_idbStore(name) {
2019-02-05 04:51:50 +05:30
if (!this._allowedStoreNames.includes(name)) {
// more specific error? this is a bug, so maybe not ...
throw new Error(`Invalid store for transaction: ${name}, only ${this._allowedStoreNames.join(", ")} are allowed.`);
}
return new Store(this._txn.getObjectStore(name));
}
2019-02-07 04:49:14 +05:30
get roomTimeline() {
if (!this._stores.roomTimeline) {
const idbStore = this._idbStore("roomTimeline");
this._stores.roomTimeline = new TimelineStore(idbStore);
2019-02-05 04:51:50 +05:30
}
2019-02-07 04:49:14 +05:30
return this._stores.roomTimeline;
2019-02-05 04:51:50 +05:30
}
complete() {
return txnAsPromise(this._txn);
}
abort() {
this._txn.abort();
}
}