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

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-02-07 05:50:27 +05:30
import {txnAsPromise} from "./utils.js";
2019-02-05 04:51:50 +05:30
import Store from "./store.js";
2019-02-11 01:55:29 +05:30
import SessionStore from "./stores/SessionStore.js";
import RoomSummaryStore from "./stores/RoomSummaryStore.js";
import RoomTimelineStore from "./stores/RoomTimelineStore.js";
import RoomStateStore from "./stores/RoomStateStore.js";
2019-02-05 04:51:50 +05:30
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.`);
}
2019-02-07 05:50:27 +05:30
return new Store(this._txn.objectStore(name));
2019-02-05 04:51:50 +05:30
}
2019-02-07 05:50:27 +05:30
_store(name, mapStore) {
if (!this._stores[name]) {
const idbStore = this._idbStore(name);
this._stores[name] = mapStore(idbStore);
2019-02-05 04:51:50 +05:30
}
2019-02-07 05:50:27 +05:30
return this._stores[name];
}
get session() {
return this._store("session", idbStore => new SessionStore(idbStore));
2019-02-05 04:51:50 +05:30
}
2019-02-11 01:55:29 +05:30
get roomSummary() {
return this._store("roomSummary", idbStore => new RoomSummaryStore(idbStore));
}
get roomTimeline() {
return this._store("roomTimeline", idbStore => new RoomTimelineStore(idbStore));
}
get roomState() {
return this._store("roomState", idbStore => new RoomStateStore(idbStore));
}
2019-02-05 04:51:50 +05:30
complete() {
return txnAsPromise(this._txn);
}
abort() {
this._txn.abort();
}
}