export class WorkerAssetJobs {
    pipeline;
    worker;
    constructor(pipeline, worker) {
        this.pipeline = pipeline;
        this.worker = worker;
    }
    async run(job) {
        if (job.signal?.aborted) {
            throw new Error(`Worker asset job aborted: ${job.id}`);
        }
        const runPromise = this.worker
            ? this.worker.run(job)
            : this.pipeline.run(job.input, { url: job.url, signal: job.signal });
        if (!job.signal) {
            return runPromise;
        }
        return this.withAbort(job, runPromise);
    }
    async withAbort(job, promise) {
        if (job.signal?.aborted) {
            throw new Error(`Worker asset job aborted: ${job.id}`);
        }
        return new Promise((resolve, reject) => {
            const abort = () => reject(new Error(`Worker asset job aborted: ${job.id}`));
            job.signal?.addEventListener("abort", abort, { once: true });
            promise.then((value) => {
                job.signal?.removeEventListener("abort", abort);
                resolve(value);
            }, (error) => {
                job.signal?.removeEventListener("abort", abort);
                reject(error);
            });
        });
    }
}
//# sourceMappingURL=WorkerAssetJobs.js.map