engine/core renders with the LightCraft pipeline; engine/wasm runs it in the browser; workers/render replays op logs and renders originals natively. Op log with live fan-out and offline outbox; edited thumbnails and deliveries rendered from the original. Revelar page with Basic and optional modules, crop with aspect presets and straighten, before/after, undo/redo, copy/paste settings. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
48 lines
2.1 KiB
TypeScript
48 lines
2.1 KiB
TypeScript
/// <reference lib="webworker" />
|
|
import { loadEngine, type WasmPhoto, type WorkerReply, type WorkerRequest } from "./engine.js";
|
|
|
|
/**
|
|
* Decodes and renders off the main thread. Holds one photo (the one being edited); the page sends settings and
|
|
* gets pixels back. Requests are handled in order; the page keeps at most one render in flight.
|
|
*/
|
|
let current: { key: string; photo: WasmPhoto } | undefined;
|
|
const opening = new Map<string, Promise<WasmPhoto>>();
|
|
|
|
const post = (msg: WorkerReply, transfer: Transferable[] = []) => (self as unknown as DedicatedWorkerGlobalScope).postMessage(msg, transfer);
|
|
|
|
async function open(key: string, url: string): Promise<WasmPhoto> {
|
|
if (current?.key === key) return current.photo;
|
|
let p = opening.get(key);
|
|
if (!p) {
|
|
p = (async () => {
|
|
const [engine, res] = await Promise.all([loadEngine(), fetch(url)]);
|
|
if (!res.ok) throw new Error(`photo download failed (${res.status})`);
|
|
const photo = new engine.Photo(new Uint8Array(await res.arrayBuffer()), 2048);
|
|
current?.photo.free();
|
|
current = { key, photo };
|
|
return photo;
|
|
})();
|
|
opening.set(key, p);
|
|
p.finally(() => opening.delete(key)).catch(() => {});
|
|
}
|
|
return p;
|
|
}
|
|
|
|
self.onmessage = async (e: MessageEvent<WorkerRequest>) => {
|
|
const msg = e.data;
|
|
try {
|
|
if (msg.type === "open") {
|
|
const photo = await open(msg.key, msg.url);
|
|
post({ type: "opened", key: msg.key, sourceWidth: photo.sourceWidth, sourceHeight: photo.sourceHeight });
|
|
return;
|
|
}
|
|
const photo = current?.key === msg.key ? current.photo : await opening.get(msg.key);
|
|
if (!photo) throw new Error("photo not open");
|
|
photo.render(msg.settings, msg.maxW, msg.maxH, msg.draft, msg.applyCrop);
|
|
const pixels = new Uint8ClampedArray(photo.pixels().buffer);
|
|
post({ type: "frame", ticket: msg.ticket, key: msg.key, width: photo.width, height: photo.height, pixels }, [pixels.buffer]);
|
|
} catch (err) {
|
|
post({ type: "error", key: msg.key, ...(msg.type === "render" && { ticket: msg.ticket }), message: err instanceof Error ? err.message : String(err) });
|
|
}
|
|
};
|