2019-02-07 01:20:27 +01:00
|
|
|
import {txnAsPromise} from "./utils.js";
|
2019-06-26 22:00:50 +02:00
|
|
|
import {StorageError} from "../common.js";
|
2019-02-05 00:21:50 +01:00
|
|
|
import Store from "./store.js";
|
2019-02-10 21:25:29 +01:00
|
|
|
import SessionStore from "./stores/SessionStore.js";
|
|
|
|
import RoomSummaryStore from "./stores/RoomSummaryStore.js";
|
2019-05-12 20:24:06 +02:00
|
|
|
import TimelineEventStore from "./stores/TimelineEventStore.js";
|
2019-02-10 21:25:29 +01:00
|
|
|
import RoomStateStore from "./stores/RoomStateStore.js";
|
2019-05-12 20:24:06 +02:00
|
|
|
import TimelineFragmentStore from "./stores/TimelineFragmentStore.js";
|
2019-02-05 00:21:50 +01:00
|
|
|
|
|
|
|
export default class Transaction {
|
|
|
|
constructor(txn, allowedStoreNames) {
|
|
|
|
this._txn = txn;
|
|
|
|
this._allowedStoreNames = allowedStoreNames;
|
|
|
|
this._stores = {
|
2019-02-07 00:19:14 +01:00
|
|
|
session: null,
|
|
|
|
roomSummary: null,
|
|
|
|
roomTimeline: null,
|
|
|
|
roomState: null,
|
2019-02-05 00:21:50 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-07 00:19:14 +01:00
|
|
|
_idbStore(name) {
|
2019-02-05 00:21:50 +01:00
|
|
|
if (!this._allowedStoreNames.includes(name)) {
|
|
|
|
// more specific error? this is a bug, so maybe not ...
|
2019-06-26 22:00:50 +02:00
|
|
|
throw new StorageError(`Invalid store for transaction: ${name}, only ${this._allowedStoreNames.join(", ")} are allowed.`);
|
2019-02-05 00:21:50 +01:00
|
|
|
}
|
2019-02-07 01:20:27 +01:00
|
|
|
return new Store(this._txn.objectStore(name));
|
2019-02-05 00:21:50 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 01:20:27 +01:00
|
|
|
_store(name, mapStore) {
|
|
|
|
if (!this._stores[name]) {
|
|
|
|
const idbStore = this._idbStore(name);
|
|
|
|
this._stores[name] = mapStore(idbStore);
|
2019-02-05 00:21:50 +01:00
|
|
|
}
|
2019-02-07 01:20:27 +01:00
|
|
|
return this._stores[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
get session() {
|
|
|
|
return this._store("session", idbStore => new SessionStore(idbStore));
|
2019-02-05 00:21:50 +01:00
|
|
|
}
|
|
|
|
|
2019-02-10 21:25:29 +01:00
|
|
|
get roomSummary() {
|
|
|
|
return this._store("roomSummary", idbStore => new RoomSummaryStore(idbStore));
|
|
|
|
}
|
|
|
|
|
2019-05-12 20:24:06 +02:00
|
|
|
get timelineFragments() {
|
|
|
|
return this._store("timelineFragments", idbStore => new TimelineFragmentStore(idbStore));
|
|
|
|
}
|
|
|
|
|
|
|
|
get timelineEvents() {
|
|
|
|
return this._store("timelineEvents", idbStore => new TimelineEventStore(idbStore));
|
2019-02-10 21:25:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get roomState() {
|
|
|
|
return this._store("roomState", idbStore => new RoomStateStore(idbStore));
|
|
|
|
}
|
|
|
|
|
2019-02-05 00:21:50 +01:00
|
|
|
complete() {
|
|
|
|
return txnAsPromise(this._txn);
|
|
|
|
}
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
this._txn.abort();
|
|
|
|
}
|
2019-05-12 20:24:06 +02:00
|
|
|
}
|