From 5f3c9cda975e6ea6e6a62ab5556472c3e4261a02 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 12 Aug 2021 12:38:44 -0700 Subject: [PATCH] Migrate Transaction to TypeScript --- src/matrix/storage/idb/Storage.js | 2 +- .../idb/{Transaction.js => Transaction.ts} | 51 ++++++++++--------- 2 files changed, 29 insertions(+), 24 deletions(-) rename src/matrix/storage/idb/{Transaction.js => Transaction.ts} (78%) diff --git a/src/matrix/storage/idb/Storage.js b/src/matrix/storage/idb/Storage.js index a0814a53..d04183b6 100644 --- a/src/matrix/storage/idb/Storage.js +++ b/src/matrix/storage/idb/Storage.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {Transaction} from "./Transaction.js"; +import {Transaction} from "./Transaction"; import { STORE_NAMES, StoreNames, StorageError } from "../common"; import { reqAsPromise } from "./utils"; diff --git a/src/matrix/storage/idb/Transaction.js b/src/matrix/storage/idb/Transaction.ts similarity index 78% rename from src/matrix/storage/idb/Transaction.js rename to src/matrix/storage/idb/Transaction.ts index 8b85b795..b6fee2f2 100644 --- a/src/matrix/storage/idb/Transaction.js +++ b/src/matrix/storage/idb/Transaction.ts @@ -36,14 +36,19 @@ import {OperationStore} from "./stores/OperationStore"; import {AccountDataStore} from "./stores/AccountDataStore"; export class Transaction { - constructor(txn, allowedStoreNames, IDBKeyRange) { + private _txn: IDBTransaction; + private _allowedStoreNames: string[]; + private _stores: { [storeName : string] : any }; + + constructor(txn: IDBTransaction, allowedStoreNames: string[], IDBKeyRange) { this._txn = txn; this._allowedStoreNames = allowedStoreNames; this._stores = {}; + // @ts-ignore this.IDBKeyRange = IDBKeyRange; } - _idbStore(name) { + _idbStore(name: string): 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.`); @@ -51,7 +56,7 @@ export class Transaction { return new Store(this._txn.objectStore(name), this); } - _store(name, mapStore) { + _store(name: string, mapStore: (idbStore: Store) => any): any { if (!this._stores[name]) { const idbStore = this._idbStore(name); this._stores[name] = mapStore(idbStore); @@ -59,83 +64,83 @@ export class Transaction { return this._stores[name]; } - get session() { + get session(): SessionStore { return this._store("session", idbStore => new SessionStore(idbStore)); } - get roomSummary() { + get roomSummary(): RoomSummaryStore { return this._store("roomSummary", idbStore => new RoomSummaryStore(idbStore)); } - get archivedRoomSummary() { + get archivedRoomSummary(): RoomSummaryStore { return this._store("archivedRoomSummary", idbStore => new RoomSummaryStore(idbStore)); } - get invites() { + get invites(): InviteStore { return this._store("invites", idbStore => new InviteStore(idbStore)); } - get timelineFragments() { + get timelineFragments(): TimelineFragmentStore { return this._store("timelineFragments", idbStore => new TimelineFragmentStore(idbStore)); } - get timelineEvents() { + get timelineEvents(): TimelineEventStore { return this._store("timelineEvents", idbStore => new TimelineEventStore(idbStore)); } - get timelineRelations() { + get timelineRelations(): TimelineRelationStore { return this._store("timelineRelations", idbStore => new TimelineRelationStore(idbStore)); } - get roomState() { + get roomState(): RoomStateStore { return this._store("roomState", idbStore => new RoomStateStore(idbStore)); } - get roomMembers() { + get roomMembers(): RoomMemberStore { return this._store("roomMembers", idbStore => new RoomMemberStore(idbStore)); } - get pendingEvents() { + get pendingEvents(): PendingEventStore { return this._store("pendingEvents", idbStore => new PendingEventStore(idbStore)); } - get userIdentities() { + get userIdentities(): UserIdentityStore { return this._store("userIdentities", idbStore => new UserIdentityStore(idbStore)); } - get deviceIdentities() { + get deviceIdentities(): DeviceIdentityStore { return this._store("deviceIdentities", idbStore => new DeviceIdentityStore(idbStore)); } - get olmSessions() { + get olmSessions(): OlmSessionStore { return this._store("olmSessions", idbStore => new OlmSessionStore(idbStore)); } - get inboundGroupSessions() { + get inboundGroupSessions(): InboundGroupSessionStore { return this._store("inboundGroupSessions", idbStore => new InboundGroupSessionStore(idbStore)); } - get outboundGroupSessions() { + get outboundGroupSessions(): OutboundGroupSessionStore { return this._store("outboundGroupSessions", idbStore => new OutboundGroupSessionStore(idbStore)); } - get groupSessionDecryptions() { + get groupSessionDecryptions(): GroupSessionDecryptionStore { return this._store("groupSessionDecryptions", idbStore => new GroupSessionDecryptionStore(idbStore)); } - get operations() { + get operations(): OperationStore { return this._store("operations", idbStore => new OperationStore(idbStore)); } - get accountData() { + get accountData(): AccountDataStore { return this._store("accountData", idbStore => new AccountDataStore(idbStore)); } - complete() { + complete(): Promise { return txnAsPromise(this._txn); } - abort() { + abort(): void { // TODO: should we wrap the exception in a StorageError? this._txn.abort(); }