MCPcopy Index your code
hub / github.com/nodejs/node / postMessageToThread

Function postMessageToThread

lib/internal/worker/messaging.js:191–234  ·  view source on GitHub ↗
(threadId, value, transferList, timeout)

Source from the content-addressed store, hash-verified

189}
190
191async function postMessageToThread(threadId, value, transferList, timeout) {
192 if (typeof transferList === 'number' && typeof timeout === 'undefined') {
193 timeout = transferList;
194 transferList = [];
195 }
196
197 if (typeof timeout !== 'undefined') {
198 validateNumber(timeout, 'timeout', 0);
199 }
200
201 if (threadId === currentThreadId) {
202 throw new ERR_WORKER_MESSAGING_SAME_THREAD();
203 }
204
205 const memory = constructSharedArrayBuffer(WORKER_MESSAGING_SHARED_DATA);
206 const status = new Int32Array(memory);
207 const promise = AtomicsWaitAsync(status, WORKER_MESSAGING_STATUS_INDEX, 0, timeout).value;
208
209 const message = {
210 type: messageTypes.SEND_MESSAGE_TO_WORKER,
211 source: currentThreadId,
212 destination: threadId,
213 value,
214 memory,
215 transferList,
216 };
217
218 if (isMainThread) {
219 handleMessageFromThread(message);
220 } else {
221 mainThreadPort.postMessage(message, transferList);
222 }
223
224 // Wait for the response
225 const response = await promise;
226
227 if (response === 'timed-out') {
228 throw new ERR_WORKER_MESSAGING_TIMEOUT();
229 } else if (status[WORKER_MESSAGING_RESULT_INDEX] === WORKER_MESSAGING_RESULT_NO_LISTENERS) {
230 throw new ERR_WORKER_MESSAGING_FAILED();
231 } else if (status[WORKER_MESSAGING_RESULT_INDEX] === WORKER_MESSAGING_RESULT_LISTENER_ERROR) {
232 throw new ERR_WORKER_MESSAGING_ERRORED();
233 }
234}
235
236module.exports = {
237 createMainThreadPort,

Callers 4

pingFunction · 0.85
testFunction · 0.85
testFunction · 0.85
testFunction · 0.85

Calls 2

handleMessageFromThreadFunction · 0.85
postMessageMethod · 0.45

Tested by 3

testFunction · 0.68
testFunction · 0.68
testFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…