From 2262e6be306b89074e3edead569cd693107f4584 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 31 Aug 2021 15:44:03 -0700 Subject: [PATCH] Use store name enum for saving stores --- src/matrix/storage/idb/Transaction.ts | 47 ++++++++++++++------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/matrix/storage/idb/Transaction.ts b/src/matrix/storage/idb/Transaction.ts index b6fee2f2..73dfb7b3 100644 --- a/src/matrix/storage/idb/Transaction.ts +++ b/src/matrix/storage/idb/Transaction.ts @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +import {StoreNames} from "../common"; import {txnAsPromise} from "./utils"; import {StorageError} from "../common"; import {Store} from "./Store"; @@ -37,10 +38,10 @@ import {AccountDataStore} from "./stores/AccountDataStore"; export class Transaction { private _txn: IDBTransaction; - private _allowedStoreNames: string[]; - private _stores: { [storeName : string] : any }; + private _allowedStoreNames: StoreNames[]; + private _stores: { [storeName in StoreNames]?: any }; - constructor(txn: IDBTransaction, allowedStoreNames: string[], IDBKeyRange) { + constructor(txn: IDBTransaction, allowedStoreNames: StoreNames[], IDBKeyRange) { this._txn = txn; this._allowedStoreNames = allowedStoreNames; this._stores = {}; @@ -48,7 +49,7 @@ export class Transaction { this.IDBKeyRange = IDBKeyRange; } - _idbStore(name: string): Store { + _idbStore(name: StoreNames): Store { 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.`); @@ -56,7 +57,7 @@ export class Transaction { return new Store(this._txn.objectStore(name), this); } - _store(name: string, mapStore: (idbStore: Store) => any): any { + _store(name: StoreNames, mapStore: (idbStore: Store) => any): any { if (!this._stores[name]) { const idbStore = this._idbStore(name); this._stores[name] = mapStore(idbStore); @@ -65,75 +66,75 @@ export class Transaction { } get session(): SessionStore { - return this._store("session", idbStore => new SessionStore(idbStore)); + return this._store(StoreNames.session, idbStore => new SessionStore(idbStore)); } get roomSummary(): RoomSummaryStore { - return this._store("roomSummary", idbStore => new RoomSummaryStore(idbStore)); + return this._store(StoreNames.roomSummary, idbStore => new RoomSummaryStore(idbStore)); } get archivedRoomSummary(): RoomSummaryStore { - return this._store("archivedRoomSummary", idbStore => new RoomSummaryStore(idbStore)); + return this._store(StoreNames.archivedRoomSummary, idbStore => new RoomSummaryStore(idbStore)); } get invites(): InviteStore { - return this._store("invites", idbStore => new InviteStore(idbStore)); + return this._store(StoreNames.invites, idbStore => new InviteStore(idbStore)); } get timelineFragments(): TimelineFragmentStore { - return this._store("timelineFragments", idbStore => new TimelineFragmentStore(idbStore)); + return this._store(StoreNames.timelineFragments, idbStore => new TimelineFragmentStore(idbStore)); } get timelineEvents(): TimelineEventStore { - return this._store("timelineEvents", idbStore => new TimelineEventStore(idbStore)); + return this._store(StoreNames.timelineEvents, idbStore => new TimelineEventStore(idbStore)); } get timelineRelations(): TimelineRelationStore { - return this._store("timelineRelations", idbStore => new TimelineRelationStore(idbStore)); + return this._store(StoreNames.timelineRelations, idbStore => new TimelineRelationStore(idbStore)); } get roomState(): RoomStateStore { - return this._store("roomState", idbStore => new RoomStateStore(idbStore)); + return this._store(StoreNames.roomState, idbStore => new RoomStateStore(idbStore)); } get roomMembers(): RoomMemberStore { - return this._store("roomMembers", idbStore => new RoomMemberStore(idbStore)); + return this._store(StoreNames.roomMembers, idbStore => new RoomMemberStore(idbStore)); } get pendingEvents(): PendingEventStore { - return this._store("pendingEvents", idbStore => new PendingEventStore(idbStore)); + return this._store(StoreNames.pendingEvents, idbStore => new PendingEventStore(idbStore)); } get userIdentities(): UserIdentityStore { - return this._store("userIdentities", idbStore => new UserIdentityStore(idbStore)); + return this._store(StoreNames.userIdentities, idbStore => new UserIdentityStore(idbStore)); } get deviceIdentities(): DeviceIdentityStore { - return this._store("deviceIdentities", idbStore => new DeviceIdentityStore(idbStore)); + return this._store(StoreNames.deviceIdentities, idbStore => new DeviceIdentityStore(idbStore)); } get olmSessions(): OlmSessionStore { - return this._store("olmSessions", idbStore => new OlmSessionStore(idbStore)); + return this._store(StoreNames.olmSessions, idbStore => new OlmSessionStore(idbStore)); } get inboundGroupSessions(): InboundGroupSessionStore { - return this._store("inboundGroupSessions", idbStore => new InboundGroupSessionStore(idbStore)); + return this._store(StoreNames.inboundGroupSessions, idbStore => new InboundGroupSessionStore(idbStore)); } get outboundGroupSessions(): OutboundGroupSessionStore { - return this._store("outboundGroupSessions", idbStore => new OutboundGroupSessionStore(idbStore)); + return this._store(StoreNames.outboundGroupSessions, idbStore => new OutboundGroupSessionStore(idbStore)); } get groupSessionDecryptions(): GroupSessionDecryptionStore { - return this._store("groupSessionDecryptions", idbStore => new GroupSessionDecryptionStore(idbStore)); + return this._store(StoreNames.groupSessionDecryptions, idbStore => new GroupSessionDecryptionStore(idbStore)); } get operations(): OperationStore { - return this._store("operations", idbStore => new OperationStore(idbStore)); + return this._store(StoreNames.operations, idbStore => new OperationStore(idbStore)); } get accountData(): AccountDataStore { - return this._store("accountData", idbStore => new AccountDataStore(idbStore)); + return this._store(StoreNames.accountData, idbStore => new AccountDataStore(idbStore)); } complete(): Promise {