@@ -58,7 +58,7 @@ var Events = {
58
58
plotObj . removeAllListeners = ev . removeAllListeners . bind ( ev ) ;
59
59
60
60
/*
61
- * Create funtions for managing internal events. These are *only* triggered
61
+ * Create functions for managing internal events. These are *only* triggered
62
62
* by the mirroring of external events via the emit function.
63
63
*/
64
64
plotObj . _internalOn = internalEv . on . bind ( internalEv ) ;
@@ -93,6 +93,7 @@ var Events = {
93
93
triggerHandler : function ( plotObj , event , data ) {
94
94
var jQueryHandlerValue ;
95
95
var nodeEventHandlerValue ;
96
+
96
97
/*
97
98
* If jQuery exists run all its handlers for this event and
98
99
* collect the return value of the LAST handler function
@@ -110,30 +111,41 @@ var Events = {
110
111
var handlers = ev . _events [ event ] ;
111
112
if ( ! handlers ) return jQueryHandlerValue ;
112
113
113
- /*
114
- * handlers can be function or an array of functions
115
- */
116
- if ( typeof handlers === 'function' ) handlers = [ handlers ] ;
117
- var lastHandler = handlers . pop ( ) ;
118
-
119
- /*
120
- * Call all the handlers except the last one.
121
- */
122
- for ( var i = 0 ; i < handlers . length ; i ++ ) {
123
- handlers [ i ] ( data ) ;
114
+ // making sure 'this' is the EventEmitter instance
115
+ function apply ( handler ) {
116
+ // The 'once' case, we can't just call handler() as we need
117
+ // the return value here. So,
118
+ // - remove handler
119
+ // - call listener and grab return value!
120
+ // - stash 'fired' key to not call handler twice
121
+ if ( handler . listener ) {
122
+ ev . removeListener ( event , handler . listener ) ;
123
+ if ( ! handler . fired ) {
124
+ handler . fired = true ;
125
+ return handler . listener . apply ( ev , [ data ] ) ;
126
+ }
127
+ } else {
128
+ return handler . apply ( ev , [ data ] ) ;
129
+ }
124
130
}
125
131
126
- /*
127
- * Now call the final handler and collect its value
128
- */
129
- nodeEventHandlerValue = lastHandler ( data ) ;
132
+ // handlers can be function or an array of functions
133
+ handlers = Array . isArray ( handlers ) ? handlers : [ handlers ] ;
134
+
135
+ var i ;
136
+ for ( i = 0 ; i < handlers . length - 1 ; i ++ ) {
137
+ apply ( handlers [ i ] ) ;
138
+ }
139
+ // now call the final handler and collect its value
140
+ nodeEventHandlerValue = apply ( handlers [ i ] ) ;
130
141
131
142
/*
132
143
* Return either the jQuery handler value if it exists or the
133
144
* nodeEventHandler value. jQuery event value supersedes nodejs
134
145
* events for backwards compatibility reasons.
135
146
*/
136
- return jQueryHandlerValue !== undefined ? jQueryHandlerValue :
147
+ return jQueryHandlerValue !== undefined ?
148
+ jQueryHandlerValue :
137
149
nodeEventHandlerValue ;
138
150
} ,
139
151
0 commit comments