Use store name enum for saving stores

This commit is contained in:
Danila Fedorin 2021-08-31 15:44:03 -07:00
parent de3807f690
commit 2262e6be30

View file

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {StoreNames} from "../common";
import {txnAsPromise} from "./utils"; import {txnAsPromise} from "./utils";
import {StorageError} from "../common"; import {StorageError} from "../common";
import {Store} from "./Store"; import {Store} from "./Store";
@ -37,10 +38,10 @@ import {AccountDataStore} from "./stores/AccountDataStore";
export class Transaction { export class Transaction {
private _txn: IDBTransaction; private _txn: IDBTransaction;
private _allowedStoreNames: string[]; private _allowedStoreNames: StoreNames[];
private _stores: { [storeName : string] : any }; private _stores: { [storeName in StoreNames]?: any };
constructor(txn: IDBTransaction, allowedStoreNames: string[], IDBKeyRange) { constructor(txn: IDBTransaction, allowedStoreNames: StoreNames[], IDBKeyRange) {
this._txn = txn; this._txn = txn;
this._allowedStoreNames = allowedStoreNames; this._allowedStoreNames = allowedStoreNames;
this._stores = {}; this._stores = {};
@ -48,7 +49,7 @@ export class Transaction {
this.IDBKeyRange = IDBKeyRange; this.IDBKeyRange = IDBKeyRange;
} }
_idbStore(name: string): Store<any> { _idbStore(name: StoreNames): Store<any> {
if (!this._allowedStoreNames.includes(name)) { if (!this._allowedStoreNames.includes(name)) {
// more specific error? this is a bug, so maybe not ... // more specific error? this is a bug, so maybe not ...
throw new StorageError(`Invalid store for transaction: ${name}, only ${this._allowedStoreNames.join(", ")} are allowed.`); 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); return new Store(this._txn.objectStore(name), this);
} }
_store(name: string, mapStore: (idbStore: Store<any>) => any): any { _store(name: StoreNames, mapStore: (idbStore: Store<any>) => any): any {
if (!this._stores[name]) { if (!this._stores[name]) {
const idbStore = this._idbStore(name); const idbStore = this._idbStore(name);
this._stores[name] = mapStore(idbStore); this._stores[name] = mapStore(idbStore);
@ -65,75 +66,75 @@ export class Transaction {
} }
get session(): SessionStore { get session(): SessionStore {
return this._store("session", idbStore => new SessionStore(idbStore)); return this._store(StoreNames.session, idbStore => new SessionStore(idbStore));
} }
get roomSummary(): RoomSummaryStore { get roomSummary(): RoomSummaryStore {
return this._store("roomSummary", idbStore => new RoomSummaryStore(idbStore)); return this._store(StoreNames.roomSummary, idbStore => new RoomSummaryStore(idbStore));
} }
get archivedRoomSummary(): RoomSummaryStore { get archivedRoomSummary(): RoomSummaryStore {
return this._store("archivedRoomSummary", idbStore => new RoomSummaryStore(idbStore)); return this._store(StoreNames.archivedRoomSummary, idbStore => new RoomSummaryStore(idbStore));
} }
get invites(): InviteStore { get invites(): InviteStore {
return this._store("invites", idbStore => new InviteStore(idbStore)); return this._store(StoreNames.invites, idbStore => new InviteStore(idbStore));
} }
get timelineFragments(): TimelineFragmentStore { get timelineFragments(): TimelineFragmentStore {
return this._store("timelineFragments", idbStore => new TimelineFragmentStore(idbStore)); return this._store(StoreNames.timelineFragments, idbStore => new TimelineFragmentStore(idbStore));
} }
get timelineEvents(): TimelineEventStore { get timelineEvents(): TimelineEventStore {
return this._store("timelineEvents", idbStore => new TimelineEventStore(idbStore)); return this._store(StoreNames.timelineEvents, idbStore => new TimelineEventStore(idbStore));
} }
get timelineRelations(): TimelineRelationStore { get timelineRelations(): TimelineRelationStore {
return this._store("timelineRelations", idbStore => new TimelineRelationStore(idbStore)); return this._store(StoreNames.timelineRelations, idbStore => new TimelineRelationStore(idbStore));
} }
get roomState(): RoomStateStore { get roomState(): RoomStateStore {
return this._store("roomState", idbStore => new RoomStateStore(idbStore)); return this._store(StoreNames.roomState, idbStore => new RoomStateStore(idbStore));
} }
get roomMembers(): RoomMemberStore { get roomMembers(): RoomMemberStore {
return this._store("roomMembers", idbStore => new RoomMemberStore(idbStore)); return this._store(StoreNames.roomMembers, idbStore => new RoomMemberStore(idbStore));
} }
get pendingEvents(): PendingEventStore { get pendingEvents(): PendingEventStore {
return this._store("pendingEvents", idbStore => new PendingEventStore(idbStore)); return this._store(StoreNames.pendingEvents, idbStore => new PendingEventStore(idbStore));
} }
get userIdentities(): UserIdentityStore { get userIdentities(): UserIdentityStore {
return this._store("userIdentities", idbStore => new UserIdentityStore(idbStore)); return this._store(StoreNames.userIdentities, idbStore => new UserIdentityStore(idbStore));
} }
get deviceIdentities(): DeviceIdentityStore { get deviceIdentities(): DeviceIdentityStore {
return this._store("deviceIdentities", idbStore => new DeviceIdentityStore(idbStore)); return this._store(StoreNames.deviceIdentities, idbStore => new DeviceIdentityStore(idbStore));
} }
get olmSessions(): OlmSessionStore { get olmSessions(): OlmSessionStore {
return this._store("olmSessions", idbStore => new OlmSessionStore(idbStore)); return this._store(StoreNames.olmSessions, idbStore => new OlmSessionStore(idbStore));
} }
get inboundGroupSessions(): InboundGroupSessionStore { get inboundGroupSessions(): InboundGroupSessionStore {
return this._store("inboundGroupSessions", idbStore => new InboundGroupSessionStore(idbStore)); return this._store(StoreNames.inboundGroupSessions, idbStore => new InboundGroupSessionStore(idbStore));
} }
get outboundGroupSessions(): OutboundGroupSessionStore { get outboundGroupSessions(): OutboundGroupSessionStore {
return this._store("outboundGroupSessions", idbStore => new OutboundGroupSessionStore(idbStore)); return this._store(StoreNames.outboundGroupSessions, idbStore => new OutboundGroupSessionStore(idbStore));
} }
get groupSessionDecryptions(): GroupSessionDecryptionStore { get groupSessionDecryptions(): GroupSessionDecryptionStore {
return this._store("groupSessionDecryptions", idbStore => new GroupSessionDecryptionStore(idbStore)); return this._store(StoreNames.groupSessionDecryptions, idbStore => new GroupSessionDecryptionStore(idbStore));
} }
get operations(): OperationStore { get operations(): OperationStore {
return this._store("operations", idbStore => new OperationStore(idbStore)); return this._store(StoreNames.operations, idbStore => new OperationStore(idbStore));
} }
get accountData(): AccountDataStore { get accountData(): AccountDataStore {
return this._store("accountData", idbStore => new AccountDataStore(idbStore)); return this._store(StoreNames.accountData, idbStore => new AccountDataStore(idbStore));
} }
complete(): Promise<void> { complete(): Promise<void> {