(threadId, value, transferList, timeout)
| 189 | } |
| 190 | |
| 191 | async 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 | |
| 236 | module.exports = { |
| 237 | createMainThreadPort, |
searching dependent graphs…