forked from external-repos/squoosh
Two workers & worker termination (#198)
* Refactoring codecs * Plugging in new processor * Fixing decorator * MozJPEG free issue * Better worker aborting, and terminate workers that aren't used for 10 seconds * Better comment * Ooops, half-typed comment * Uncommenting problematic line * Surma fixed it! * Abstracting WASM initialisation * Better comment * Don't need this. * Adding ticket * noInitalRun * Reverting MozJPEG issue demo * Making a const for worker timeout * Inline docs * Bail early rather than nesting * Addressing nits
This commit is contained in:
27
src/codecs/util.ts
Normal file
27
src/codecs/util.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
type ModuleFactory<M extends EmscriptenWasm.Module> = (
|
||||
opts: EmscriptenWasm.ModuleOpts,
|
||||
) => M;
|
||||
|
||||
export function initWasmModule<T extends EmscriptenWasm.Module>(
|
||||
moduleFactory: ModuleFactory<T>,
|
||||
wasmUrl: string,
|
||||
): Promise<T> {
|
||||
return new Promise((resolve) => {
|
||||
const module = moduleFactory({
|
||||
// Just to be safe, don't automatically invoke any wasm functions
|
||||
noInitialRun: true,
|
||||
locateFile(url: string): string {
|
||||
// Redirect the request for the wasm binary to whatever webpack gave us.
|
||||
if (url.endsWith('.wasm')) return wasmUrl;
|
||||
return url;
|
||||
},
|
||||
onRuntimeInitialized() {
|
||||
// An Emscripten is a then-able that resolves with itself, causing an infite loop when you
|
||||
// wrap it in a real promise. Delete the `then` prop solves this for now.
|
||||
// https://github.com/kripken/emscripten/issues/5820
|
||||
delete (module as any).then;
|
||||
resolve(module);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user