Browse Source

better flowtype on withLibcore

master
Gaëtan Renaudeau 7 years ago
parent
commit
e69fa02533
  1. 33
      src/helpers/withLibcore.js

33
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<A> = (Object, Object) => Promise<A>
export default function withLibcore(job: Job) {
if (!walletPool) {
walletPool = core.instanciateWalletPool({
export default function withLibcore<A>(job: Job<A>): Promise<A> {
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
}

Loading…
Cancel
Save