@@ -86,6 +86,7 @@ export class Socket extends EventEmitter {
8686 > = [ ] ;
8787 private flags : BroadcastFlags = { } ;
8888 private _rooms : Set < Room > = new Set ( ) ;
89+ private _anyListeners : Array < ( ...args : any [ ] ) => void > ;
8990
9091 /**
9192 * Interface to a `Client` for a given `Namespace`.
@@ -335,7 +336,13 @@ export class Socket extends EventEmitter {
335336 args . push ( this . ack ( packet . id ) ) ;
336337 }
337338
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 ) ;
339346 }
340347
341348 /**
@@ -502,86 +509,87 @@ export class Socket extends EventEmitter {
502509 }
503510
504511 /**
505- * Dispatch incoming event to socket listeners .
512+ * A reference to the request that originated the underlying Engine.IO Socket .
506513 *
507- * @param {Array } event - event that will get emitted
508- * @private
514+ * @public
509515 */
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 ;
520518 }
521519
522520 /**
523- * Sets up socket middleware .
521+ * A reference to the underlying Client transport connection (Engine.IO Socket object) .
524522 *
525- * @param {Function } fn - middleware function (event, next)
526- * @return {Socket } self
527523 * @public
528524 */
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 ;
534527 }
535528
536529 /**
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
542531 */
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+ }
559535
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 ;
561547 }
562548
563549 /**
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.
565552 *
553+ * @param listener
566554 * @public
567555 */
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 ;
570560 }
571561
572562 /**
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 .
574564 *
565+ * @param listener
575566 * @public
576567 */
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 ;
579584 }
580585
581586 /**
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+ *
582590 * @public
583591 */
584- public get rooms ( ) : Set < Room > {
585- return this . adapter . socketRooms ( this . id ) || new Set ( ) ;
592+ public listenersAny ( ) {
593+ return this . _anyListeners || [ ] ;
586594 }
587595}
0 commit comments