| 1 |
"use strict"; |
| 2 |
// Copyright 2021 Google LLC. Use of this source code is governed by an |
| 3 |
// MIT-style license that can be found in the LICENSE file or at |
| 4 |
// https://opensource.org/licenses/MIT. |
| 5 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 |
exports.SyncMessagePort = exports.TimeoutException = void 0; |
| 7 |
const events_1 = require("events"); |
| 8 |
const worker_threads_1 = require("worker_threads"); |
| 9 |
const atomic_counter_1 = require("./atomic_counter"); |
| 10 |
/** |
| 11 |
* An exception thrown by {@link SyncMessagePort.receiveMessage} if a message |
| 12 |
* isn't received within {@link ReceivedMessageOptions.timeout} milliseconds. |
| 13 |
*/ |
| 14 |
class TimeoutException extends Error { |
| 15 |
constructor(message) { |
| 16 |
super(message); |
| 17 |
} |
| 18 |
} |
| 19 |
exports.TimeoutException = TimeoutException; |
| 20 |
/** |
| 21 |
* A communication port that can receive messages synchronously from another |
| 22 |
* `SyncMessagePort`. |
| 23 |
* |
| 24 |
* This also emits the same asynchronous events as `MessagePort`. Messages are |
| 25 |
* preferentially sent to {@link receiveMessage} if a call to it is outstanding, |
| 26 |
* and only sent to the event handler if they weren't received synchronously. |
| 27 |
*/ |
| 28 |
class SyncMessagePort extends events_1.EventEmitter { |
| 29 |
port; |
| 30 |
/** Creates a channel whose ports can be passed to `new SyncMessagePort()`. */ |
| 31 |
static createChannel() { |
| 32 |
const channel = new worker_threads_1.MessageChannel(); |
| 33 |
// 16 bytes is required for `AtomicCounter`. |
| 34 |
const buffer1 = new SharedArrayBuffer(16); |
| 35 |
const buffer2 = new SharedArrayBuffer(16); |
| 36 |
// Queue up messages on each port so the caller doesn't have to explicitly |
| 37 |
// pass the buffer around along with them. |
| 38 |
channel.port1.postMessage(buffer1); |
| 39 |
channel.port1.postMessage(buffer2); |
| 40 |
channel.port2.postMessage(buffer2); |
| 41 |
channel.port2.postMessage(buffer1); |
| 42 |
return channel; |
| 43 |
} |
| 44 |
/** |
| 45 |
* An atomic counter of messages posted yet to be received. |
| 46 |
*/ |
| 47 |
postCounter; |
| 48 |
/** |
| 49 |
* An atomic counter of messages available to be received. |
| 50 |
*/ |
| 51 |
receiveCounter; |
| 52 |
/** |
| 53 |
* Creates a new message port. The `port` must be created by |
| 54 |
* `SyncMessagePort.createChannel()` and must connect to a port passed to |
| 55 |
* another `SyncMessagePort` in another worker. |
| 56 |
*/ |
| 57 |
constructor(port) { |
| 58 |
super(); |
| 59 |
this.port = port; |
| 60 |
const buffer1 = (0, worker_threads_1.receiveMessageOnPort)(this.port)?.message; |
| 61 |
const buffer2 = (0, worker_threads_1.receiveMessageOnPort)(this.port)?.message; |
| 62 |
if (!buffer1 || !buffer2) { |
| 63 |
throw new Error('new SyncMessagePort() must be passed a port from ' + |
| 64 |
'SyncMessagePort.createChannel().'); |
| 65 |
} |
| 66 |
this.postCounter = new atomic_counter_1.AtomicCounter(buffer1); |
| 67 |
this.receiveCounter = new atomic_counter_1.AtomicCounter(buffer2); |
| 68 |
const messageHandler = () => { |
| 69 |
this.receiveCounter.wait(); |
| 70 |
this.receiveCounter.decrement(); |
| 71 |
}; |
| 72 |
this.port.on('messageerror', (error) => { |
| 73 |
messageHandler(); |
| 74 |
if (!this.listenerCount('messageerror')) { |
| 75 |
throw error; |
| 76 |
} |
| 77 |
}); |
| 78 |
this.on('newListener', (event, listener) => { |
| 79 |
if (event === 'message' && !this.listenerCount(event)) { |
| 80 |
this.port.on(event, messageHandler); |
| 81 |
} |
| 82 |
this.port.on(event, listener); |
| 83 |
}); |
| 84 |
this.on('removeListener', (event, listener) => { |
| 85 |
this.port.removeListener(event, listener); |
| 86 |
if (event === 'message' && !this.listenerCount(event)) { |
| 87 |
this.port.removeListener(event, messageHandler); |
| 88 |
} |
| 89 |
}); |
| 90 |
} |
| 91 |
/** See `MessagePort.postMesage()`. */ |
| 92 |
postMessage(value, transferList) { |
| 93 |
// @ts-expect-error: TypeScript gets confused with the overloads. |
| 94 |
this.port.postMessage(value, transferList); |
| 95 |
this.postCounter.increment(); |
| 96 |
} |
| 97 |
/** |
| 98 |
* Returns the message sent by the other port, if one is available. This *does |
| 99 |
* not* block, and will return `undefined` immediately if no message is |
| 100 |
* available. In order to distinguish between a message with value `undefined` |
| 101 |
* and no message, a message is return in an object with a `message` field. |
| 102 |
* |
| 103 |
* It does *not* throw an error if the port is closed when this is called; |
| 104 |
* instead, it just returns `undefined`. |
| 105 |
*/ |
| 106 |
receiveMessageIfAvailable() { |
| 107 |
const message = (0, worker_threads_1.receiveMessageOnPort)(this.port); |
| 108 |
if (message) { |
| 109 |
this.receiveCounter.wait(); |
| 110 |
this.receiveCounter.decrement(); |
| 111 |
} |
| 112 |
return message; |
| 113 |
} |
| 114 |
/** |
| 115 |
* Blocks and returns the next message sent by the other port. |
| 116 |
* |
| 117 |
* Throws an error if the channel is closed and all messages are drained, |
| 118 |
* including if it closes while this is waiting for a message, unless |
| 119 |
* {@link ReceiveMessageOptions.closedValue} is passed. |
| 120 |
*/ |
| 121 |
receiveMessage(options) { |
| 122 |
if (!this.receiveCounter.wait(options?.timeout)) { |
| 123 |
if ('timeoutValue' in options) |
| 124 |
return options.timeoutValue; |
| 125 |
throw new TimeoutException('SyncMessagePort.receiveMessage() timed out.'); |
| 126 |
} |
| 127 |
const message = (0, worker_threads_1.receiveMessageOnPort)(this.port); |
| 128 |
if (message) { |
| 129 |
this.receiveCounter.decrement(); |
| 130 |
return message.message; |
| 131 |
} |
| 132 |
// The port is closed and all remaining messages are drained. |
| 133 |
if (options && 'closedValue' in options) |
| 134 |
return options.closedValue; |
| 135 |
throw new Error("The SyncMessagePort's channel is closed."); |
| 136 |
} |
| 137 |
/** See `MessagePort.close()`. */ |
| 138 |
close() { |
| 139 |
this.port.close(); |
| 140 |
this.postCounter.close(); |
| 141 |
this.receiveCounter.close(); |
| 142 |
} |
| 143 |
} |
| 144 |
exports.SyncMessagePort = SyncMessagePort; |
| 145 |
//# sourceMappingURL=index.js.map |