From 57c27e6a4b51fef7db004280bf34491bfeb5ed8b Mon Sep 17 00:00:00 2001 From: fourdim <59462000+fourdim@users.noreply.github.com> Date: Fri, 28 Jul 2023 00:30:09 +0800 Subject: [PATCH] fix: undefined allDb in firefox (#3417) --- packages/y-indexeddb/src/utils.ts | 81 ++++++++++++++++--------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/packages/y-indexeddb/src/utils.ts b/packages/y-indexeddb/src/utils.ts index 9258975145..0af92ffe03 100644 --- a/packages/y-indexeddb/src/utils.ts +++ b/packages/y-indexeddb/src/utils.ts @@ -78,47 +78,48 @@ export async function tryMigrate( } // run the migration await Promise.all( - allDb.map(meta => { - if (meta.name && meta.version === 1) { - const name = meta.name; - const version = meta.version; - return openDB>(name, version).then( - async oldDB => { - if (!oldDB.objectStoreNames.contains('updates')) { - return; + allDb && + allDb.map(meta => { + if (meta.name && meta.version === 1) { + const name = meta.name; + const version = meta.version; + return openDB>(name, version).then( + async oldDB => { + if (!oldDB.objectStoreNames.contains('updates')) { + return; + } + const t = oldDB + .transaction('updates', 'readonly') + .objectStore('updates'); + const updates = await t.getAll(); + if ( + !Array.isArray(updates) || + !updates.every(update => update instanceof Uint8Array) + ) { + return; + } + const update = mergeUpdates(updates); + const workspaceTransaction = db + .transaction('workspace', 'readwrite') + .objectStore('workspace'); + const data = await workspaceTransaction.get(name); + if (!data) { + console.log('upgrading the database'); + await workspaceTransaction.put({ + id: name, + updates: [ + { + timestamp: Date.now(), + update, + }, + ], + }); + } } - const t = oldDB - .transaction('updates', 'readonly') - .objectStore('updates'); - const updates = await t.getAll(); - if ( - !Array.isArray(updates) || - !updates.every(update => update instanceof Uint8Array) - ) { - return; - } - const update = mergeUpdates(updates); - const workspaceTransaction = db - .transaction('workspace', 'readwrite') - .objectStore('workspace'); - const data = await workspaceTransaction.get(name); - if (!data) { - console.log('upgrading the database'); - await workspaceTransaction.put({ - id: name, - updates: [ - { - timestamp: Date.now(), - update, - }, - ], - }); - } - } - ); - } - return void 0; - }) + ); + } + return void 0; + }) ); localStorage.setItem(`${dbName}-migration`, 'true'); break;