mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 01:13:23 -04:00
[Events] [SimpleEmitter] added testing helpers
This commit is contained in:
parent
4258f49d7b
commit
da9949ec59
3 changed files with 85 additions and 53 deletions
|
@ -2,10 +2,10 @@ define(function (require) {
|
|||
var _ = require('lodash');
|
||||
|
||||
return function EventsProvider(Private, Promise, Notifier) {
|
||||
var BaseObject = Private(require('factories/base_object'));
|
||||
var SimpleEmitter = require('utils/SimpleEmitter');
|
||||
var notify = new Notifier({ location: 'EventEmitter' });
|
||||
|
||||
_(Events).inherits(BaseObject);
|
||||
_(Events).inherits(SimpleEmitter);
|
||||
function Events() {
|
||||
Events.Super.call(this);
|
||||
this._listeners = {};
|
||||
|
@ -89,6 +89,16 @@ define(function (require) {
|
|||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a list of the handler functions for a specific event
|
||||
*
|
||||
* @param {string} name
|
||||
* @return {array[function]}
|
||||
*/
|
||||
Events.prototype.listeners = function (name) {
|
||||
return _.pluck(this._listeners[name], 'handler');
|
||||
};
|
||||
|
||||
return Events;
|
||||
};
|
||||
});
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
define(function (require) {
|
||||
var _ = require('lodash');
|
||||
var BaseObject = require('utils/BaseObject');
|
||||
|
||||
/**
|
||||
* Simple event emitter class used in the vislib. Calls
|
||||
|
@ -7,6 +8,7 @@ define(function (require) {
|
|||
*
|
||||
* @class
|
||||
*/
|
||||
_(SimpleEmitter).inherits(BaseObject);
|
||||
function SimpleEmitter() {
|
||||
this._listeners = {};
|
||||
}
|
||||
|
@ -14,17 +16,15 @@ define(function (require) {
|
|||
/**
|
||||
* Add an event handler
|
||||
*
|
||||
* @param {string} event
|
||||
* @param {string} name
|
||||
* @param {function} handler
|
||||
* @return {SimpleEmitter} - this, for chaining
|
||||
*/
|
||||
SimpleEmitter.prototype.on = function (event, handler) {
|
||||
var handlers = this._listeners[event];
|
||||
if (!handlers) handlers = this._listeners[event] = [];
|
||||
SimpleEmitter.prototype.on = function (name, handler) {
|
||||
var handlers = this._listeners[name];
|
||||
if (!handlers) handlers = this._listeners[name] = [];
|
||||
|
||||
if (!_.contains(handlers, handler)) {
|
||||
handlers.push(handler);
|
||||
}
|
||||
handlers.push(handler);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
@ -32,61 +32,24 @@ define(function (require) {
|
|||
/**
|
||||
* Remove an event handler
|
||||
*
|
||||
* @param {string} event
|
||||
* @param {string} name
|
||||
* @param {function} [handler] - optional handler to remove, if no handler is
|
||||
* passed then all are removed
|
||||
* @return {SimpleEmitter} - this, for chaining
|
||||
*/
|
||||
SimpleEmitter.prototype.off = function (event, handler) {
|
||||
if (!this._listeners[event]) {
|
||||
SimpleEmitter.prototype.off = function (name, handler) {
|
||||
if (!this._listeners[name]) {
|
||||
return this;
|
||||
}
|
||||
|
||||
// remove a specific handler
|
||||
if (handler) _.pull(this._listeners[event], handler);
|
||||
if (handler) _.pull(this._listeners[name], handler);
|
||||
// or remove all listeners
|
||||
else this._listeners[event] = null;
|
||||
else this._listeners[name] = null;
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Emit an event and all arguments to all listeners for an event name
|
||||
*
|
||||
* @param {string} event
|
||||
* @param {*} [arg...] - any number of arguments that will be applied to each handler
|
||||
* @return {SimpleEmitter} - this, for chaining
|
||||
*/
|
||||
SimpleEmitter.prototype.emit = function (event, arg) {
|
||||
if (!this._listeners[event]) return this;
|
||||
|
||||
var args = _.rest(arguments);
|
||||
var handlers = this._listeners[event].slice(0);
|
||||
var i = -1;
|
||||
|
||||
while (++i < handlers.length) {
|
||||
handlers[i].apply(this, args);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the count of handlers for a specific event
|
||||
*
|
||||
* @param {string} [event] - optional event name to filter by
|
||||
* @return {number}
|
||||
*/
|
||||
SimpleEmitter.prototype.listenerCount = function (event) {
|
||||
if (event) {
|
||||
return _.size(this._listeners[event]);
|
||||
}
|
||||
|
||||
return _.reduce(this._listeners, function (count, handlers) {
|
||||
return count + _.size(handlers);
|
||||
}, 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove all event listeners bound to this emitter.
|
||||
*
|
||||
|
@ -97,5 +60,64 @@ define(function (require) {
|
|||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Emit an event and all arguments to all listeners for an event name
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {*} [arg...] - any number of arguments that will be applied to each handler
|
||||
* @return {SimpleEmitter} - this, for chaining
|
||||
*/
|
||||
SimpleEmitter.prototype.emit = function (name, arg) {
|
||||
if (!this._listeners[name]) return this;
|
||||
|
||||
var args = _.rest(arguments);
|
||||
var listeners = this.listeners(name);
|
||||
var i = -1;
|
||||
|
||||
while (++i < listeners.length) {
|
||||
listeners[i].apply(this, args);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a list of the event names that currently have listeners
|
||||
*
|
||||
* @return {array[string]}
|
||||
*/
|
||||
SimpleEmitter.prototype.activeEvents = function () {
|
||||
return _.reduce(this._listeners, function (active, listeners, name) {
|
||||
return active.concat(_.size(listeners) ? name : []);
|
||||
}, []);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a list of the handler functions for a specific event
|
||||
*
|
||||
* @param {string} name
|
||||
* @return {array[function]}
|
||||
*/
|
||||
SimpleEmitter.prototype.listeners = function (name) {
|
||||
return this._listeners[name] ? this._listeners[name].slice(0) : [];
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the count of handlers for a specific event
|
||||
*
|
||||
* @param {string} [name] - optional event name to filter by
|
||||
* @return {number}
|
||||
*/
|
||||
SimpleEmitter.prototype.listenerCount = function (name) {
|
||||
if (name) {
|
||||
return _.size(this._listeners[name]);
|
||||
}
|
||||
|
||||
return _.reduce(this._listeners, function (count, handlers) {
|
||||
return count + _.size(handlers);
|
||||
}, 0);
|
||||
};
|
||||
|
||||
|
||||
return SimpleEmitter;
|
||||
});
|
||||
|
|
|
@ -55,12 +55,12 @@ define(function (require) {
|
|||
expect(emitter.listenerCount('a')).to.be(2);
|
||||
});
|
||||
|
||||
it('only registers a specific listener once', function () {
|
||||
it('allows the same function to be registered multiple times', function () {
|
||||
var handler = function () {};
|
||||
emitter.on('a', handler);
|
||||
expect(emitter.listenerCount()).to.be(1);
|
||||
emitter.on('a', handler);
|
||||
expect(emitter.listenerCount()).to.be(1);
|
||||
expect(emitter.listenerCount()).to.be(2);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue