mirror of
https://github.com/nodejs/node.git
synced 2025-05-22 22:11:41 +00:00

Use `NodeEventTarget` to provide a mixed `EventEmitter`/`EventTarget` API interface. PR-URL: https://github.com/nodejs/node/pull/34057 Refs: https://twitter.com/addaleax/status/1276289101671608320 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
32 lines
726 B
JavaScript
32 lines
726 B
JavaScript
'use strict';
|
|
const {
|
|
SymbolFor,
|
|
} = primordials;
|
|
|
|
class MessageEvent {
|
|
constructor(data, target, type) {
|
|
this.data = data;
|
|
this.target = target;
|
|
this.type = type;
|
|
}
|
|
}
|
|
|
|
const kHybridDispatch = SymbolFor('nodejs.internal.kHybridDispatch');
|
|
|
|
exports.emitMessage = function(data, type) {
|
|
if (typeof this[kHybridDispatch] === 'function') {
|
|
this[kHybridDispatch](data, type, undefined);
|
|
return;
|
|
}
|
|
|
|
const event = new MessageEvent(data, this, type);
|
|
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);
|
|
}
|
|
};
|