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

127 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-08-05 18:38:55 +02:00
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
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";
2020-06-26 23:26:24 +02:00
import {RoomMemberStore} from "./stores/RoomMemberStore.js";
import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js";
import {PendingEventStore} from "./stores/PendingEventStore.js";
2020-08-31 14:38:03 +02:00
import {UserIdentityStore} from "./stores/UserIdentityStore.js";
import {DeviceIdentityStore} from "./stores/DeviceIdentityStore.js";
2020-09-01 17:59:59 +02:00
import {OlmSessionStore} from "./stores/OlmSessionStore.js";
2020-09-02 14:24:38 +02:00
import {InboundGroupSessionStore} from "./stores/InboundGroupSessionStore.js";
2020-09-03 17:49:20 +02:00
import {OutboundGroupSessionStore} from "./stores/OutboundGroupSessionStore.js";
import {GroupSessionDecryptionStore} from "./stores/GroupSessionDecryptionStore.js";
2020-09-11 14:40:05 +02:00
import {OperationStore} from "./stores/OperationStore.js";
import {AccountDataStore} from "./stores/AccountDataStore.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 = {};
2019-06-26 22:31:36 +02:00
}
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
2020-06-26 23:26:24 +02:00
get roomMembers() {
return this._store("roomMembers", idbStore => new RoomMemberStore(idbStore));
}
get pendingEvents() {
return this._store("pendingEvents", idbStore => new PendingEventStore(idbStore));
}
2020-08-31 14:38:03 +02:00
get userIdentities() {
return this._store("userIdentities", idbStore => new UserIdentityStore(idbStore));
}
get deviceIdentities() {
return this._store("deviceIdentities", idbStore => new DeviceIdentityStore(idbStore));
}
2020-09-01 17:59:59 +02:00
get olmSessions() {
return this._store("olmSessions", idbStore => new OlmSessionStore(idbStore));
}
2020-09-02 14:24:38 +02:00
get inboundGroupSessions() {
return this._store("inboundGroupSessions", idbStore => new InboundGroupSessionStore(idbStore));
}
2020-09-03 17:49:20 +02:00
get outboundGroupSessions() {
return this._store("outboundGroupSessions", idbStore => new OutboundGroupSessionStore(idbStore));
}
get groupSessionDecryptions() {
2020-09-04 16:40:51 +02:00
return this._store("groupSessionDecryptions", idbStore => new GroupSessionDecryptionStore(idbStore));
}
2020-09-11 14:40:05 +02:00
get operations() {
return this._store("operations", idbStore => new OperationStore(idbStore));
}
get accountData() {
return this._store("accountData", idbStore => new AccountDataStore(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();
}
}