From 78fb8fdadf7064014ee4e8d11c6d3865a8f4c199 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 31 Aug 2021 15:50:57 -0700 Subject: [PATCH] Make export types more precise --- src/matrix/storage/idb/StorageFactory.ts | 6 +++--- src/matrix/storage/idb/export.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/matrix/storage/idb/StorageFactory.ts b/src/matrix/storage/idb/StorageFactory.ts index 261338a1..8f1e80e2 100644 --- a/src/matrix/storage/idb/StorageFactory.ts +++ b/src/matrix/storage/idb/StorageFactory.ts @@ -16,7 +16,7 @@ limitations under the License. import {Storage} from "./Storage"; import { openDatabase, reqAsPromise } from "./utils"; -import { exportSession, importSession } from "./export"; +import { exportSession, importSession, Export } from "./export"; import { schema } from "./schema"; import { detectWebkitEarlyCloseTxnBug } from "./quirks"; @@ -80,12 +80,12 @@ export class StorageFactory { return reqAsPromise(req); } - async export(sessionId: string): Promise<{ [storeName: string]: any }> { + async export(sessionId: string): Promise { const db = await openDatabaseWithSessionId(sessionId, this._idbFactory); return await exportSession(db); } - async import(sessionId: string, data: { [storeName: string]: any }): Promise { + async import(sessionId: string, data: Export): Promise { const db = await openDatabaseWithSessionId(sessionId, this._idbFactory); return await importSession(db, data); } diff --git a/src/matrix/storage/idb/export.ts b/src/matrix/storage/idb/export.ts index c2880f3d..a9d58ee4 100644 --- a/src/matrix/storage/idb/export.ts +++ b/src/matrix/storage/idb/export.ts @@ -15,9 +15,11 @@ limitations under the License. */ import { iterateCursor, NOT_DONE, txnAsPromise } from "./utils"; -import { STORE_NAMES } from "../common"; +import { STORE_NAMES, StoreNames } from "../common"; -export async function exportSession(db: IDBDatabase): Promise<{ [storeName : string] : any }> { +export type Export = { [storeName in StoreNames] : any[] } + +export async function exportSession(db: IDBDatabase): Promise { const txn = db.transaction(STORE_NAMES, "readonly"); const data = {}; await Promise.all(STORE_NAMES.map(async name => { @@ -28,10 +30,10 @@ export async function exportSession(db: IDBDatabase): Promise<{ [storeName : str return NOT_DONE; }); })); - return data; + return data as Export; } -export async function importSession(db: IDBDatabase, data: { [storeName: string]: any }): Promise { +export async function importSession(db: IDBDatabase, data: Export): Promise { const txn = db.transaction(STORE_NAMES, "readwrite"); for (const name of STORE_NAMES) { const store = txn.objectStore(name);