mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-15 01:59:57 +00:00
wip
# Conflicts: # codecs/cpp.Dockerfile # codecs/imagequant/example.html # codecs/webp/dec/webp_dec.d.ts # codecs/webp/dec/webp_dec.js # codecs/webp/dec/webp_dec.wasm # codecs/webp/enc/webp_enc.d.ts # codecs/webp/enc/webp_enc.js # codecs/webp/enc/webp_enc.wasm # package-lock.json # package.json # src/codecs/tiny.webp # src_old/codecs/encoders.ts # src_old/codecs/processor-worker/tiny.avif # src_old/codecs/processor-worker/tiny.webp # src_old/codecs/tiny.webp # src_old/components/compress/index.tsx # src_old/lib/util.ts # src_old/sw/util.ts
This commit is contained in:
39
src_old/codecs/util.ts
Normal file
39
src_old/codecs/util.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
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,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user