/** * Promisify emscripten modules * * idea taken from squoosh app: https://raw.githubusercontent.com/GoogleChromeLabs/squoosh/master/src/codecs/util.ts * * usage: * // create a global variable to store the initialized module once * let emscriptenModule * * // inside an async function: * // use the global as cache to the module or initialize it * if (!emscriptenModule) emscriptenModule = initEmscriptenModule(importedGlueCode, urlToWasmFile) * const module = await emscriptenModule */ export function initEmscriptenModule(moduleFactory, wasmUrl) { return new Promise (resolve => { const module = moduleFactory({ noInitialRun: true, locateFile: (url) => wasmUrl ? wasmUrl : url, onRuntimeInitialized () { // hack: deleting then breaks an infinite loop // https://github.com/emscripten-core/emscripten/issues/5820 delete module.then resolve(module) } }) }) }