66} = internalBinding ( 'symbols' ) ;
77const {
88 MessagePort,
9- MessageChannel
9+ MessageChannel,
10+ drainMessagePort,
11+ stopMessagePort
1012} = internalBinding ( 'messaging' ) ;
1113const { threadId } = internalBinding ( 'worker' ) ;
1214
@@ -33,13 +35,6 @@ const messageTypes = {
3335 LOAD_SCRIPT : 'loadScript'
3436} ;
3537
36- // Original drain from C++
37- const originalDrain = MessagePort . prototype . drain ;
38-
39- function drainMessagePort ( port ) {
40- return originalDrain . call ( port ) ;
41- }
42-
4338// We have to mess with the MessagePort prototype a bit, so that a) we can make
4439// it inherit from EventEmitter, even though it is a C++ class, and b) we do
4540// not provide methods that are not present in the Browser and not documented
@@ -51,9 +46,8 @@ const MessagePortPrototype = Object.create(
5146// Set up the new inheritance chain.
5247Object . setPrototypeOf ( MessagePort , EventEmitter ) ;
5348Object . 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.
5751MessagePort . prototype . ref = MessagePortPrototype . ref ;
5852MessagePort . prototype . unref = MessagePortPrototype . unref ;
5953
@@ -84,7 +78,7 @@ Object.defineProperty(MessagePort.prototype, 'onmessage', {
8478 MessagePortPrototype . start . call ( this ) ;
8579 } else {
8680 this . unref ( ) ;
87- MessagePortPrototype . stop . call ( this ) ;
81+ stopMessagePort ( this ) ;
8882 }
8983 }
9084} ) ;
@@ -152,7 +146,7 @@ function setupPortReferencing(port, eventEmitter, eventName) {
152146 } ) ;
153147 eventEmitter . on ( 'removeListener' , ( name ) => {
154148 if ( name === eventName && eventEmitter . listenerCount ( eventName ) === 0 ) {
155- MessagePortPrototype . stop . call ( port ) ;
149+ stopMessagePort ( port ) ;
156150 port . unref ( ) ;
157151 }
158152 } ) ;
0 commit comments