@@ -12,6 +12,9 @@ public class EventEmitter
1212 private readonly Dictionary < string , List < Action < dynamic > > > _eventListeners = new Dictionary < string , List < Action < dynamic > > > ( ) ;
1313 private readonly List < Action < string , dynamic > > _generalListeners = new List < Action < string , dynamic > > ( ) ;
1414
15+ private readonly Dictionary < string , List < Action < string > > > _rawEventListeners = new Dictionary < string , List < Action < string > > > ( ) ;
16+ private readonly List < Action < string , string > > _rawGeneralListeners = new List < Action < string , string > > ( ) ;
17+
1518 /// <summary>
1619 /// Binds to a given event name
1720 /// </summary>
@@ -30,6 +33,24 @@ public void Bind(string eventName, Action<dynamic> listener)
3033 }
3134 }
3235
36+ /// <summary>
37+ /// Binds to a given event name. The listener will receive the raw JSON message.
38+ /// </summary>
39+ /// <param name="eventName">The Event Name to listen for</param>
40+ /// <param name="listener">The action to perform when the event occurs</param>
41+ public void Bind ( string eventName , Action < string > listener )
42+ {
43+ if ( _rawEventListeners . ContainsKey ( eventName ) )
44+ {
45+ _rawEventListeners [ eventName ] . Add ( listener ) ;
46+ }
47+ else
48+ {
49+ var listeners = new List < Action < string > > { listener } ;
50+ _rawEventListeners . Add ( eventName , listeners ) ;
51+ }
52+ }
53+
3354 /// <summary>
3455 /// Binds to ALL event
3556 /// </summary>
@@ -39,13 +60,23 @@ public void BindAll(Action<string, dynamic> listener)
3960 _generalListeners . Add ( listener ) ;
4061 }
4162
63+ /// <summary>
64+ /// Binds to ALL event. The listener will receive the raw JSON message.
65+ /// </summary>
66+ /// <param name="listener">The action to perform when the any event occurs</param>
67+ public void BindAll ( Action < string , string > listener )
68+ {
69+ _rawGeneralListeners . Add ( listener ) ;
70+ }
71+
4272 /// <summary>
4373 /// Removes the binding for the given event name
4474 /// </summary>
4575 /// <param name="eventName">The name of the event to unbind</param>
4676 public void Unbind ( string eventName )
4777 {
4878 _eventListeners . Remove ( eventName ) ;
79+ _rawEventListeners . Remove ( eventName ) ;
4980 }
5081
5182 /// <summary>
@@ -55,36 +86,68 @@ public void Unbind(string eventName)
5586 /// <param name="listener">The action to remove</param>
5687 public void Unbind ( string eventName , Action < dynamic > listener )
5788 {
58- if ( _eventListeners . ContainsKey ( eventName ) )
89+ if ( _eventListeners . ContainsKey ( eventName ) )
5990 {
6091 _eventListeners [ eventName ] . Remove ( listener ) ;
6192 }
6293 }
6394
95+ /// <summary>
96+ /// Remove the action for the event name
97+ /// </summary>
98+ /// <param name="eventName">The name of the event to unbind</param>
99+ /// <param name="listener">The action to remove</param>
100+ public void Unbind ( string eventName , Action < string > listener )
101+ {
102+ if ( _rawEventListeners . ContainsKey ( eventName ) )
103+ {
104+ _rawEventListeners [ eventName ] . Remove ( listener ) ;
105+ }
106+ }
107+
64108 /// <summary>
65109 /// Remove All bindings
66110 /// </summary>
67111 public void UnbindAll ( )
68112 {
69113 _eventListeners . Clear ( ) ;
70114 _generalListeners . Clear ( ) ;
115+
116+ _rawEventListeners . Clear ( ) ;
117+ _rawGeneralListeners . Clear ( ) ;
71118 }
72119
73120 internal void EmitEvent ( string eventName , string data )
74121 {
75- var obj = JsonConvert . DeserializeObject < dynamic > ( data ) ;
122+ foreach ( var action in _rawGeneralListeners )
123+ {
124+ action ( eventName , data ) ;
125+ }
76126
77- // Emit to general listeners regardless of event type
78- foreach ( var action in _generalListeners )
127+ if ( _rawEventListeners . ContainsKey ( eventName ) )
79128 {
80- action ( eventName , obj ) ;
129+ foreach ( var action in _rawEventListeners [ eventName ] )
130+ {
131+ action ( data ) ;
132+ }
81133 }
82134
83- if ( _eventListeners . ContainsKey ( eventName ) )
135+ // Don't bother with deserialization if there are no dynamic listeners
136+ if ( _generalListeners . Count > 0 || _eventListeners . Count > 0 )
84137 {
85- foreach ( var action in _eventListeners [ eventName ] )
138+ var obj = JsonConvert . DeserializeObject < dynamic > ( data ) ;
139+
140+ foreach ( var action in _generalListeners )
141+ {
142+ action ( eventName , obj ) ;
143+ }
144+
145+ if ( _eventListeners . ContainsKey ( eventName ) )
86146 {
87- action ( obj ) ;
147+ foreach ( var action in _eventListeners [ eventName ] )
148+ {
149+ action ( obj ) ;
150+ }
88151 }
89152 }
90153 }
0 commit comments