Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promisify emscripten modules & fix webp examples #817

Merged
merged 4 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codecs/avif/dec/avif_dec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AVIFModule>;

2 changes: 1 addition & 1 deletion codecs/avif/enc/avif_enc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AVIFModule>;
2 changes: 1 addition & 1 deletion codecs/imagequant/imagequant.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<QuantizerModule>;
2 changes: 1 addition & 1 deletion codecs/mozjpeg_enc/mozjpeg_enc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MozJPEGModule>;
14 changes: 6 additions & 8 deletions codecs/webp/dec/example.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<!doctype html>
<script src='webp_dec.js'></script>
<script>
const Module = webp_dec();

async function loadFile(src) {
const resp = await fetch(src);
return await resp.arrayBuffer();
}

Module.onRuntimeInitialized = async _ => {
console.log('Version:', Module.version().toString(16));
webp_dec().then(async module => {
console.log('Version:', module.version().toString(16));
const image = await loadFile('../../example.webp');
const imageData = Module.decode(image);
const imageData = module.decode(image);
const canvas = document.createElement('canvas');
canvas.width = result.width;
canvas.height = result.height;
canvas.width = imageData.width;
canvas.height = imageData.height;
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
};
});
</script>
2 changes: 1 addition & 1 deletion codecs/webp/dec/webp_dec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebPModule>;
6 changes: 2 additions & 4 deletions codecs/webp/enc/example.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<!doctype html>
<script src='webp_enc.js'></script>
<script>
const module = webp_enc();

async function loadImage(src) {
// Load image
const img = document.createElement('img');
Expand All @@ -17,7 +15,7 @@
return ctx.getImageData(0, 0, img.width, img.height);
}

module.onRuntimeInitialized = async _ => {
webp_enc().then(async module => {
console.log('Version:', module.version().toString(16));
const image = await loadImage('../../example.png');
const result = module.encode(image.data, image.width, image.height, {
Expand Down Expand Up @@ -56,5 +54,5 @@
const img = document.createElement('img');
img.src = blobURL;
document.body.appendChild(img);
};
});
</script>
2 changes: 1 addition & 1 deletion codecs/webp/enc/webp_enc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebPModule>;
27 changes: 9 additions & 18 deletions src/codecs/util.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
type ModuleFactory<M extends EmscriptenWasm.Module> = (
opts: EmscriptenWasm.ModuleOpts,
) => M;
) => Promise<M>;

export function initEmscriptenModule<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);
},
});
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;
},
});
}

Expand Down