From b9d003196a6deb3f62396b39df6211b3360fcd5d Mon Sep 17 00:00:00 2001 From: Nicola Thouliss Date: Thu, 27 May 2021 17:13:14 +1000 Subject: [PATCH] Converted more files to typescript --- src/global.d.ts | 4 ++- src/maps/{index.js => index.ts} | 0 src/tokens/{index.js => index.ts} | 2 +- .../{DatabaseWorker.js => DatabaseWorker.ts} | 25 +++++++++++-------- 4 files changed, 19 insertions(+), 12 deletions(-) rename src/maps/{index.js => index.ts} (100%) rename src/tokens/{index.js => index.ts} (97%) rename src/workers/{DatabaseWorker.js => DatabaseWorker.ts} (83%) diff --git a/src/global.d.ts b/src/global.d.ts index a476569..64cda09 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,2 +1,4 @@ declare module 'pepjs'; -declare module 'socket.io-msgpack-parser'; \ No newline at end of file +declare module 'socket.io-msgpack-parser'; +declare module 'fake-indexeddb'; +declare module 'fake-indexeddb/lib/FDBKeyRange' \ No newline at end of file diff --git a/src/maps/index.js b/src/maps/index.ts similarity index 100% rename from src/maps/index.js rename to src/maps/index.ts diff --git a/src/tokens/index.js b/src/tokens/index.ts similarity index 97% rename from src/tokens/index.js rename to src/tokens/index.ts index 5a16e82..122975e 100644 --- a/src/tokens/index.js +++ b/src/tokens/index.ts @@ -67,7 +67,7 @@ export const tokenSources = { undead, }; -function getDefaultTokenSize(key) { +function getDefaultTokenSize(key: string) { switch (key) { case "dragon": case "elemental": diff --git a/src/workers/DatabaseWorker.js b/src/workers/DatabaseWorker.ts similarity index 83% rename from src/workers/DatabaseWorker.js rename to src/workers/DatabaseWorker.ts index 28d6c9c..6959aee 100644 --- a/src/workers/DatabaseWorker.js +++ b/src/workers/DatabaseWorker.ts @@ -8,16 +8,19 @@ import { encode, decode } from "@msgpack/msgpack"; import { getDatabase } from "../database"; import blobToBuffer from "../helpers/blobToBuffer"; +import { ExportProgress } from "@mitchemmc/dexie-export-import/dist/export"; + +type ProgressCallback = (progress: ExportProgress) => boolean; // Worker to load large amounts of database data on a separate thread let service = { /** * Load either a whole table or individual item from the DB * @param {string} table Table to load from - * @param {string=} key Optional database key to load, if undefined whole table will be loaded + * @param {string} key Optional database key to load, if undefined whole table will be loaded * @param {bool} excludeFiles Optional exclude files from loaded data when using whole table loading */ - async loadData(table, key, excludeFiles = true) { + async loadData(table: string, key: string, excludeFiles: boolean = true) { try { let db = getDatabase({}); if (key) { @@ -26,7 +29,7 @@ let service = { return data; } else { // Load entire table - let items = []; + let items: any[] = []; // Use a cursor instead of toArray to prevent IPC max size error await db.table(table).each((item) => { if (excludeFiles) { @@ -41,7 +44,9 @@ let service = { const packed = encode(items); return Comlink.transfer(packed, [packed.buffer]); } - } catch {} + } catch { + // TODO: throw error in empty catch? + } }, /** @@ -50,7 +55,7 @@ let service = { * @param {string} table * @param {boolean} wait Whether to wait for the put to finish */ - async putData(data, table, wait = true) { + async putData(data: Uint8Array, table: string, wait: boolean = true) { try { let db = getDatabase({}); const decoded = decode(data); @@ -67,14 +72,14 @@ let service = { /** * Export current database - * @param {function} progressCallback + * @param {ProgressCallback} progressCallback * @param {string[]} maps An array of map ids to export * @param {string[]} tokens An array of token ids to export */ - async exportData(progressCallback, maps, tokens) { + async exportData(progressCallback: ProgressCallback, maps: string[], tokens: string[]) { let db = getDatabase({}); - const filter = (table, value) => { + const filter = (table: string, value: any) => { if (table === "maps") { return maps.includes(value.id); } @@ -103,9 +108,9 @@ let service = { * Import into current database * @param {Blob} data * @param {string} databaseName The name of the database to import into - * @param {function} progressCallback + * @param {ProgressCallback} progressCallback */ - async importData(data, databaseName, progressCallback) { + async importData(data: Blob, databaseName: string, progressCallback: ProgressCallback) { const importMeta = await peakImportFile(data); if (!importMeta.data) { throw new Error("Uanble to parse file");