forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageport.js
More file actions
39 lines (35 loc) · 995 Bytes
/
Copy pathmessageport.js
File metadata and controls
39 lines (35 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'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);
}
};