Meriadec Pillet
7 years ago
committed by
GitHub
1 changed files with 16 additions and 17 deletions
@ -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…
Reference in new issue