From e69fa025338e6865fd16cf95fd55157aa46bc4ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Sun, 3 Jun 2018 17:44:25 +0200 Subject: [PATCH] better flowtype on withLibcore --- src/helpers/withLibcore.js | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) 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 }