* Send a message to the other end of the port * @param {any} message - The message to send (should be JSON-ifiable)
(message)
| 57 | * @param {any} message - The message to send (should be JSON-ifiable) |
| 58 | */ |
| 59 | postMessage(message) { |
| 60 | if (!this._isConnected) { |
| 61 | throw new Error('Attempting to use a disconnected port object'); |
| 62 | } |
| 63 | |
| 64 | if (this._otherPort?._isConnected) { |
| 65 | // Clone the message to simulate serialization |
| 66 | const clonedMessage = JSON.parse(JSON.stringify(message)); |
| 67 | |
| 68 | // Asynchronously deliver the message to simulate real behavior |
| 69 | setTimeout(async () => { |
| 70 | if (this._otherPort?._isConnected) { |
| 71 | await this._otherPort._triggerMessageEvent(clonedMessage, this); |
| 72 | } |
| 73 | }, 0); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Disconnect the port |