vector-im-hydrogen-web/src/matrix/storage/idb/Transaction.js

71 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-02-07 01:20:27 +01:00
import {txnAsPromise} from "./utils.js";
import {StorageError} from "../common.js";
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 00:21:50 +01:00
export class Transaction {
2019-06-26 22:31:36 +02:00
constructor(txn, allowedStoreNames) {
this._txn = txn;
this._allowedStoreNames = allowedStoreNames;
this._stores = {
session: null,
roomSummary: null,
roomTimeline: null,
roomState: null,
};
}
2019-02-05 00:21:50 +01:00
2019-06-26 22:31:36 +02:00
_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 00:21:50 +01:00
2019-06-26 22:31:36 +02:00
_store(name, mapStore) {
if (!this._stores[name]) {
const idbStore = this._idbStore(name);
this._stores[name] = mapStore(idbStore);
}
return this._stores[name];
}
2019-02-07 01:20:27 +01:00
2019-06-26 22:31:36 +02:00
get session() {
return this._store("session", idbStore => new SessionStore(idbStore));
}
2019-02-05 00:21:50 +01:00
2019-06-26 22:31:36 +02:00
get roomSummary() {
return this._store("roomSummary", idbStore => new RoomSummaryStore(idbStore));
}
2019-02-10 21:25:29 +01:00
get timelineFragments() {
return this._store("timelineFragments", idbStore => new TimelineFragmentStore(idbStore));
}
2019-06-26 22:31:36 +02:00
get timelineEvents() {
return this._store("timelineEvents", idbStore => new TimelineEventStore(idbStore));
}
2019-02-10 21:25:29 +01:00
2019-06-26 22:31:36 +02:00
get roomState() {
return this._store("roomState", idbStore => new RoomStateStore(idbStore));
}
2019-02-10 21:25:29 +01:00
get pendingEvents() {
return this._store("pendingEvents", idbStore => new PendingEventStore(idbStore));
}
2019-06-26 22:31:36 +02:00
complete() {
return txnAsPromise(this._txn);
}
2019-02-05 00:21:50 +01:00
2019-06-26 22:31:36 +02:00
abort() {
this._txn.abort();
}
}