-
-
Notifications
You must be signed in to change notification settings - Fork 361
Chore: remove dead code in CommonDB #18107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| import { BroadcastChannel } from 'broadcast-channel'; | ||
| import { | ||
| IDBPDatabase, | ||
| IDBPTransaction, | ||
|
|
@@ -12,11 +11,8 @@ import { | |
| unwrap, | ||
| } from 'idb'; | ||
|
|
||
| import { isFirefox } from '@trezor/env-utils'; | ||
| import { createLazy } from '@trezor/utils'; | ||
|
|
||
| import { StorageMessageEvent } from './types'; | ||
|
|
||
| export type OnUpgradeFunc<TDBStructure> = ( | ||
| db: IDBPDatabase<TDBStructure>, | ||
| oldVersion: number, | ||
|
|
@@ -28,7 +24,6 @@ class CommonDB<TDBStructure> { | |
| private static instance: CommonDB<any>; | ||
| dbName!: string; | ||
| version!: number; | ||
| broadcastChannel!: BroadcastChannel; | ||
| supported: boolean | undefined; | ||
| blocking = false; | ||
| blocked = false; | ||
|
|
@@ -67,39 +62,14 @@ class CommonDB<TDBStructure> { | |
|
|
||
| this.isSupported(); | ||
|
|
||
| // create global instance of broadcast channel | ||
| this.broadcastChannel = new BroadcastChannel('storageChangeEvent'); | ||
|
|
||
| CommonDB.instance = this; | ||
| } | ||
|
|
||
| static isDBAvailable = () => | ||
| // Firefox doesn't support indexedDB while in incognito mode, but still returns valid window.indexedDB object. | ||
| // https://bugzilla.mozilla.org/show_bug.cgi?id=781982 | ||
| // so we need to try accessing the IDB. try/catch around idb.open() does not catch the error (bug in idb?), that's why we use callbacks. | ||
| // this solution calls callback function from within onerror/onsuccess event handlers. | ||
| // For other browsers checking the window.indexedDB should be enough. | ||
| new Promise<boolean>(resolve => { | ||
| if (isFirefox()) { | ||
| const r = indexedDB.open('test'); | ||
| r.onerror = () => resolve(false); | ||
| r.onsuccess = () => { | ||
| indexedDB.deleteDatabase('test'); | ||
| resolve(true); | ||
| }; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This workaround is not needed anymore. The bug linked in comment was marked resolved 5 yrs ago. |
||
| } else { | ||
| const idbAvailable = !!indexedDB || !!window.indexedDB || !!global.indexedDB; | ||
| if (idbAvailable) { | ||
| resolve(true); | ||
| } else { | ||
| resolve(false); | ||
| } | ||
| } | ||
| }); | ||
| static isDBAvailable = () => !!indexedDB || !!window.indexedDB || !!global.indexedDB; | ||
|
|
||
| isSupported = async () => { | ||
| isSupported = () => { | ||
| if (this.supported === undefined) { | ||
| const isAvailable = await CommonDB.isDBAvailable(); | ||
| const isAvailable = CommonDB.isDBAvailable(); | ||
| this.supported = isAvailable; | ||
| if (!isAvailable) { | ||
| console.warn("Couldn't get an access to IndexedDB."); | ||
|
|
@@ -109,22 +79,11 @@ class CommonDB<TDBStructure> { | |
| return this.supported; | ||
| }; | ||
|
|
||
| isAccessible = async () => { | ||
| const isSupported = await this.isSupported(); | ||
| isAccessible = (): Promise<boolean> => { | ||
| const isSupported = this.isSupported(); | ||
|
|
||
| // if the instance is blocking db upgrade, db connection will be closed | ||
| return isSupported && !this.blocking && !this.blocked; | ||
| }; | ||
|
|
||
| notify = (store: StoreNames<TDBStructure>, keys: any[]) => { | ||
| // sends the message containing store, keys which were updated to other tabs/windows | ||
| const message = { store, keys }; | ||
| this.broadcastChannel.postMessage(message); | ||
| }; | ||
|
|
||
| onChange = (handler: (event: StorageMessageEvent<TDBStructure>) => any) => { | ||
| // listens to the channel. On receiving a message triggers the handler func | ||
| this.broadcastChannel.onmessage = handler; | ||
| return Promise.resolve(isSupported && !this.blocking && !this.blocked); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first I thought that removing the FF workaround can reduce this whole chain of function from async to sync. |
||
| }; | ||
|
|
||
| closeAfterTimeout = (timeout = 1000) => { | ||
|
|
@@ -206,7 +165,6 @@ class CommonDB<TDBStructure> { | |
| reject(req.error); | ||
| }; | ||
| req.onsuccess = _event => { | ||
| this.notify(store, [req.result]); | ||
| resolve(req.result); | ||
| }; | ||
| } catch (error) { | ||
|
|
@@ -231,7 +189,7 @@ class CommonDB<TDBStructure> { | |
| const tx = db.transaction(store, 'readwrite'); | ||
|
|
||
| const keys: StoreKey<TDBStructure, StoreNames<TDBStructure>>[] = []; | ||
| const promises = Promise.all( | ||
| const promisesOfItems: Promise<void[]> = Promise.all( | ||
| items | ||
| .map(item => { | ||
| if (upsert) { | ||
|
|
@@ -245,11 +203,10 @@ class CommonDB<TDBStructure> { | |
| }); | ||
| }) | ||
| .concat(tx.done), | ||
| ).then(_ => { | ||
| this.notify(store, keys); | ||
| }); | ||
| ); | ||
| const aggregatedPromise: Promise<void> = promisesOfItems.then(() => {}); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this fn is expected to return |
||
|
|
||
| return promises; | ||
| return aggregatedPromise; | ||
| }; | ||
|
|
||
| getItemByPK = async < | ||
|
|
@@ -300,7 +257,6 @@ class CommonDB<TDBStructure> { | |
| const result = await index.get(key); | ||
| if (result) { | ||
| Object.assign(result, updateObject); | ||
| this.notify(store, [result]); | ||
|
|
||
| return tx.store.put(result); | ||
| } | ||
|
|
@@ -339,7 +295,6 @@ class CommonDB<TDBStructure> { | |
| let cursor = await txIdIndex.openCursor(IDBKeyRange.only(key)); | ||
| while (cursor) { | ||
| cursor.delete(); | ||
| this.notify(store, cursor.value ? [cursor.value] : []); | ||
|
|
||
| cursor = await cursor.continue(); | ||
| } | ||
|
|
@@ -471,4 +426,3 @@ class CommonDB<TDBStructure> { | |
| } | ||
|
|
||
| export default CommonDB; | ||
| export type { StorageUpdateMessage } from './types'; | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,6 @@ express | |
|
|
||
| # STORAGE | ||
| idb | ||
| broadcast-channel | ||
|
|
||
| # ESLINT | ||
| @eslint/js | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BroadcastChannelis natively offerred by browsers, and we rely on the native implementation at multiple places!