mirror of
https://github.com/nodejs/node.git
synced 2025-05-04 02:44:59 +00:00

Add `ev.ports` for spec compliancy. Since we only emit the raw `data` value, and only create the `MessageEvent` instance if there are EventTarget-style listeners, we store the ports list temporarily on the MessagePort object itself, so that we can look it up when we need to create the event object. Fixes: https://github.com/nodejs/node/issues/37358 PR-URL: https://github.com/nodejs/node/pull/37538 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
40 lines
995 B
JavaScript
40 lines
995 B
JavaScript
'use strict';
|
|
const {
|
|
SymbolFor,
|
|
} = primordials;
|
|
|
|
class MessageEvent {
|
|
constructor(data, target, type, ports) {
|
|
this.data = data;
|
|
this.target = target;
|
|
this.type = type;
|
|
this.ports = ports ?? [];
|
|
}
|
|
}
|
|
|
|
const kHybridDispatch = SymbolFor('nodejs.internal.kHybridDispatch');
|
|
const kCurrentlyReceivingPorts =
|
|
SymbolFor('nodejs.internal.kCurrentlyReceivingPorts');
|
|
|
|
exports.emitMessage = function(data, ports, type) {
|
|
if (typeof this[kHybridDispatch] === 'function') {
|
|
this[kCurrentlyReceivingPorts] = ports;
|
|
try {
|
|
this[kHybridDispatch](data, type, undefined);
|
|
} finally {
|
|
this[kCurrentlyReceivingPorts] = undefined;
|
|
}
|
|
return;
|
|
}
|
|
|
|
const event = new MessageEvent(data, this, type, ports);
|
|
if (type === 'message') {
|
|
if (typeof this.onmessage === 'function')
|
|
this.onmessage(event);
|
|
} else {
|
|
// eslint-disable-next-line no-lonely-if
|
|
if (typeof this.onmessageerror === 'function')
|
|
this.onmessageerror(event);
|
|
}
|
|
};
|