Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw). Local database, uploaded photos, build output and dependencies are gitignored. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
import { RAW_IDLE_MS, RAW_WORKERS } from './config.mjs';
|
||||
import { installLibrawRuntime } from './libraw-runtime.mjs';
|
||||
|
||||
let ctorPromise = null;
|
||||
|
||||
/** Load the decoder class once; installs the Node worker shim on first use. */
|
||||
export function loadLibRaw() {
|
||||
if (!ctorPromise) {
|
||||
ctorPromise = (async () => {
|
||||
await installLibrawRuntime();
|
||||
const mod = await import('libraw-wasm');
|
||||
const ctor = mod.default?.default ?? mod.default;
|
||||
if (typeof ctor !== 'function') throw new Error('libraw-wasm did not export a constructor');
|
||||
return ctor;
|
||||
})();
|
||||
}
|
||||
return ctorPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bounded pool of decoder instances. Each instance owns one worker thread, so
|
||||
* this caps how much demosaicing runs at the same time; extra jobs queue.
|
||||
* Idle instances are disposed after RAW_IDLE_MS to give the memory back.
|
||||
*/
|
||||
class LibRawPool {
|
||||
constructor(size = RAW_WORKERS) {
|
||||
this.size = Math.max(1, Math.floor(size));
|
||||
this.free = [];
|
||||
this.total = 0;
|
||||
this.waiters = [];
|
||||
this.closed = false;
|
||||
}
|
||||
|
||||
async acquire() {
|
||||
const entry = this.free.pop();
|
||||
if (entry) {
|
||||
if (entry.timer) clearTimeout(entry.timer);
|
||||
return entry.lib;
|
||||
}
|
||||
if (this.total < this.size) {
|
||||
this.total++;
|
||||
try {
|
||||
const Ctor = await loadLibRaw();
|
||||
return new Ctor();
|
||||
} catch (err) {
|
||||
this.total--;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
return new Promise((resolve, reject) => this.waiters.push({ resolve, reject }));
|
||||
}
|
||||
|
||||
release(lib, healthy = true) {
|
||||
if (this.closed || !healthy) {
|
||||
this.discard(lib);
|
||||
return;
|
||||
}
|
||||
const waiter = this.waiters.shift();
|
||||
if (waiter) {
|
||||
waiter.resolve(lib);
|
||||
return;
|
||||
}
|
||||
const entry = { lib, timer: null };
|
||||
entry.timer = setTimeout(() => {
|
||||
const idx = this.free.indexOf(entry);
|
||||
if (idx >= 0) {
|
||||
this.free.splice(idx, 1);
|
||||
this.discard(lib);
|
||||
}
|
||||
}, RAW_IDLE_MS);
|
||||
entry.timer.unref?.();
|
||||
this.free.push(entry);
|
||||
}
|
||||
|
||||
discard(lib) {
|
||||
try {
|
||||
lib.dispose();
|
||||
} catch {
|
||||
/* worker already gone */
|
||||
}
|
||||
this.total = Math.max(0, this.total - 1);
|
||||
const waiter = this.waiters.shift();
|
||||
if (waiter) {
|
||||
this.total++;
|
||||
loadLibRaw()
|
||||
.then((Ctor) => new Ctor())
|
||||
.then(waiter.resolve, waiter.reject);
|
||||
}
|
||||
}
|
||||
|
||||
/** Run `fn` with a decoder instance; the instance returns to the pool after. */
|
||||
async use(fn) {
|
||||
const lib = await this.acquire();
|
||||
let healthy = true;
|
||||
try {
|
||||
return await fn(lib);
|
||||
} catch (err) {
|
||||
// A dead worker keeps failing, so recycle it instead of handing it back.
|
||||
if (/worker|terminated|disposed|memory/i.test(String(err?.message ?? ''))) healthy = false;
|
||||
throw err;
|
||||
} finally {
|
||||
this.release(lib, healthy);
|
||||
}
|
||||
}
|
||||
|
||||
get stats() {
|
||||
return {
|
||||
size: this.size,
|
||||
busy: this.total - this.free.length,
|
||||
idle: this.free.length,
|
||||
queued: this.waiters.length,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const rawPool = new LibRawPool();
|
||||
|
||||
export function poolSupport() {
|
||||
return { size: rawPool.size, workers: RAW_WORKERS };
|
||||
}
|
||||
Reference in New Issue
Block a user