181 lines
4.1 KiB
JavaScript
181 lines
4.1 KiB
JavaScript
/**
|
|
* event-lite.js - Light-weight EventEmitter (less than 1KB when gzipped)
|
|
*
|
|
* @copyright Yusuke Kawasaki
|
|
* @license MIT
|
|
* @constructor
|
|
* @see https://github.com/kawanet/event-lite
|
|
* @see http://kawanet.github.io/event-lite/EventLite.html
|
|
* @example
|
|
* var EventLite = require("event-lite");
|
|
*
|
|
* function MyClass() {...} // your class
|
|
*
|
|
* EventLite.mixin(MyClass.prototype); // import event methods
|
|
*
|
|
* var obj = new MyClass();
|
|
* obj.on("foo", function() {...}); // add event listener
|
|
* obj.once("bar", function() {...}); // add one-time event listener
|
|
* obj.emit("foo"); // dispatch event
|
|
* obj.emit("bar"); // dispatch another event
|
|
* obj.off("foo"); // remove event listener
|
|
*/
|
|
|
|
function EventLite() {
|
|
if (!(this instanceof EventLite)) return new EventLite();
|
|
}
|
|
|
|
(function(EventLite) {
|
|
// export the class for node.js
|
|
if ("undefined" !== typeof module) module.exports = EventLite;
|
|
|
|
// property name to hold listeners
|
|
var LISTENERS = "listeners";
|
|
|
|
// methods to export
|
|
var methods = {
|
|
on: on,
|
|
once: once,
|
|
off: off,
|
|
emit: emit
|
|
};
|
|
|
|
// mixin to self
|
|
mixin(EventLite.prototype);
|
|
|
|
// export mixin function
|
|
EventLite.mixin = mixin;
|
|
|
|
/**
|
|
* Import on(), once(), off() and emit() methods into target object.
|
|
*
|
|
* @function EventLite.mixin
|
|
* @param target {Prototype}
|
|
*/
|
|
|
|
function mixin(target) {
|
|
for (var key in methods) {
|
|
target[key] = methods[key];
|
|
}
|
|
return target;
|
|
}
|
|
|
|
/**
|
|
* Add an event listener.
|
|
*
|
|
* @function EventLite.prototype.on
|
|
* @param type {string}
|
|
* @param func {Function}
|
|
* @returns {EventLite} Self for method chaining
|
|
*/
|
|
|
|
function on(type, func) {
|
|
getListeners(this, type).push(func);
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Add one-time event listener.
|
|
*
|
|
* @function EventLite.prototype.once
|
|
* @param type {string}
|
|
* @param func {Function}
|
|
* @returns {EventLite} Self for method chaining
|
|
*/
|
|
|
|
function once(type, func) {
|
|
var that = this;
|
|
wrap.originalListener = func;
|
|
getListeners(that, type).push(wrap);
|
|
return that;
|
|
|
|
function wrap() {
|
|
off.call(that, type, wrap);
|
|
func.apply(this, arguments);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove an event listener.
|
|
*
|
|
* @function EventLite.prototype.off
|
|
* @param [type] {string}
|
|
* @param [func] {Function}
|
|
* @returns {EventLite} Self for method chaining
|
|
*/
|
|
|
|
function off(type, func) {
|
|
var that = this;
|
|
var listners;
|
|
if (!arguments.length) {
|
|
delete that[LISTENERS];
|
|
} else if (!func) {
|
|
listners = that[LISTENERS];
|
|
if (listners) {
|
|
delete listners[type];
|
|
if (!Object.keys(listners).length) return off.call(that);
|
|
}
|
|
} else {
|
|
listners = getListeners(that, type, true);
|
|
if (listners) {
|
|
listners = listners.filter(ne);
|
|
if (!listners.length) return off.call(that, type);
|
|
that[LISTENERS][type] = listners;
|
|
}
|
|
}
|
|
return that;
|
|
|
|
function ne(test) {
|
|
return test !== func && test.originalListener !== func;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Dispatch (trigger) an event.
|
|
*
|
|
* @function EventLite.prototype.emit
|
|
* @param type {string}
|
|
* @param [value] {*}
|
|
* @returns {boolean} True when a listener received the event
|
|
*/
|
|
|
|
function emit(type, value) {
|
|
var that = this;
|
|
var listeners = getListeners(that, type, true);
|
|
if (!listeners) return false;
|
|
var arglen = arguments.length;
|
|
if (arglen === 1) {
|
|
listeners.forEach(zeroarg);
|
|
} else if (arglen === 2) {
|
|
listeners.forEach(onearg);
|
|
} else {
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
listeners.forEach(moreargs);
|
|
}
|
|
return !!listeners.length;
|
|
|
|
function zeroarg(func) {
|
|
func.call(that);
|
|
}
|
|
|
|
function onearg(func) {
|
|
func.call(that, value);
|
|
}
|
|
|
|
function moreargs(func) {
|
|
func.apply(that, args);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
|
|
function getListeners(that, type, readonly) {
|
|
if (readonly && !that[LISTENERS]) return;
|
|
var listeners = that[LISTENERS] || (that[LISTENERS] = {});
|
|
return listeners[type] || (listeners[type] = []);
|
|
}
|
|
|
|
})(EventLite);
|