import { StatusCode } from "../models/index.js"; import type { IThread as IThread, WorkerThreadFn } from "../models/thread.js"; import { Worker as WorkerImpl } from "../internals/NodeWorker.js"; import { Queue } from "../sync/queue.js"; export declare const yieldMicrotask: () => Promise; declare const ThreadType: { readonly Inline: "inline"; readonly InlineBlob: "inline-blob"; readonly File: "file"; }; export declare abstract class AbstractThread implements IThread { protected src: any; protected config: any; protected abstract channel: MessagePort; protected abstract handle: InstanceType; protected abstract options: IWorkerOptions & { eval?: boolean; }; protected abstract resolvers: Queue<{ resolve: (value?: Output | PromiseLike) => void; reject: (reason?: unknown) => void; }>; protected abstract type: (typeof ThreadType)[keyof typeof ThreadType]; constructor(src: URL | string, config: { maxConcurrency?: number | null; type?: "module" | undefined; once?: boolean; id?: number; }); constructor(src: string | URL, options: { type?: "module" | undefined; once?: boolean | undefined; id?: number | undefined; maxConcurrency?: number | undefined; }); constructor(src: WorkerThreadFn, config: { maxConcurrency?: number | null; type?: "module" | undefined; once?: boolean; id?: number; }); abstract terminate(): Promise; abstract send(...data: Args extends any[] ? Args : Args[]): Promise; onMessage(callback: (data: Output) => void): () => void; } /** * A default implementation of a worker thread, which is used for both the `InlineThread` and the `Thread` classes. * * @example * import { ThreadImpl } from 'nanothreads'; * * const thread = new ThreadImpl(...) * */ export declare class ThreadImpl extends AbstractThread { protected config: { maxConcurrency?: number | null; type?: "module" | undefined; once?: boolean; id?: number; }; private _activeCount; protected channel: MessagePort; protected handle: InstanceType; protected options: IWorkerOptions & { eval?: boolean | undefined; }; protected resolvers: Queue<{ resolve: (value?: Output | PromiseLike) => void; reject: (reason?: unknown) => void; }>; protected src: string | WorkerThreadFn; protected type: "inline" | "file" | "inline-blob"; private pool; protected taskFactory: (...data: Args extends any[] ? Args : [...args: Args[]]) => Promise; constructor(src: URL | string, config: { maxConcurrency?: number | null; type?: "module" | undefined; once?: boolean; id?: number; }); constructor(src: (...args: any[]) => Output, config: { maxConcurrency?: number | null; type?: "module" | undefined; once?: boolean; id?: number; }); constructor(src: WorkerThreadFn, config: { maxConcurrency?: number | null; type?: "module" | undefined; once?: boolean; id?: number; }); /** Returns how many tasks are waiting to be processed. */ get activeCount(): number; /** * The ID for the thread. * */ get id(): number; /** * If a concurrency limit is set, `isBusy` will return `true` if `activeCount >= maxConcurrency`. * Otherwise, it will return false. * */ get isBusy(): boolean; get maxConcurrency(): number | null | undefined; private onmessage; protected createTaskFactory: () => (...data: Args extends any[] ? Args : [...args: Args[]]) => Promise; private preparePayload; send(...data: Args extends any[] ? Args : [...args: Args[]]): Promise; onMessage(callback: (data: Output) => void): () => void; terminate(): Promise; } /** * Creates a new thread using a specified script * @example * ```ts * import { Thread } from 'nanothreads'; * * const handle = new Thread<[number, number], number>('./worker.js'); * await handle.send(4, 1); // output: 5 * * ``` */ export declare class Thread extends ThreadImpl { constructor(src: string | URL, options: { type?: "module" | undefined; once?: boolean; id?: number; maxConcurrency?: number; }); } /** * Creates a new thread using an inline function. * @example * ```ts * import { InlineThread } from 'nanothreads'; * * const handle = new InlineThread<[number, number], number>((a, b) => a + b); * await handle.send(4, 1); // output: 5 * ``` */ export declare class InlineThread extends ThreadImpl { constructor(src: WorkerThreadFn, options: { once?: boolean; type?: "module" | undefined; id?: number; maxConcurrency?: number; }); } export {};