From cc4e8ddc85d36f29a7385a7f4eb08c77f46b3af6 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 31 Jul 2019 01:33:56 +0200 Subject: headless wallet WIP --- src/crypto/emscLoader.js | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'src/crypto/emscLoader.js') diff --git a/src/crypto/emscLoader.js b/src/crypto/emscLoader.js index 59437da42..25dc6b85c 100644 --- a/src/crypto/emscLoader.js +++ b/src/crypto/emscLoader.js @@ -25,20 +25,29 @@ */ let cachedLib = undefined; +let cachedLibPromise = undefined; + +export let enableTracing = false; /** * Load the taler emscripten lib. * * If in a WebWorker, importScripts is used. Inside a browser, the module must * be globally available. Inside node, require is used. + * + * Returns a Promise<{ lib: EmscLib }> */ export function getLib() { - console.log("in getLib"); + enableTracing && console.log("in getLib"); if (cachedLib) { - console.log("lib is cached"); + enableTracing && console.log("lib is cached"); return Promise.resolve({ lib: cachedLib }); } + if (cachedLibPromise) { + return cachedLibPromise; + } if (typeof require !== "undefined") { + enableTracing && console.log("trying to load emscripten lib with 'require'"); // Make sure that TypeScript doesn't try // to check the taler-emscripten-lib. const indirectRequire = require; @@ -49,17 +58,30 @@ export function getLib() { const savedImportScripts = g.importScripts; delete g.importScripts; // Assume that the code is run from the build/ directory. - const lib = indirectRequire("../../../emscripten/taler-emscripten-lib.js"); + const libFn = indirectRequire("../../../emscripten/taler-emscripten-lib.js"); + const lib = libFn(); g.importScripts = savedImportScripts; if (lib) { - cachedLib = lib; - return Promise.resolve({ lib: cachedLib }); + if (!lib.ccall) { + throw Error("sanity check failed: taler-emscripten lib does not have 'ccall'"); + } + cachedLibPromise = new Promise((resolve, reject) => { + lib.onRuntimeInitialized = () => { + cachedLib = lib; + cachedLibPromise = undefined; + resolve({ lib: cachedLib }); + }; + }); + return cachedLibPromise; + } else { + // When we're running as a webpack bundle, the above require might + // have failed and returned 'undefined', so we try other ways to import. + console.log("failed to load emscripten lib with 'require', trying alternatives"); } - // When we're running as a webpack bundle, the above require might - // have failed and returned 'undefined', so we try other ways to import. } if (typeof importScripts !== "undefined") { + console.log("trying to load emscripten lib with 'importScripts'"); self.TalerEmscriptenLib = {}; importScripts('/emscripten/taler-emscripten-lib.js') if (!self.TalerEmscriptenLib) { -- cgit v1.2.3