diff --git a/codecs/avif/dec/avif_dec.d.ts b/codecs/avif/dec/avif_dec.d.ts index ff52bcff..b7a6633c 100644 --- a/codecs/avif/dec/avif_dec.d.ts +++ b/codecs/avif/dec/avif_dec.d.ts @@ -2,5 +2,5 @@ interface AVIFModule extends EmscriptenWasm.Module { decode(data: BufferSource): ImageData | null; } -export default function(opts: EmscriptenWasm.ModuleOpts): AVIFModule; +export default function(opts: EmscriptenWasm.ModuleOpts): Promise; diff --git a/codecs/avif/enc/avif_enc.d.ts b/codecs/avif/enc/avif_enc.d.ts index c00e18e7..5f22ecf8 100644 --- a/codecs/avif/enc/avif_enc.d.ts +++ b/codecs/avif/enc/avif_enc.d.ts @@ -4,4 +4,4 @@ interface AVIFModule extends EmscriptenWasm.Module { encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array | null; } -export default function(opts: EmscriptenWasm.ModuleOpts): AVIFModule; +export default function(opts: EmscriptenWasm.ModuleOpts): Promise; diff --git a/codecs/imagequant/imagequant.d.ts b/codecs/imagequant/imagequant.d.ts index d25d7a35..3f5da5cc 100644 --- a/codecs/imagequant/imagequant.d.ts +++ b/codecs/imagequant/imagequant.d.ts @@ -3,4 +3,4 @@ interface QuantizerModule extends EmscriptenWasm.Module { zx_quantize(data: BufferSource, width: number, height: number, dither: number): Uint8ClampedArray; } -export default function(opts: EmscriptenWasm.ModuleOpts): QuantizerModule; +export default function(opts: EmscriptenWasm.ModuleOpts): Promise; diff --git a/codecs/mozjpeg_enc/mozjpeg_enc.d.ts b/codecs/mozjpeg_enc/mozjpeg_enc.d.ts index 58e808f4..29d33b0f 100644 --- a/codecs/mozjpeg_enc/mozjpeg_enc.d.ts +++ b/codecs/mozjpeg_enc/mozjpeg_enc.d.ts @@ -4,4 +4,4 @@ interface MozJPEGModule extends EmscriptenWasm.Module { encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array; } -export default function(opts: EmscriptenWasm.ModuleOpts): MozJPEGModule; +export default function(opts: EmscriptenWasm.ModuleOpts): Promise; diff --git a/codecs/webp/dec/example.html b/codecs/webp/dec/example.html index 46089934..64eb175f 100644 --- a/codecs/webp/dec/example.html +++ b/codecs/webp/dec/example.html @@ -1,22 +1,20 @@ diff --git a/codecs/webp/dec/webp_dec.d.ts b/codecs/webp/dec/webp_dec.d.ts index a26279d8..71b72267 100644 --- a/codecs/webp/dec/webp_dec.d.ts +++ b/codecs/webp/dec/webp_dec.d.ts @@ -2,4 +2,4 @@ interface WebPModule extends EmscriptenWasm.Module { decode(data: BufferSource): ImageData | null; } -export default function(opts: EmscriptenWasm.ModuleOpts): WebPModule; +export default function(opts: EmscriptenWasm.ModuleOpts): Promise; diff --git a/codecs/webp/enc/example.html b/codecs/webp/enc/example.html index d868a9de..2b0c8c4a 100644 --- a/codecs/webp/enc/example.html +++ b/codecs/webp/enc/example.html @@ -1,8 +1,6 @@ diff --git a/codecs/webp/enc/webp_enc.d.ts b/codecs/webp/enc/webp_enc.d.ts index 106290db..d60b97b7 100644 --- a/codecs/webp/enc/webp_enc.d.ts +++ b/codecs/webp/enc/webp_enc.d.ts @@ -4,4 +4,4 @@ interface WebPModule extends EmscriptenWasm.Module { encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array | null; } -export default function(opts: EmscriptenWasm.ModuleOpts): WebPModule; +export default function(opts: EmscriptenWasm.ModuleOpts): Promise; diff --git a/src/codecs/util.ts b/src/codecs/util.ts index b3edcee0..dd9b58bb 100644 --- a/src/codecs/util.ts +++ b/src/codecs/util.ts @@ -1,28 +1,19 @@ type ModuleFactory = ( opts: EmscriptenWasm.ModuleOpts, -) => M; +) => Promise; export function initEmscriptenModule( moduleFactory: ModuleFactory, wasmUrl: string, ): Promise { - 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); - }, - }); + return 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; + }, }); }