• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const {
3  SymbolFor,
4} = primordials;
5
6class MessageEvent {
7  constructor(data, target, type, ports) {
8    this.data = data;
9    this.target = target;
10    this.type = type;
11    this.ports = ports ?? [];
12  }
13}
14
15const kHybridDispatch = SymbolFor('nodejs.internal.kHybridDispatch');
16const kCurrentlyReceivingPorts =
17  SymbolFor('nodejs.internal.kCurrentlyReceivingPorts');
18
19exports.emitMessage = function(data, ports, type) {
20  if (typeof this[kHybridDispatch] === 'function') {
21    this[kCurrentlyReceivingPorts] = ports;
22    try {
23      this[kHybridDispatch](data, type, undefined);
24    } finally {
25      this[kCurrentlyReceivingPorts] = undefined;
26    }
27    return;
28  }
29
30  const event = new MessageEvent(data, this, type, ports);
31  if (type === 'message') {
32    if (typeof this.onmessage === 'function')
33      this.onmessage(event);
34  } else {
35    // eslint-disable-next-line no-lonely-if
36    if (typeof this.onmessageerror === 'function')
37      this.onmessageerror(event);
38  }
39};
40