2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
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 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 01:11:10 +05:30
|
|
|
import {Store} from "./Store.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {SessionStore} from "./stores/SessionStore.js";
|
|
|
|
import {RoomSummaryStore} from "./stores/RoomSummaryStore.js";
|
2021-04-20 16:32:50 +05:30
|
|
|
import {InviteStore} from "./stores/InviteStore.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {TimelineEventStore} from "./stores/TimelineEventStore.js";
|
|
|
|
import {RoomStateStore} from "./stores/RoomStateStore.js";
|
2020-06-27 02:56:24 +05:30
|
|
|
import {RoomMemberStore} from "./stores/RoomMemberStore.js";
|
2020-04-21 00:56:39 +05:30
|
|
|
import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js";
|
|
|
|
import {PendingEventStore} from "./stores/PendingEventStore.js";
|
2020-08-31 18:08:03 +05:30
|
|
|
import {UserIdentityStore} from "./stores/UserIdentityStore.js";
|
|
|
|
import {DeviceIdentityStore} from "./stores/DeviceIdentityStore.js";
|
2020-09-01 21:29:59 +05:30
|
|
|
import {OlmSessionStore} from "./stores/OlmSessionStore.js";
|
2020-09-02 17:54:38 +05:30
|
|
|
import {InboundGroupSessionStore} from "./stores/InboundGroupSessionStore.js";
|
2020-09-03 21:19:20 +05:30
|
|
|
import {OutboundGroupSessionStore} from "./stores/OutboundGroupSessionStore.js";
|
2020-09-04 19:01:00 +05:30
|
|
|
import {GroupSessionDecryptionStore} from "./stores/GroupSessionDecryptionStore.js";
|
2020-09-11 18:10:05 +05:30
|
|
|
import {OperationStore} from "./stores/OperationStore.js";
|
2020-09-17 14:09:51 +05:30
|
|
|
import {AccountDataStore} from "./stores/AccountDataStore.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;
|
2020-09-14 19:14:34 +05:30
|
|
|
this._stores = {};
|
2019-06-27 02:01:36 +05:30
|
|
|
}
|
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));
|
|
|
|
}
|
2021-05-04 17:04:42 +05:30
|
|
|
|
|
|
|
get archivedRoomSummary() {
|
|
|
|
return this._store("archivedRoomSummary", idbStore => new RoomSummaryStore(idbStore));
|
|
|
|
}
|
2019-02-11 01:55:29 +05:30
|
|
|
|
2021-04-20 16:32:50 +05:30
|
|
|
get invites() {
|
|
|
|
return this._store("invites", idbStore => new InviteStore(idbStore));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-06-27 02:56:24 +05:30
|
|
|
get roomMembers() {
|
|
|
|
return this._store("roomMembers", idbStore => new RoomMemberStore(idbStore));
|
|
|
|
}
|
|
|
|
|
2019-07-27 14:10:56 +05:30
|
|
|
get pendingEvents() {
|
|
|
|
return this._store("pendingEvents", idbStore => new PendingEventStore(idbStore));
|
|
|
|
}
|
|
|
|
|
2020-08-31 18:08:03 +05:30
|
|
|
get userIdentities() {
|
|
|
|
return this._store("userIdentities", idbStore => new UserIdentityStore(idbStore));
|
|
|
|
}
|
|
|
|
|
|
|
|
get deviceIdentities() {
|
|
|
|
return this._store("deviceIdentities", idbStore => new DeviceIdentityStore(idbStore));
|
|
|
|
}
|
|
|
|
|
2020-09-01 21:29:59 +05:30
|
|
|
get olmSessions() {
|
|
|
|
return this._store("olmSessions", idbStore => new OlmSessionStore(idbStore));
|
|
|
|
}
|
2020-09-02 17:54:38 +05:30
|
|
|
|
|
|
|
get inboundGroupSessions() {
|
|
|
|
return this._store("inboundGroupSessions", idbStore => new InboundGroupSessionStore(idbStore));
|
|
|
|
}
|
2020-09-03 21:19:20 +05:30
|
|
|
|
|
|
|
get outboundGroupSessions() {
|
|
|
|
return this._store("outboundGroupSessions", idbStore => new OutboundGroupSessionStore(idbStore));
|
|
|
|
}
|
2020-09-04 19:01:00 +05:30
|
|
|
|
|
|
|
get groupSessionDecryptions() {
|
2020-09-04 20:10:51 +05:30
|
|
|
return this._store("groupSessionDecryptions", idbStore => new GroupSessionDecryptionStore(idbStore));
|
2020-09-04 19:01:00 +05:30
|
|
|
}
|
|
|
|
|
2020-09-11 18:10:05 +05:30
|
|
|
get operations() {
|
|
|
|
return this._store("operations", idbStore => new OperationStore(idbStore));
|
|
|
|
}
|
|
|
|
|
2020-09-17 14:09:51 +05:30
|
|
|
get accountData() {
|
|
|
|
return this._store("accountData", idbStore => new AccountDataStore(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() {
|
2021-02-12 01:38:06 +05:30
|
|
|
// TODO: should we wrap the exception in a StorageError?
|
2019-06-27 02:01:36 +05:30
|
|
|
this._txn.abort();
|
|
|
|
}
|
2019-05-12 23:54:06 +05:30
|
|
|
}
|