Offload functions to worker threads with shared memory primitives for Node.js.
1import { PromiseLikeTask } from './task.ts';
2import { AsyncIterableTask } from './stream-task.ts';
3import type { Task, WorkerHandle } from './runner.ts';
4
5/**
6 * Returns a copy of the task pinned to a specific worker.
7 * The original task is unchanged.
8 * @param worker - The worker handle to pin the task to.
9 * @param task - The task to assign.
10 * @returns A new task with the same id and args, pinned to the given worker.
11 */
12export function assign<T>(worker: WorkerHandle, task: Task<T>): Task<T> {
13 if (task instanceof AsyncIterableTask) {
14 const copy = new AsyncIterableTask(task.id, task.args);
15 copy.worker = worker;
16 return copy as unknown as Task<T>;
17 }
18 const copy = new PromiseLikeTask(task.id, task.args);
19 copy.worker = worker;
20 return copy as unknown as Task<T>;
21}