UNPKG

nanolith

Version:

Multi-threading in no time with seamless TypeScript support.

88 lines (87 loc) 3.91 kB
/// <reference types="node" resolution-mode="require"/> import { parentPort } from 'worker_threads'; import type { TransferListItem } from 'worker_threads'; import type { Awaitable } from '../types/utilities.js'; import type { Messenger } from '../messenger/index.js'; import type { RemoveListenerFunction } from '../types/messages.js'; import type { OnStreamCallback } from '../types/streams.js'; declare function sendMessage<Data = any>(data: Data, transferList?: readonly TransferListItem[]): void; declare function waitForMessage<Data = any>(callback: (body: Data) => Awaitable<boolean>): Promise<Data>; declare function onMessage<Data = any>(callback: (body: Data) => Awaitable<void>): RemoveListenerFunction; declare function onMessengerReceived(callback: (messenger: Messenger) => Awaitable<any>): () => import("worker_threads").MessagePort; declare function onStream(callback: OnStreamCallback<Exclude<typeof parentPort, null>>): RemoveListenerFunction; declare function createStream(metaData?: Record<string | number, any>): Promise<import("../streams/index.js").WritableToPort<import("worker_threads").MessagePort>>; /** * An object containing functions to be used within workers when communicating with the main thread. * * @example * ParentThread.sendMessage('hello from worker!'); * * ParentThread.onMessage<Record<string, string>>((data) => { * console.log(Object.values(data)); * }); */ export declare const ParentThread: Readonly<{ /** * @param data The data to send to the main thread. * @param transferList An optional array of {@link TransferListItem}s. See the * [Node.js documentation](https://nodejs.org/api/worker_threads.html#workerpostmessagevalue-transferlist) for more information. * * @example * ParentThread.sendMessage('foo'); * ParentThread.sendMessage({ hello: 'world' }); */ sendMessage: typeof sendMessage; /** * Listen for messages coming from the main thread. * * @param callback A function to run each time a message is received from the main thread. * * @returns A function that will remove the listener when called. * * @example * ParentThread.onMessage<string>((data) => console.log(data, 'received!')); */ onMessage: typeof onMessage; /** * Listen for {@link Messenger}s being sent to the worker from the main thread. * * @param callback A function to run each time a `Messenger` is received from the main thread. * * @returns A function that will remove the listener when called. * * @example * ParentThread.onMessengerReceived((messenger) => console.log(messenger.ID)); */ onMessengerReceived: typeof onMessengerReceived; /** * Wait for specific messages coming from the main thread. * * @param callback A function returning a boolean that will be run each time a message is received the main thread. * Once the condition is met and the function returns `true`, the promise will resolve with the data received. * * @returns A promise of the received data. * * @example * const data = await ParentThread.waitForMessage<{ foo: string }>(({ foo }) => foo === 'bar'); * * console.log(data); */ waitForMessage: typeof waitForMessage; /** * Receive data streams from the main thread. * * @param callback The callback to run once the stream has been initialized and is ready to consume. */ onStream: typeof onStream; /** * Create a {@link Writable} instance that can be piped into in order to stream data to * the main thread. The main thread can listen for incoming streams with the * `service.onStream()` listener. * * @param metaData Any specific data about the stream that should be accessible when * using it. */ createStream: typeof createStream; }>; export {};