|
|
@@ -5,6 +5,13 @@
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
+import EventInfo from './eventinfo.js';
|
|
|
+import utils from './utils.js';
|
|
|
+
|
|
|
+// Saves how many callbacks has been already added. Does not decrement when callback is removed.
|
|
|
+// Used internally as a unique id for a callback.
|
|
|
+let eventsCounter = 0;
|
|
|
+
|
|
|
/**
|
|
|
* Mixin that injects the events API into its host.
|
|
|
*
|
|
|
@@ -12,9 +19,6 @@
|
|
|
* @singleton
|
|
|
*/
|
|
|
|
|
|
-import EventInfo from './eventinfo.js';
|
|
|
-import utils from './utils.js';
|
|
|
-
|
|
|
const EmitterMixin = {
|
|
|
/**
|
|
|
* Registers a callback function to be executed when an event is fired.
|
|
|
@@ -37,7 +41,9 @@ const EmitterMixin = {
|
|
|
callback = {
|
|
|
callback: callback,
|
|
|
ctx: ctx || this,
|
|
|
- priority: priority
|
|
|
+ priority: priority,
|
|
|
+ // Save counter value as unique id.
|
|
|
+ counter: ++eventsCounter
|
|
|
};
|
|
|
|
|
|
// Add the callback to the list in the right priority position.
|
|
|
@@ -228,7 +234,15 @@ const EmitterMixin = {
|
|
|
args = Array.prototype.slice.call( arguments, 1 );
|
|
|
args.unshift( eventInfo );
|
|
|
|
|
|
+ // Save how many callbacks were added at the moment when the event has been fired.
|
|
|
+ const counter = eventsCounter;
|
|
|
+
|
|
|
for ( let i = 0; i < callbacks.length; i++ ) {
|
|
|
+ // Filter out callbacks that have been added after event has been fired.
|
|
|
+ if ( callbacks[ i ].counter > counter ) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
callbacks[ i ].callback.apply( callbacks[ i ].ctx, args );
|
|
|
|
|
|
// Remove the callback from future requests if off() has been called.
|
|
|
@@ -282,4 +296,4 @@ function getCallbacksIfAny( source, event ) {
|
|
|
}
|
|
|
|
|
|
return callbacks;
|
|
|
-}
|
|
|
+}
|