Debian Codegraph @

node/sync-message-port/dist/lib/index.d.ts

raw
1 import { EventEmitter } from 'events';
2 import { MessageChannel, MessagePort, Transferable } from 'worker_threads';
3 /**
4 * Options that can be passed to {@link SyncMessagePort.receiveMessage}.
5 */
6 export interface ReceiveMessageOptions {
7 /**
8 * The time (in milliseconds) to wait for a message before returning {@link
9 * timeoutValue} (if set) or throwing a [TimeoutException] otherwise.
10 */
11 timeout?: number;
12 /**
13 * If a message isn't received within {@link timeout} milliseconds, this value
14 * is returned. Ignored if {@link timeout} is not set.
15 */
16 timeoutValue?: unknown;
17 /**
18 * If the underlying channel is closed before calling {@link
19 * SyncMessagePort.receiveMessage} or while a call is pending, return this
20 * value.
21 */
22 closedValue?: unknown;
23 }
24 /**
25 * An exception thrown by {@link SyncMessagePort.receiveMessage} if a message
26 * isn't received within {@link ReceivedMessageOptions.timeout} milliseconds.
27 */
28 export declare class TimeoutException extends Error {
29 constructor(message: string);
30 }
31 /**
32 * A communication port that can receive messages synchronously from another
33 * `SyncMessagePort`.
34 *
35 * This also emits the same asynchronous events as `MessagePort`. Messages are
36 * preferentially sent to {@link receiveMessage} if a call to it is outstanding,
37 * and only sent to the event handler if they weren't received synchronously.
38 */
39 export declare class SyncMessagePort extends EventEmitter {
40 private readonly port;
41 /** Creates a channel whose ports can be passed to `new SyncMessagePort()`. */
42 static createChannel(): MessageChannel;
43 /**
44 * An atomic counter of messages posted yet to be received.
45 */
46 private readonly postCounter;
47 /**
48 * An atomic counter of messages available to be received.
49 */
50 private readonly receiveCounter;
51 /**
52 * Creates a new message port. The `port` must be created by
53 * `SyncMessagePort.createChannel()` and must connect to a port passed to
54 * another `SyncMessagePort` in another worker.
55 */
56 constructor(port: MessagePort);
57 /** See `MessagePort.postMesage()`. */
58 postMessage(value: unknown, transferList?: Transferable[]): void;
59 /**
60 * Returns the message sent by the other port, if one is available. This *does
61 * not* block, and will return `undefined` immediately if no message is
62 * available. In order to distinguish between a message with value `undefined`
63 * and no message, a message is return in an object with a `message` field.
64 *
65 * It does *not* throw an error if the port is closed when this is called;
66 * instead, it just returns `undefined`.
67 */
68 receiveMessageIfAvailable(): {
69 message: unknown;
70 } | undefined;
71 /**
72 * Blocks and returns the next message sent by the other port.
73 *
74 * Throws an error if the channel is closed and all messages are drained,
75 * including if it closes while this is waiting for a message, unless
76 * {@link ReceiveMessageOptions.closedValue} is passed.
77 */
78 receiveMessage(options?: ReceiveMessageOptions): unknown;
79 /** See `MessagePort.close()`. */
80 close(): void;
81 }