@@ -19,11 +19,13 @@ const {
19
19
const {
20
20
MessagePort,
21
21
MessageChannel,
22
+ broadcastChannel,
22
23
drainMessagePort,
23
24
moveMessagePortToContext,
24
25
receiveMessageOnPort : receiveMessageOnPort_ ,
25
26
stopMessagePort,
26
- checkMessagePort
27
+ checkMessagePort,
28
+ DOMException,
27
29
} = internalBinding ( 'messaging' ) ;
28
30
const {
29
31
getEnvMessagePort
@@ -41,14 +43,20 @@ const {
41
43
} = require ( 'internal/event_target' ) ;
42
44
const { inspect } = require ( 'internal/util/inspect' ) ;
43
45
const {
44
- ERR_INVALID_ARG_TYPE
45
- } = require ( 'internal/errors' ) . codes ;
46
+ codes : {
47
+ ERR_INVALID_ARG_TYPE ,
48
+ ERR_MISSING_ARGS ,
49
+ }
50
+ } = require ( 'internal/errors' ) ;
46
51
47
52
const kData = Symbol ( 'kData' ) ;
53
+ const kHandle = Symbol ( 'kHandle' ) ;
48
54
const kIncrementsPortRef = Symbol ( 'kIncrementsPortRef' ) ;
49
55
const kLastEventId = Symbol ( 'kLastEventId' ) ;
50
56
const kName = Symbol ( 'kName' ) ;
51
57
const kOrigin = Symbol ( 'kOrigin' ) ;
58
+ const kOnMessage = Symbol ( 'kOnMessage' ) ;
59
+ const kOnMessageError = Symbol ( 'kOnMessageError' ) ;
52
60
const kPort = Symbol ( 'kPort' ) ;
53
61
const kPorts = Symbol ( 'kPorts' ) ;
54
62
const kWaitingStreams = Symbol ( 'kWaitingStreams' ) ;
@@ -324,6 +332,76 @@ function receiveMessageOnPort(port) {
324
332
return { message } ;
325
333
}
326
334
335
+ function onMessageEvent ( type , data ) {
336
+ this . dispatchEvent ( new MessageEvent ( type , { data } ) ) ;
337
+ }
338
+
339
+ class BroadcastChannel extends EventTarget {
340
+ constructor ( name ) {
341
+ if ( arguments . length === 0 )
342
+ throw new ERR_MISSING_ARGS ( 'name' ) ;
343
+ super ( ) ;
344
+ this [ kName ] = `${ name } ` ;
345
+ this [ kHandle ] = broadcastChannel ( this [ kName ] ) ;
346
+ this [ kOnMessage ] = onMessageEvent . bind ( this , 'message' ) ;
347
+ this [ kOnMessageError ] = onMessageEvent . bind ( this , 'messageerror' ) ;
348
+ this [ kHandle ] . on ( 'message' , this [ kOnMessage ] ) ;
349
+ this [ kHandle ] . on ( 'messageerror' , this [ kOnMessageError ] ) ;
350
+ }
351
+
352
+ [ inspect . custom ] ( depth , options ) {
353
+ if ( depth < 0 )
354
+ return 'BroadcastChannel' ;
355
+
356
+ const opts = {
357
+ ...options ,
358
+ depth : options . depth == null ? null : options . depth - 1
359
+ } ;
360
+
361
+ return `BroadcastChannel ${ inspect ( {
362
+ name : this [ kName ] ,
363
+ active : this [ kHandle ] !== undefined ,
364
+ } , opts ) } `;
365
+ }
366
+
367
+ get name ( ) { return this [ kName ] ; }
368
+
369
+ close ( ) {
370
+ if ( this [ kHandle ] === undefined )
371
+ return ;
372
+ this [ kHandle ] . off ( 'message' , this [ kOnMessage ] ) ;
373
+ this [ kHandle ] . off ( 'messageerror' , this [ kOnMessageError ] ) ;
374
+ this [ kOnMessage ] = undefined ;
375
+ this [ kOnMessageError ] = undefined ;
376
+ this [ kHandle ] . close ( ) ;
377
+ this [ kHandle ] = undefined ;
378
+ }
379
+
380
+ postMessage ( message ) {
381
+ if ( arguments . length === 0 )
382
+ throw new ERR_MISSING_ARGS ( 'message' ) ;
383
+ if ( this [ kHandle ] === undefined )
384
+ throw new DOMException ( 'BroadcastChannel is closed.' ) ;
385
+ if ( this [ kHandle ] . postMessage ( message ) === undefined )
386
+ throw new DOMException ( 'Message could not be posted.' ) ;
387
+ }
388
+
389
+ ref ( ) {
390
+ if ( this [ kHandle ] )
391
+ this [ kHandle ] . ref ( ) ;
392
+ return this ;
393
+ }
394
+
395
+ unref ( ) {
396
+ if ( this [ kHandle ] )
397
+ this [ kHandle ] . unref ( ) ;
398
+ return this ;
399
+ }
400
+ }
401
+
402
+ defineEventHandler ( BroadcastChannel . prototype , 'message' ) ;
403
+ defineEventHandler ( BroadcastChannel . prototype , 'messageerror' ) ;
404
+
327
405
module . exports = {
328
406
drainMessagePort,
329
407
messageTypes,
@@ -339,5 +417,6 @@ module.exports = {
339
417
setupPortReferencing,
340
418
ReadableWorkerStdio,
341
419
WritableWorkerStdio,
342
- createWorkerStdio
420
+ createWorkerStdio,
421
+ BroadcastChannel,
343
422
} ;
0 commit comments