MCPcopy
hub / github.com/callstack/agent-device / runWorkerJob

Function runWorkerJob

src/utils/png-worker-client.ts:138–168  ·  view source on GitHub ↗

* 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>,
)

Source from the content-addressed store, hash-verified

136 * the reconstructed job `AppError` for job-level failures (e.g. corrupt PNG).
137 */
138function 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. */
171async function runPngJob<Kind extends PngWorkerJobKind>(

Callers 1

runPngJobFunction · 0.85

Calls 4

obtainWorkerFunction · 0.85
updateWorkerRefFunction · 0.85
setMethod · 0.80
deleteMethod · 0.80

Tested by

no test coverage detected