diff --git a/src/helpers/withLibcore.js b/src/helpers/withLibcore.js index 7b053ea4..babacb3e 100644 --- a/src/helpers/withLibcore.js +++ b/src/helpers/withLibcore.js @@ -1,31 +1,30 @@ // @flow +import invariant from 'invariant' + const core = require('@ledgerhq/ledger-core') -let walletPool = null +let walletPoolInstance: ?Object = null let queue = Promise.resolve() // TODO: `core` and `NJSWalletPool` should be typed -type Job = (Object, Object) => any +type Job = (Object, Object) => Promise -export default function withLibcore(job: Job) { - if (!walletPool) { - walletPool = core.instanciateWalletPool({ +export default function withLibcore(job: Job): Promise { + if (!walletPoolInstance) { + walletPoolInstance = core.instanciateWalletPool({ // sqlite files will be located in the app local data folder dbPath: process.env.LEDGER_LIVE_SQLITE_PATH, }) } - // $FlowFixMe WTF is happening here, dudes. - queue = queue.then(async () => { - try { - if (!walletPool) { - throw new Error('wallet pool not instanciated. this should not happen') - } - return job(core, walletPool) - } catch (e) { - console.log(`withLibCore: Error in job`, e) // eslint-disable-line no-console - return Promise.resolve() - } + const walletPool = walletPoolInstance + invariant(walletPool, 'core.instanciateWalletPool returned null !!') + + const p = queue.then(() => job(core, walletPool)) + + queue = p.catch(e => { + console.warn(`withLibCore: Error in job`, e) }) - return queue + + return p }