mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-11 16:26:20 +00:00
* Scaling works on native * It works in wasm * Integrate with UI * Remove benchmark * Integrate Hqx into Resizer module * Link against repo for hqx * Remove unused defaultOpts * Re-add test file * Adding size dropdown * Chrome: go and sit on the naughty step * Better docs * Review * Add link to crbug * Update src/codecs/processor-worker/index.ts Co-Authored-By: Jake Archibald <jaffathecake@gmail.com> * Terminate worker inbetween resize jobs
37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
type ModuleFactory<M extends EmscriptenWasm.Module> = (
|
|
opts: EmscriptenWasm.ModuleOpts,
|
|
) => 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);
|
|
},
|
|
});
|
|
});
|
|
}
|
|
|
|
interface ClampOpts {
|
|
min?: number;
|
|
max?: number;
|
|
}
|
|
|
|
export function clamp(x: number, opts: ClampOpts): number {
|
|
return Math.min(Math.max(x, opts.min || Number.MIN_VALUE), opts.max || Number.MAX_VALUE);
|
|
}
|