Skip to content

Commit 40519b3

Browse files
authored
🤖 Merge PR DefinitelyTyped#73621 node/worker_threads: Export postMessageToThread as a top-level function in worker_threads by @y-hsgw
1 parent 2840242 commit 40519b3

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

‎types/node/test/worker_threads.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,11 @@ import { createContext } from "node:vm";
240240
const arrayBuffer = new ArrayBuffer(0);
241241
structuredClone({ test: arrayBuffer }, { transfer: [arrayBuffer] }); // $ExpectType { test: ArrayBuffer; }
242242
}
243+
244+
{
245+
const { port1 } = new workerThreads.MessageChannel();
246+
workerThreads.postMessageToThread(10, { port: port1 }, [port1], 1000);
247+
workerThreads.postMessageToThread(10, { port: port1 }, [port1]);
248+
workerThreads.postMessageToThread(10, { x: 100 }, 1000);
249+
workerThreads.postMessageToThread(10, { x: 100 });
250+
}

‎types/node/v20/test/worker_threads.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,11 @@ import { createContext } from "node:vm";
223223
const arrayBuffer = new ArrayBuffer(0);
224224
structuredClone({ test: arrayBuffer }, { transfer: [arrayBuffer] }); // $ExpectType { test: ArrayBuffer; }
225225
}
226+
227+
{
228+
const { port1 } = new workerThreads.MessageChannel();
229+
workerThreads.postMessageToThread(10, { port: port1 }, [port1], 1000);
230+
workerThreads.postMessageToThread(10, { port: port1 }, [port1]);
231+
workerThreads.postMessageToThread(10, { x: 100 }, 1000);
232+
workerThreads.postMessageToThread(10, { x: 100 });
233+
}

‎types/node/v20/worker_threads.d.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,24 @@ declare module "worker_threads" {
676676
* for the `key` will be deleted.
677677
*/
678678
function setEnvironmentData(key: Serializable, value?: Serializable): void;
679+
/**
680+
* Sends a value to another worker, identified by its thread ID.
681+
* @param threadId The target thread ID. If the thread ID is invalid, a `ERR_WORKER_MESSAGING_FAILED` error will be thrown.
682+
* If the target thread ID is the current thread ID, a `ERR_WORKER_MESSAGING_SAME_THREAD` error will be thrown.
683+
* @param value The value to send.
684+
* @param transferList If one or more `MessagePort`-like objects are passed in value, a `transferList` is required for those items
685+
* or `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` is thrown. See `port.postMessage()` for more information.
686+
* @param timeout Time to wait for the message to be delivered in milliseconds. By default it's `undefined`, which means wait forever.
687+
* If the operation times out, a `ERR_WORKER_MESSAGING_TIMEOUT` error is thrown.
688+
* @since v22.5.0
689+
*/
690+
function postMessageToThread(threadId: number, value: any, timeout?: number): Promise<void>;
691+
function postMessageToThread(
692+
threadId: number,
693+
value: any,
694+
transferList: readonly Transferable[],
695+
timeout?: number,
696+
): Promise<void>;
679697

680698
import {
681699
BroadcastChannel as _BroadcastChannel,

‎types/node/v22/test/worker_threads.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,11 @@ import { createContext } from "node:vm";
240240
const arrayBuffer = new ArrayBuffer(0);
241241
structuredClone({ test: arrayBuffer }, { transfer: [arrayBuffer] }); // $ExpectType { test: ArrayBuffer; }
242242
}
243+
244+
{
245+
const { port1 } = new workerThreads.MessageChannel();
246+
workerThreads.postMessageToThread(10, { port: port1 }, [port1], 1000);
247+
workerThreads.postMessageToThread(10, { port: port1 }, [port1]);
248+
workerThreads.postMessageToThread(10, { x: 100 }, 1000);
249+
workerThreads.postMessageToThread(10, { x: 100 });
250+
}

‎types/node/v22/worker_threads.d.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,24 @@ declare module "worker_threads" {
738738
* for the `key` will be deleted.
739739
*/
740740
function setEnvironmentData(key: Serializable, value?: Serializable): void;
741+
/**
742+
* Sends a value to another worker, identified by its thread ID.
743+
* @param threadId The target thread ID. If the thread ID is invalid, a `ERR_WORKER_MESSAGING_FAILED` error will be thrown.
744+
* If the target thread ID is the current thread ID, a `ERR_WORKER_MESSAGING_SAME_THREAD` error will be thrown.
745+
* @param value The value to send.
746+
* @param transferList If one or more `MessagePort`-like objects are passed in value, a `transferList` is required for those items
747+
* or `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` is thrown. See `port.postMessage()` for more information.
748+
* @param timeout Time to wait for the message to be delivered in milliseconds. By default it's `undefined`, which means wait forever.
749+
* If the operation times out, a `ERR_WORKER_MESSAGING_TIMEOUT` error is thrown.
750+
* @since v22.5.0
751+
*/
752+
function postMessageToThread(threadId: number, value: any, timeout?: number): Promise<void>;
753+
function postMessageToThread(
754+
threadId: number,
755+
value: any,
756+
transferList: readonly Transferable[],
757+
timeout?: number,
758+
): Promise<void>;
741759

742760
import {
743761
BroadcastChannel as _BroadcastChannel,

‎types/node/worker_threads.d.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,24 @@ declare module "worker_threads" {
739739
* for the `key` will be deleted.
740740
*/
741741
function setEnvironmentData(key: Serializable, value?: Serializable): void;
742+
/**
743+
* Sends a value to another worker, identified by its thread ID.
744+
* @param threadId The target thread ID. If the thread ID is invalid, a `ERR_WORKER_MESSAGING_FAILED` error will be thrown.
745+
* If the target thread ID is the current thread ID, a `ERR_WORKER_MESSAGING_SAME_THREAD` error will be thrown.
746+
* @param value The value to send.
747+
* @param transferList If one or more `MessagePort`-like objects are passed in value, a `transferList` is required for those items
748+
* or `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` is thrown. See `port.postMessage()` for more information.
749+
* @param timeout Time to wait for the message to be delivered in milliseconds. By default it's `undefined`, which means wait forever.
750+
* If the operation times out, a `ERR_WORKER_MESSAGING_TIMEOUT` error is thrown.
751+
* @since v22.5.0
752+
*/
753+
function postMessageToThread(threadId: number, value: any, timeout?: number): Promise<void>;
754+
function postMessageToThread(
755+
threadId: number,
756+
value: any,
757+
transferList: readonly Transferable[],
758+
timeout?: number,
759+
): Promise<void>;
742760

743761
import {
744762
BroadcastChannel as _BroadcastChannel,

0 commit comments

Comments
 (0)