* Sends one job to the worker. Rejects with `PngWorkerUnavailableError` for * worker-infrastructure failures (the single unavailability channel) and with * the reconstructed job `AppError` for job-level failures (e.g. corrupt PNG).
( job: PngWorkerJobFor<Kind>, )
| 136 | * the reconstructed job `AppError` for job-level failures (e.g. corrupt PNG). |
| 137 | */ |
| 138 | function runWorkerJob<Kind extends PngWorkerJobKind>( |
| 139 | job: PngWorkerJobFor<Kind>, |
| 140 | ): Promise<PngWorkerJobResultFor<Kind>> { |
| 141 | const activeWorker = obtainWorker(); |
| 142 | if (!activeWorker) { |
| 143 | return Promise.reject(new PngWorkerUnavailableError('PNG worker is unavailable')); |
| 144 | } |
| 145 | nextJobId += 1; |
| 146 | const id = nextJobId; |
| 147 | return new Promise<PngWorkerJobResultFor<Kind>>((resolve, reject) => { |
| 148 | pendingJobs.set(id, { |
| 149 | // The worker answers each request id with the result of the same kind. |
| 150 | resolve: resolve as (result: PngWorkerJobResult) => void, |
| 151 | reject, |
| 152 | }); |
| 153 | updateWorkerRef(); |
| 154 | try { |
| 155 | activeWorker.postMessage({ ...job, id }); |
| 156 | } catch (error) { |
| 157 | // Job-specific send failure (e.g. DataCloneError): fall back to the sync |
| 158 | // path for this call without permanently disabling the worker. |
| 159 | pendingJobs.delete(id); |
| 160 | updateWorkerRef(); |
| 161 | reject( |
| 162 | new PngWorkerUnavailableError( |
| 163 | `failed to post job to PNG worker: ${error instanceof Error ? error.message : String(error)}`, |
| 164 | ), |
| 165 | ); |
| 166 | } |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | /** Runs a job on the worker, falling back to `runSync` when it is unavailable. */ |
| 171 | async function runPngJob<Kind extends PngWorkerJobKind>( |
no test coverage detected