Browse Source

Merge pull request #2 from gre/fix-flow-withLibcore

better flowtype on withLibcore
master
Meriadec Pillet 7 years ago
committed by GitHub
parent
commit
75571720ca
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      src/helpers/withLibcore.js

33
src/helpers/withLibcore.js

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

Loading…
Cancel
Save