6
6
} = internalBinding ( 'symbols' ) ;
7
7
const {
8
8
MessagePort,
9
- MessageChannel
9
+ MessageChannel,
10
+ drainMessagePort,
11
+ stopMessagePort
10
12
} = internalBinding ( 'messaging' ) ;
11
13
const { threadId } = internalBinding ( 'worker' ) ;
12
14
@@ -33,13 +35,6 @@ const messageTypes = {
33
35
LOAD_SCRIPT : 'loadScript'
34
36
} ;
35
37
36
- // Original drain from C++
37
- const originalDrain = MessagePort . prototype . drain ;
38
-
39
- function drainMessagePort ( port ) {
40
- return originalDrain . call ( port ) ;
41
- }
42
-
43
38
// We have to mess with the MessagePort prototype a bit, so that a) we can make
44
39
// it inherit from EventEmitter, even though it is a C++ class, and b) we do
45
40
// not provide methods that are not present in the Browser and not documented
@@ -51,9 +46,8 @@ const MessagePortPrototype = Object.create(
51
46
// Set up the new inheritance chain.
52
47
Object . setPrototypeOf ( MessagePort , EventEmitter ) ;
53
48
Object . setPrototypeOf ( MessagePort . prototype , EventEmitter . prototype ) ;
54
- // Finally, purge methods we don't want to be public.
55
- delete MessagePort . prototype . stop ;
56
- delete MessagePort . prototype . drain ;
49
+ // Copy methods that are inherited from HandleWrap, because
50
+ // changing the prototype of MessagePort.prototype implicitly removed them.
57
51
MessagePort . prototype . ref = MessagePortPrototype . ref ;
58
52
MessagePort . prototype . unref = MessagePortPrototype . unref ;
59
53
@@ -84,7 +78,7 @@ Object.defineProperty(MessagePort.prototype, 'onmessage', {
84
78
MessagePortPrototype . start . call ( this ) ;
85
79
} else {
86
80
this . unref ( ) ;
87
- MessagePortPrototype . stop . call ( this ) ;
81
+ stopMessagePort ( this ) ;
88
82
}
89
83
}
90
84
} ) ;
@@ -152,7 +146,7 @@ function setupPortReferencing(port, eventEmitter, eventName) {
152
146
} ) ;
153
147
eventEmitter . on ( 'removeListener' , ( name ) => {
154
148
if ( name === eventName && eventEmitter . listenerCount ( eventName ) === 0 ) {
155
- MessagePortPrototype . stop . call ( port ) ;
149
+ stopMessagePort ( port ) ;
156
150
port . unref ( ) ;
157
151
}
158
152
} ) ;
0 commit comments