@@ -142,6 +142,7 @@ export class Socket<
142
142
private fns : Array < ( event : Event , next : ( err ?: Error ) => void ) => void > = [ ] ;
143
143
private flags : BroadcastFlags = { } ;
144
144
private _anyListeners ?: Array < ( ...args : any [ ] ) => void > ;
145
+ private _anyOutgoingListeners ?: Array < ( ...args : any [ ] ) => void > ;
145
146
146
147
/**
147
148
* Interface to a `Client` for a given `Namespace`.
@@ -220,6 +221,7 @@ export class Socket<
220
221
const flags = Object . assign ( { } , this . flags ) ;
221
222
this . flags = { } ;
222
223
224
+ this . notifyOutgoingListeners ( packet ) ;
223
225
this . packet ( packet , flags ) ;
224
226
225
227
return true ;
@@ -710,8 +712,8 @@ export class Socket<
710
712
}
711
713
712
714
/**
713
- * Adds a listener that will be fired when any event is emitted . The event name is passed as the first argument to the
714
- * callback.
715
+ * Adds a listener that will be fired when any event is received . The event name is passed as the first argument to
716
+ * the callback.
715
717
*
716
718
* @param listener
717
719
* @public
@@ -723,8 +725,8 @@ export class Socket<
723
725
}
724
726
725
727
/**
726
- * Adds a listener that will be fired when any event is emitted . The event name is passed as the first argument to the
727
- * callback. The listener is added to the beginning of the listeners array.
728
+ * Adds a listener that will be fired when any event is received . The event name is passed as the first argument to
729
+ * the callback. The listener is added to the beginning of the listeners array.
728
730
*
729
731
* @param listener
730
732
* @public
@@ -736,7 +738,7 @@ export class Socket<
736
738
}
737
739
738
740
/**
739
- * Removes the listener that will be fired when any event is emitted .
741
+ * Removes the listener that will be fired when any event is received .
740
742
*
741
743
* @param listener
742
744
* @public
@@ -769,6 +771,114 @@ export class Socket<
769
771
return this . _anyListeners || [ ] ;
770
772
}
771
773
774
+ /**
775
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
776
+ * callback.
777
+ *
778
+ * @param listener
779
+ *
780
+ * <pre><code>
781
+ *
782
+ * socket.onAnyOutgoing((event, ...args) => {
783
+ * console.log(event);
784
+ * });
785
+ *
786
+ * </pre></code>
787
+ *
788
+ * @public
789
+ */
790
+ public onAnyOutgoing ( listener : ( ...args : any [ ] ) => void ) : this {
791
+ this . _anyOutgoingListeners = this . _anyOutgoingListeners || [ ] ;
792
+ this . _anyOutgoingListeners . push ( listener ) ;
793
+ return this ;
794
+ }
795
+
796
+ /**
797
+ * Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the
798
+ * callback. The listener is added to the beginning of the listeners array.
799
+ *
800
+ * @param listener
801
+ *
802
+ * <pre><code>
803
+ *
804
+ * socket.prependAnyOutgoing((event, ...args) => {
805
+ * console.log(event);
806
+ * });
807
+ *
808
+ * </pre></code>
809
+ *
810
+ * @public
811
+ */
812
+ public prependAnyOutgoing ( listener : ( ...args : any [ ] ) => void ) : this {
813
+ this . _anyOutgoingListeners = this . _anyOutgoingListeners || [ ] ;
814
+ this . _anyOutgoingListeners . unshift ( listener ) ;
815
+ return this ;
816
+ }
817
+
818
+ /**
819
+ * Removes the listener that will be fired when any event is emitted.
820
+ *
821
+ * @param listener
822
+ *
823
+ * <pre><code>
824
+ *
825
+ * const handler = (event, ...args) => {
826
+ * console.log(event);
827
+ * }
828
+ *
829
+ * socket.onAnyOutgoing(handler);
830
+ *
831
+ * // then later
832
+ * socket.offAnyOutgoing(handler);
833
+ *
834
+ * </pre></code>
835
+ *
836
+ * @public
837
+ */
838
+ public offAnyOutgoing ( listener ?: ( ...args : any [ ] ) => void ) : this {
839
+ if ( ! this . _anyOutgoingListeners ) {
840
+ return this ;
841
+ }
842
+ if ( listener ) {
843
+ const listeners = this . _anyOutgoingListeners ;
844
+ for ( let i = 0 ; i < listeners . length ; i ++ ) {
845
+ if ( listener === listeners [ i ] ) {
846
+ listeners . splice ( i , 1 ) ;
847
+ return this ;
848
+ }
849
+ }
850
+ } else {
851
+ this . _anyOutgoingListeners = [ ] ;
852
+ }
853
+ return this ;
854
+ }
855
+
856
+ /**
857
+ * Returns an array of listeners that are listening for any event that is specified. This array can be manipulated,
858
+ * e.g. to remove listeners.
859
+ *
860
+ * @public
861
+ */
862
+ public listenersAnyOutgoing ( ) {
863
+ return this . _anyOutgoingListeners || [ ] ;
864
+ }
865
+
866
+ /**
867
+ * Notify the listeners for each packet sent (emit or broadcast)
868
+ *
869
+ * @param packet
870
+ *
871
+ * @private
872
+ */
873
+ private notifyOutgoingListeners ( packet : Packet ) {
874
+ if ( this . _anyOutgoingListeners && this . _anyOutgoingListeners . length ) {
875
+ const listeners = this . _anyOutgoingListeners . slice ( ) ;
876
+ for ( const listener of listeners ) {
877
+ listener . apply ( this , packet . data ) ;
878
+ }
879
+ }
880
+ }
881
+
772
882
private newBroadcastOperator ( ) : BroadcastOperator < EmitEvents , SocketData > {
773
883
const flags = Object . assign ( { } , this . flags ) ;
774
884
this . flags = { } ;
0 commit comments