Fixup Emscripten factory & types

These days Emscripten returns a Promise to the module directly without hacks.
This commit is contained in:
Ingvar Stepanyan
2020-09-25 00:46:31 +01:00
parent 684b041262
commit 4c4f05f2e3
7 changed files with 17 additions and 26 deletions

View File

@@ -2,5 +2,5 @@ interface AVIFModule extends EmscriptenWasm.Module {
decode(data: BufferSource): ImageData | null; decode(data: BufferSource): ImageData | null;
} }
export default function(opts: EmscriptenWasm.ModuleOpts): AVIFModule; export default function(opts: EmscriptenWasm.ModuleOpts): Promise<AVIFModule>;

View File

@@ -4,4 +4,4 @@ interface AVIFModule extends EmscriptenWasm.Module {
encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array | null; 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<AVIFModule>;

View File

@@ -3,4 +3,4 @@ interface QuantizerModule extends EmscriptenWasm.Module {
zx_quantize(data: BufferSource, width: number, height: number, dither: number): Uint8ClampedArray; 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<QuantizerModule>;

View File

@@ -4,4 +4,4 @@ interface MozJPEGModule extends EmscriptenWasm.Module {
encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array; encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array;
} }
export default function(opts: EmscriptenWasm.ModuleOpts): MozJPEGModule; export default function(opts: EmscriptenWasm.ModuleOpts): Promise<MozJPEGModule>;

View File

@@ -2,4 +2,4 @@ interface WebPModule extends EmscriptenWasm.Module {
decode(data: BufferSource): ImageData | null; decode(data: BufferSource): ImageData | null;
} }
export default function(opts: EmscriptenWasm.ModuleOpts): WebPModule; export default function(opts: EmscriptenWasm.ModuleOpts): Promise<WebPModule>;

View File

@@ -4,4 +4,4 @@ interface WebPModule extends EmscriptenWasm.Module {
encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array | null; 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<WebPModule>;

View File

@@ -1,6 +1,6 @@
export type ModuleFactory<M extends EmscriptenWasm.Module> = ( export type ModuleFactory<M extends EmscriptenWasm.Module> = (
opts: EmscriptenWasm.ModuleOpts, opts: EmscriptenWasm.ModuleOpts,
) => M; ) => Promise<M>;
export function initEmscriptenModule<T extends EmscriptenWasm.Module>( export function initEmscriptenModule<T extends EmscriptenWasm.Module>(
moduleFactory: ModuleFactory<T>, moduleFactory: ModuleFactory<T>,
@@ -8,8 +8,7 @@ export function initEmscriptenModule<T extends EmscriptenWasm.Module>(
workerUrl?: string, workerUrl?: string,
mainUrl?: string, mainUrl?: string,
): Promise<T> { ): Promise<T> {
return new Promise((resolve) => { return moduleFactory({
const module = moduleFactory({
// Just to be safe, don't automatically invoke any wasm functions // Just to be safe, don't automatically invoke any wasm functions
mainScriptUrlOrBlob: mainUrl, mainScriptUrlOrBlob: mainUrl,
noInitialRun: true, noInitialRun: true,
@@ -19,14 +18,6 @@ export function initEmscriptenModule<T extends EmscriptenWasm.Module>(
if (url.endsWith('.worker.js')) return workerUrl!; if (url.endsWith('.worker.js')) return workerUrl!;
return url; 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);
},
});
}); });
} }