@@ -86,6 +86,7 @@ export class Socket extends EventEmitter {
86
86
> = [ ] ;
87
87
private flags : BroadcastFlags = { } ;
88
88
private _rooms : Set < Room > = new Set ( ) ;
89
+ private _anyListeners : Array < ( ...args : any [ ] ) => void > ;
89
90
90
91
/**
91
92
* Interface to a `Client` for a given `Namespace`.
@@ -335,7 +336,13 @@ export class Socket extends EventEmitter {
335
336
args . push ( this . ack ( packet . id ) ) ;
336
337
}
337
338
338
- this . dispatch ( args ) ;
339
+ if ( this . _anyListeners && this . _anyListeners . length ) {
340
+ const listeners = this . _anyListeners . slice ( ) ;
341
+ for ( const listener of listeners ) {
342
+ listener . apply ( this , args ) ;
343
+ }
344
+ }
345
+ super . emit . apply ( this , args ) ;
339
346
}
340
347
341
348
/**
@@ -502,86 +509,87 @@ export class Socket extends EventEmitter {
502
509
}
503
510
504
511
/**
505
- * Dispatch incoming event to socket listeners .
512
+ * A reference to the request that originated the underlying Engine.IO Socket .
506
513
*
507
- * @param {Array } event - event that will get emitted
508
- * @private
514
+ * @public
509
515
*/
510
- private dispatch ( event : Array < string > ) : void {
511
- debug ( "dispatching an event %j" , event ) ;
512
- this . run ( event , err => {
513
- process . nextTick ( ( ) => {
514
- if ( err ) {
515
- return this . _error ( err . message ) ;
516
- }
517
- super . emit . apply ( this , event ) ;
518
- } ) ;
519
- } ) ;
516
+ public get request ( ) : IncomingMessage {
517
+ return this . client . request ;
520
518
}
521
519
522
520
/**
523
- * Sets up socket middleware .
521
+ * A reference to the underlying Client transport connection (Engine.IO Socket object) .
524
522
*
525
- * @param {Function } fn - middleware function (event, next)
526
- * @return {Socket } self
527
523
* @public
528
524
*/
529
- public use (
530
- fn : ( event : Array < any > , next : ( err : Error ) => void ) => void
531
- ) : Socket {
532
- this . fns . push ( fn ) ;
533
- return this ;
525
+ public get conn ( ) {
526
+ return this . client . conn ;
534
527
}
535
528
536
529
/**
537
- * Executes the middleware for an incoming event.
538
- *
539
- * @param {Array } event - event that will get emitted
540
- * @param {Function } fn - last fn call in the middleware
541
- * @private
530
+ * @public
542
531
*/
543
- private run ( event : Array < any > , fn : ( err : Error ) => void ) {
544
- const fns = this . fns . slice ( 0 ) ;
545
- if ( ! fns . length ) return fn ( null ) ;
546
-
547
- function run ( i ) {
548
- fns [ i ] ( event , function ( err ) {
549
- // upon error, short-circuit
550
- if ( err ) return fn ( err ) ;
551
-
552
- // if no middleware left, summon callback
553
- if ( ! fns [ i + 1 ] ) return fn ( null ) ;
554
-
555
- // go on to next
556
- run ( i + 1 ) ;
557
- } ) ;
558
- }
532
+ public get rooms ( ) : Set < Room > {
533
+ return this . adapter . socketRooms ( this . id ) || new Set ( ) ;
534
+ }
559
535
560
- run ( 0 ) ;
536
+ /**
537
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
538
+ * callback.
539
+ *
540
+ * @param listener
541
+ * @public
542
+ */
543
+ public onAny ( listener : ( ...args : any [ ] ) => void ) : Socket {
544
+ this . _anyListeners = this . _anyListeners || [ ] ;
545
+ this . _anyListeners . push ( listener ) ;
546
+ return this ;
561
547
}
562
548
563
549
/**
564
- * A reference to the request that originated the underlying Engine.IO Socket.
550
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
551
+ * callback. The listener is added to the beginning of the listeners array.
565
552
*
553
+ * @param listener
566
554
* @public
567
555
*/
568
- public get request ( ) : IncomingMessage {
569
- return this . client . request ;
556
+ public prependAny ( listener : ( ...args : any [ ] ) => void ) : Socket {
557
+ this . _anyListeners = this . _anyListeners || [ ] ;
558
+ this . _anyListeners . unshift ( listener ) ;
559
+ return this ;
570
560
}
571
561
572
562
/**
573
- * A reference to the underlying Client transport connection (Engine.IO Socket object) .
563
+ * Removes the listener that will be fired when any event is emitted .
574
564
*
565
+ * @param listener
575
566
* @public
576
567
*/
577
- public get conn ( ) {
578
- return this . client . conn ;
568
+ public offAny ( listener ?: ( ...args : any [ ] ) => void ) : Socket {
569
+ if ( ! this . _anyListeners ) {
570
+ return this ;
571
+ }
572
+ if ( listener ) {
573
+ const listeners = this . _anyListeners ;
574
+ for ( let i = 0 ; i < listeners . length ; i ++ ) {
575
+ if ( listener === listeners [ i ] ) {
576
+ listeners . splice ( i , 1 ) ;
577
+ return this ;
578
+ }
579
+ }
580
+ } else {
581
+ this . _anyListeners = [ ] ;
582
+ }
583
+ return this ;
579
584
}
580
585
581
586
/**
587
+ * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
588
+ * e.g. to remove listeners.
589
+ *
582
590
* @public
583
591
*/
584
- public get rooms ( ) : Set < Room > {
585
- return this . adapter . socketRooms ( this . id ) || new Set ( ) ;
592
+ public listenersAny ( ) {
593
+ return this . _anyListeners || [ ] ;
586
594
}
587
595
}
0 commit comments