mt-polygon-simplification/lib/wasm-util/initEmscripten.js
2019-07-14 20:37:26 +02:00

29 lines
990 B
JavaScript

/**
* 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)
}
})
})
}