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