/// 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: Data, transferList?: readonly TransferListItem[]): void; declare function waitForMessage(callback: (body: Data) => Awaitable): Promise; declare function onMessage(callback: (body: Data) => Awaitable): RemoveListenerFunction; declare function onMessengerReceived(callback: (messenger: Messenger) => Awaitable): () => import("worker_threads").MessagePort; declare function onStream(callback: OnStreamCallback>): RemoveListenerFunction; declare function createStream(metaData?: Record): Promise>; /** * An object containing functions to be used within workers when communicating with the main thread. * * @example * ParentThread.sendMessage('hello from worker!'); * * ParentThread.onMessage>((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((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 {};