MCPcopy Create free account
hub / github.com/nodejs/node / setupPortReferencing

Function setupPortReferencing

lib/internal/worker/io.js:211–247  ·  view source on GitHub ↗
(port, eventEmitter, eventName)

Source from the content-addressed store, hash-verified

209});
210
211function setupPortReferencing(port, eventEmitter, eventName) {
212 // Keep track of whether there are any workerMessage listeners:
213 // If there are some, ref() the channel so it keeps the event loop alive.
214 // If there are none or all are removed, unref() the channel so the worker
215 // can shutdown gracefully.
216 port.unref();
217 eventEmitter.on('newListener', function(name) {
218 if (name === eventName) newListener(eventEmitter.listenerCount(name));
219 });
220 eventEmitter.on('removeListener', function(name) {
221 if (name === eventName) removeListener(eventEmitter.listenerCount(name));
222 });
223 const origNewListener = eventEmitter[kNewListener];
224 setOwnProperty(eventEmitter, kNewListener, function(size, type, ...args) {
225 if (type === eventName) newListener(size - 1);
226 return ReflectApply(origNewListener, this, arguments);
227 });
228 const origRemoveListener = eventEmitter[kRemoveListener];
229 setOwnProperty(eventEmitter, kRemoveListener, function(size, type, ...args) {
230 if (type === eventName) removeListener(size);
231 return ReflectApply(origRemoveListener, this, arguments);
232 });
233
234 function newListener(size) {
235 if (size === 0) {
236 port.ref();
237 FunctionPrototypeCall(MessagePortPrototype.start, port);
238 }
239 }
240
241 function removeListener(size) {
242 if (size === 0) {
243 stopMessagePort(port);
244 port.unref();
245 }
246 }
247}
248
249
250class ReadableWorkerStdio extends Readable {

Callers 2

constructorMethod · 0.85
oninitFunction · 0.85

Calls 6

removeListenerFunction · 0.85
setOwnPropertyFunction · 0.85
listenerCountMethod · 0.80
newListenerFunction · 0.70
unrefMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected