Comparing version 3.1.3 to 3.2.0
950
emmett.js
@@ -1,556 +0,566 @@ | ||
(function() { | ||
'use strict'; | ||
/** | ||
* Here is the list of every allowed parameter when using Emitter#on: | ||
* @type {Object} | ||
*/ | ||
var __allowedOptions = { | ||
once: 'boolean', | ||
scope: 'object' | ||
}; | ||
/** | ||
* Here is the list of every allowed parameter when using Emitter#on: | ||
* @type {Object} | ||
*/ | ||
var __allowedOptions = { | ||
once: 'boolean', | ||
scope: 'object' | ||
}; | ||
/** | ||
* Incremental id used to order event handlers. | ||
*/ | ||
var __order = 0; | ||
/** | ||
* Incremental id used to order event handlers. | ||
*/ | ||
var __order = 0; | ||
/** | ||
* A simple helper to shallowly merge two objects. The second one will "win" | ||
* over the first one. | ||
* | ||
* @param {object} o1 First target object. | ||
* @param {object} o2 Second target object. | ||
* @return {object} Returns the merged object. | ||
*/ | ||
function shallowMerge(o1, o2) { | ||
var o = {}, | ||
k; | ||
/** | ||
* A simple helper to shallowly merge two objects. The second one will "win" | ||
* over the first one. | ||
* | ||
* @param {object} o1 First target object. | ||
* @param {object} o2 Second target object. | ||
* @return {object} Returns the merged object. | ||
*/ | ||
function shallowMerge(o1, o2) { | ||
var o = {}, | ||
k; | ||
for (k in o1) o[k] = o1[k]; | ||
for (k in o2) o[k] = o2[k]; | ||
for (k in o1) o[k] = o1[k]; | ||
for (k in o2) o[k] = o2[k]; | ||
return o; | ||
} | ||
return o; | ||
} | ||
/** | ||
* Is the given variable a plain JavaScript object? | ||
* | ||
* @param {mixed} v Target. | ||
* @return {boolean} The boolean result. | ||
*/ | ||
function isPlainObject(v) { | ||
return v && | ||
typeof v === 'object' && | ||
!Array.isArray(v) && | ||
!(v instanceof Function) && | ||
!(v instanceof RegExp); | ||
} | ||
/** | ||
* Is the given variable a plain JavaScript object? | ||
* | ||
* @param {mixed} v Target. | ||
* @return {boolean} The boolean result. | ||
*/ | ||
function isPlainObject(v) { | ||
return v && | ||
typeof v === 'object' && | ||
!Array.isArray(v) && | ||
!(v instanceof Function) && | ||
!(v instanceof RegExp); | ||
} | ||
/** | ||
* Iterate over an object that may have ES6 Symbols. | ||
* | ||
* @param {object} object Object on which to iterate. | ||
* @param {function} fn Iterator function. | ||
* @param {object} [scope] Optional scope. | ||
*/ | ||
function forIn(object, fn, scope) { | ||
var symbols, | ||
k, | ||
i, | ||
l; | ||
/** | ||
* Iterate over an object that may have ES6 Symbols. | ||
* | ||
* @param {object} object Object on which to iterate. | ||
* @param {function} fn Iterator function. | ||
* @param {object} [scope] Optional scope. | ||
*/ | ||
function forIn(object, fn, scope) { | ||
var symbols, | ||
k, | ||
i, | ||
l; | ||
for (k in object) | ||
fn.call(scope || null, k, object[k]); | ||
for (k in object) | ||
fn.call(scope || null, k, object[k]); | ||
if (Object.getOwnPropertySymbols) { | ||
symbols = Object.getOwnPropertySymbols(object); | ||
if (Object.getOwnPropertySymbols) { | ||
symbols = Object.getOwnPropertySymbols(object); | ||
for (i = 0, l = symbols.length; i < l; i++) | ||
fn.call(scope || null, symbols[i], object[symbols[i]]); | ||
} | ||
for (i = 0, l = symbols.length; i < l; i++) | ||
fn.call(scope || null, symbols[i], object[symbols[i]]); | ||
} | ||
} | ||
/** | ||
* The emitter's constructor. It initializes the handlers-per-events store and | ||
* the global handlers store. | ||
* | ||
* Emitters are useful for non-DOM events communication. Read its methods | ||
* documentation for more information about how it works. | ||
* | ||
* @return {Emitter} The fresh new instance. | ||
*/ | ||
var Emitter = function() { | ||
this._enabled = true; | ||
/** | ||
* The emitter's constructor. It initializes the handlers-per-events store and | ||
* the global handlers store. | ||
* | ||
* Emitters are useful for non-DOM events communication. Read its methods | ||
* documentation for more information about how it works. | ||
* | ||
* @return {Emitter} The fresh new instance. | ||
*/ | ||
var Emitter = function() { | ||
this._enabled = true; | ||
// Dirty trick that will set the necessary properties to the emitter | ||
this.unbindAll(); | ||
}; | ||
// Dirty trick that will set the necessary properties to the emitter | ||
this.unbindAll(); | ||
}; | ||
/** | ||
* This method unbinds every handlers attached to every or any events. So, | ||
* these functions will no more be executed when the related events are | ||
* emitted. If the functions were not bound to the events, nothing will | ||
* happen, and no error will be thrown. | ||
* | ||
* Usage: | ||
* ****** | ||
* > myEmitter.unbindAll(); | ||
* | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.unbindAll = function() { | ||
/** | ||
* This method unbinds every handlers attached to every or any events. So, these | ||
* functions will no more be executed when the related events are emitted. If | ||
* the functions were not bound to the events, nothing will happen, and no error | ||
* will be thrown. | ||
* | ||
* Usage: | ||
* ****** | ||
* > myEmitter.unbindAll(); | ||
* | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.unbindAll = function() { | ||
this._handlers = {}; | ||
this._handlersAll = []; | ||
this._handlersComplex = []; | ||
this._handlers = {}; | ||
this._handlersAll = []; | ||
this._handlersComplex = []; | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* This method binds one or more functions to the emitter, handled to one or a | ||
* suite of events. So, these functions will be executed anytime one related | ||
* event is emitted. | ||
* | ||
* It is also possible to bind a function to any emitted event by not | ||
* specifying any event to bind the function to. | ||
* | ||
* Recognized options: | ||
* ******************* | ||
* - {?boolean} once If true, the handlers will be unbound after the first | ||
* execution. Default value: false. | ||
* - {?object} scope If a scope is given, then the listeners will be called | ||
* with this scope as "this". | ||
* | ||
* Variant 1: | ||
* ********** | ||
* > myEmitter.on('myEvent', function(e) { console.log(e); }); | ||
* > // Or: | ||
* > myEmitter.on('myEvent', function(e) { console.log(e); }, { once: true }); | ||
* | ||
* @param {string} event The event to listen to. | ||
* @param {function} handler The function to bind. | ||
* @param {?object} options Eventually some options. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 2: | ||
* ********** | ||
* > myEmitter.on( | ||
* > ['myEvent1', 'myEvent2'], | ||
* > function(e) { console.log(e); } | ||
* >); | ||
* > // Or: | ||
* > myEmitter.on( | ||
* > ['myEvent1', 'myEvent2'], | ||
* > function(e) { console.log(e); } | ||
* > { once: true }} | ||
* >); | ||
* | ||
* @param {array} events The events to listen to. | ||
* @param {function} handler The function to bind. | ||
* @param {?object} options Eventually some options. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 3: | ||
* ********** | ||
* > myEmitter.on({ | ||
* > myEvent1: function(e) { console.log(e); }, | ||
* > myEvent2: function(e) { console.log(e); } | ||
* > }); | ||
* > // Or: | ||
* > myEmitter.on({ | ||
* > myEvent1: function(e) { console.log(e); }, | ||
* > myEvent2: function(e) { console.log(e); } | ||
* > }, { once: true }); | ||
* | ||
* @param {object} bindings An object containing pairs event / function. | ||
* @param {?object} options Eventually some options. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 4: | ||
* ********** | ||
* > myEmitter.on(function(e) { console.log(e); }); | ||
* > // Or: | ||
* > myEmitter.on(function(e) { console.log(e); }, { once: true}); | ||
* | ||
* @param {function} handler The function to bind to every events. | ||
* @param {?object} options Eventually some options. | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.on = function(a, b, c) { | ||
var i, | ||
l, | ||
k, | ||
event, | ||
eArray, | ||
handlersList, | ||
bindingObject; | ||
/** | ||
* This method binds one or more functions to the emitter, handled to one or a | ||
* suite of events. So, these functions will be executed anytime one related | ||
* event is emitted. | ||
* | ||
* It is also possible to bind a function to any emitted event by not specifying | ||
* any event to bind the function to. | ||
* | ||
* Recognized options: | ||
* ******************* | ||
* - {?boolean} once If true, the handlers will be unbound after the first | ||
* execution. Default value: false. | ||
* - {?object} scope If a scope is given, then the listeners will be called | ||
* with this scope as "this". | ||
* | ||
* Variant 1: | ||
* ********** | ||
* > myEmitter.on('myEvent', function(e) { console.log(e); }); | ||
* > // Or: | ||
* > myEmitter.on('myEvent', function(e) { console.log(e); }, { once: true }); | ||
* | ||
* @param {EventName} event The event to listen to. | ||
* @param {Handler} handler The function to bind. | ||
* @param {?object} options Some options. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 2: | ||
* ********** | ||
* > myEmitter.on( | ||
* > ['myEvent1', 'myEvent2'], | ||
* > function(e) { console.log(e); } | ||
* > ); | ||
* > // Or: | ||
* > myEmitter.on( | ||
* > ['myEvent1', 'myEvent2'], | ||
* > function(e) { console.log(e); } | ||
* > { once: true }} | ||
* > ); | ||
* | ||
* @param {EventName[]} events The events to listen to. | ||
* @param {Handler} handler The function to bind. | ||
* @param {?object} options Some options. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 3: | ||
* ********** | ||
* > myEmitter.on({ | ||
* > myEvent1: function(e) { console.log(e); }, | ||
* > myEvent2: function(e) { console.log(e); } | ||
* > }); | ||
* > // Or: | ||
* > myEmitter.on({ | ||
* > myEvent1: function(e) { console.log(e); }, | ||
* > myEvent2: function(e) { console.log(e); } | ||
* > }, { once: true }); | ||
* | ||
* @param {Object<EventName, Handler>} bindings An object containing | ||
* pairs event / function. | ||
* @param {?object} options Some options. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 4: | ||
* ********** | ||
* > myEmitter.on(function(e) { console.log(e); }); | ||
* > // Or: | ||
* > myEmitter.on(function(e) { console.log(e); }, { once: true }); | ||
* | ||
* @param {Handler} handler The function to bind to every events. | ||
* @param {?object} options Some options. | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.on = function(a, b, c) { | ||
var i, | ||
l, | ||
k, | ||
event, | ||
eArray, | ||
handlersList, | ||
bindingObject; | ||
// Variant 3 | ||
if (isPlainObject(a)) { | ||
forIn(a, function(name, fn) { | ||
this.on(name, fn, b); | ||
}, this); | ||
// Variant 3 | ||
if (isPlainObject(a)) { | ||
forIn(a, function(name, fn) { | ||
this.on(name, fn, b); | ||
}, this); | ||
return this; | ||
} | ||
return this; | ||
} | ||
// Variant 1, 2 and 4 | ||
if (typeof a === 'function') { | ||
c = b; | ||
b = a; | ||
a = null; | ||
} | ||
// Variant 4 | ||
if (typeof a === 'function') { | ||
c = b; | ||
b = a; | ||
a = null; | ||
} | ||
eArray = [].concat(a); | ||
eArray = [].concat(a); | ||
for (i = 0, l = eArray.length; i < l; i++) { | ||
event = eArray[i]; | ||
for (i = 0, l = eArray.length; i < l; i++) { | ||
event = eArray[i]; | ||
bindingObject = { | ||
order: __order++, | ||
fn: b | ||
}; | ||
bindingObject = { | ||
order: __order++, | ||
fn: b | ||
}; | ||
// Defining the list in which the handler should be inserted | ||
if (typeof event === 'string' || typeof event === 'symbol') { | ||
if (!this._handlers[event]) | ||
this._handlers[event] = []; | ||
handlersList = this._handlers[event]; | ||
bindingObject.type = event; | ||
} | ||
else if (event instanceof RegExp) { | ||
handlersList = this._handlersComplex; | ||
bindingObject.pattern = event; | ||
} | ||
else if (event === null) { | ||
handlersList = this._handlersAll; | ||
} | ||
else { | ||
throw Error('Emitter.on: invalid event.'); | ||
} | ||
// Defining the list in which the handler should be inserted | ||
if (typeof event === 'string' || typeof event === 'symbol') { | ||
if (!this._handlers[event]) | ||
this._handlers[event] = []; | ||
handlersList = this._handlers[event]; | ||
bindingObject.type = event; | ||
} | ||
else if (event instanceof RegExp) { | ||
handlersList = this._handlersComplex; | ||
bindingObject.pattern = event; | ||
} | ||
else if (event === null) { | ||
handlersList = this._handlersAll; | ||
} | ||
else { | ||
throw Error('Emitter.on: invalid event.'); | ||
} | ||
// Appending needed properties | ||
for (k in c || {}) | ||
if (__allowedOptions[k]) | ||
bindingObject[k] = c[k]; | ||
// Appending needed properties | ||
for (k in c || {}) | ||
if (__allowedOptions[k]) | ||
bindingObject[k] = c[k]; | ||
handlersList.push(bindingObject); | ||
} | ||
handlersList.push(bindingObject); | ||
} | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* This method works exactly as the previous #on, but will add an options | ||
* object if none is given, and set the option "once" to true. | ||
* | ||
* The polymorphism works exactly as with the #on method. | ||
*/ | ||
Emitter.prototype.once = function() { | ||
var args = Array.prototype.slice.call(arguments), | ||
li = args.length - 1; | ||
/** | ||
* This method works exactly as the previous #on, but will add an options object | ||
* if none is given, and set the option "once" to true. | ||
* | ||
* The polymorphism works exactly as with the #on method. | ||
*/ | ||
Emitter.prototype.once = function() { | ||
var args = Array.prototype.slice.call(arguments), | ||
li = args.length - 1; | ||
if (isPlainObject(args[li]) && args.length > 1) | ||
args[li] = shallowMerge(args[li], {once: true}); | ||
else | ||
args.push({once: true}); | ||
if (isPlainObject(args[li]) && args.length > 1) | ||
args[li] = shallowMerge(args[li], {once: true}); | ||
else | ||
args.push({once: true}); | ||
return this.on.apply(this, args); | ||
}; | ||
return this.on.apply(this, args); | ||
}; | ||
/** | ||
* This method unbinds one or more functions from events of the emitter. So, | ||
* these functions will no more be executed when the related events are | ||
* emitted. If the functions were not bound to the events, nothing will | ||
* happen, and no error will be thrown. | ||
* | ||
* Variant 1: | ||
* ********** | ||
* > myEmitter.off('myEvent', myHandler); | ||
* | ||
* @param {string} event The event to unbind the handler from. | ||
* @param {function} handler The function to unbind. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 2: | ||
* ********** | ||
* > myEmitter.off(['myEvent1', 'myEvent2'], myHandler); | ||
* | ||
* @param {array} events The events to unbind the handler from. | ||
* @param {function} handler The function to unbind. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 3: | ||
* ********** | ||
* > myEmitter.off({ | ||
* > myEvent1: myHandler1, | ||
* > myEvent2: myHandler2 | ||
* > }); | ||
* | ||
* @param {object} bindings An object containing pairs event / function. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 4: | ||
* ********** | ||
* > myEmitter.off(myHandler); | ||
* | ||
* @param {function} handler The function to unbind from every events. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 5: | ||
* ********** | ||
* > myEmitter.off(event); | ||
* | ||
* @param {string} event The event we should unbind. | ||
* @return {Emitter} Returns this. | ||
*/ | ||
function filter(target, fn) { | ||
target = target || []; | ||
/** | ||
* This method unbinds one or more functions from events of the emitter. So, | ||
* these functions will no more be executed when the related events are emitter. | ||
* If the functions were not bound to the events, nothing will happen, and no | ||
* error will be thrown. | ||
* | ||
* Variant 1: | ||
* ********** | ||
* > myEmitter.off('myEvent', myHandler); | ||
* | ||
* @param {EventName} event The event to unbind the handler from. | ||
* @param {Handler} handler The function to unbind. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 2: | ||
* ********** | ||
* > myEmitter.off(['myEvent1', 'myEvent2'], myHandler); | ||
* | ||
* @param {EventName[]} events The events to unbind the handler from. | ||
* @param {Handler} handler The function to unbind. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 3: | ||
* ********** | ||
* > myEmitter.off({ | ||
* > myEvent1: myHandler1, | ||
* > myEvent2: myHandler2 | ||
* > }); | ||
* | ||
* @param {Object<EventName, Handler>} bindings An object containing pairs | ||
* event / function. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 4: | ||
* ********** | ||
* > myEmitter.off(myHandler); | ||
* | ||
* @param {Handler} handler The function to unbind from every events. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 5: | ||
* ********** | ||
* > myEmitter.off(event); | ||
* | ||
* @param {EventName} event The event we should unbind. | ||
* @return {Emitter} Returns this. | ||
*/ | ||
function filter(target, fn) { | ||
target = target || []; | ||
var a = [], | ||
l, | ||
i; | ||
var a = [], | ||
l, | ||
i; | ||
for (i = 0, l = target.length; i < l; i++) | ||
if (target[i].fn !== fn) | ||
a.push(target[i]); | ||
for (i = 0, l = target.length; i < l; i++) | ||
if (target[i].fn !== fn) | ||
a.push(target[i]); | ||
return a; | ||
} | ||
return a; | ||
} | ||
Emitter.prototype.off = function(events, fn) { | ||
var i, | ||
n, | ||
k, | ||
event; | ||
Emitter.prototype.off = function(events, fn) { | ||
var i, | ||
n, | ||
k, | ||
event; | ||
// Variant 4: | ||
if (arguments.length === 1 && typeof events === 'function') { | ||
fn = arguments[0]; | ||
// Variant 4: | ||
if (arguments.length === 1 && typeof events === 'function') { | ||
fn = arguments[0]; | ||
// Handlers bound to events: | ||
for (k in this._handlers) { | ||
this._handlers[k] = filter(this._handlers[k], fn); | ||
var keys = Object.keys(this._handlers) | ||
.concat(Object.getOwnPropertySymbols(this._handlers)); | ||
if (this._handlers[k].length === 0) | ||
delete this._handlers[k]; | ||
} | ||
// Handlers bound to events: | ||
for (i = 0; i < keys.length; i++) { | ||
k = keys[i]; | ||
// Generic Handlers | ||
this._handlersAll = filter(this._handlersAll, fn); | ||
this._handlers[k] = filter(this._handlers[k], fn); | ||
// Complex handlers | ||
this._handlersComplex = filter(this._handlersComplex, fn); | ||
if (this._handlers[k].length === 0) | ||
delete this._handlers[k]; | ||
} | ||
// Variant 5 | ||
else if (arguments.length === 1 && | ||
(typeof events === 'string' || typeof events === 'symbol')) { | ||
delete this._handlers[events]; | ||
} | ||
// Generic Handlers | ||
this._handlersAll = filter(this._handlersAll, fn); | ||
// Variant 1 and 2: | ||
else if (arguments.length === 2) { | ||
var eArray = [].concat(events); | ||
// Complex handlers | ||
this._handlersComplex = filter(this._handlersComplex, fn); | ||
} | ||
for (i = 0, n = eArray.length; i < n; i++) { | ||
event = eArray[i]; | ||
// Variant 5 | ||
else if (arguments.length === 1 && | ||
(typeof events === 'string' || typeof events === 'symbol')) { | ||
delete this._handlers[events]; | ||
} | ||
this._handlers[event] = filter(this._handlers[event], fn); | ||
// Variant 1 and 2: | ||
else if (arguments.length === 2) { | ||
var eArray = [].concat(events); | ||
if ((this._handlers[event] || []).length === 0) | ||
delete this._handlers[event]; | ||
} | ||
} | ||
for (i = 0, n = eArray.length; i < n; i++) { | ||
event = eArray[i]; | ||
// Variant 3 | ||
else if (isPlainObject(events)) { | ||
forIn(events, this.off, this); | ||
this._handlers[event] = filter(this._handlers[event], fn); | ||
if ((this._handlers[event] || []).length === 0) | ||
delete this._handlers[event]; | ||
} | ||
} | ||
return this; | ||
}; | ||
// Variant 3 | ||
else if (isPlainObject(events)) { | ||
forIn(events, this.off, this); | ||
} | ||
/** | ||
* This method retrieve the listeners attached to a particular event. | ||
* | ||
* @param {?string} Name of the event. | ||
* @return {array} Array of handler functions. | ||
*/ | ||
Emitter.prototype.listeners = function(event) { | ||
var handlers = this._handlersAll || [], | ||
complex = false, | ||
h, | ||
i, | ||
l; | ||
return this; | ||
}; | ||
if (!event) | ||
throw Error('Emitter.listeners: no event provided.'); | ||
/** | ||
* This method retrieve the listeners attached to a particular event. | ||
* | ||
* @param {?EventName} event Name of the event. | ||
* @return {array} Array of handler functions. | ||
*/ | ||
Emitter.prototype.listeners = function(event) { | ||
var handlers = this._handlersAll || [], | ||
complex = false, | ||
h, | ||
i, | ||
l; | ||
handlers = handlers.concat(this._handlers[event] || []); | ||
if (!event) | ||
throw Error('Emitter.listeners: no event provided.'); | ||
for (i = 0, l = this._handlersComplex.length; i < l; i++) { | ||
h = this._handlersComplex[i]; | ||
handlers = handlers.concat(this._handlers[event] || []); | ||
if (~event.search(h.pattern)) { | ||
complex = true; | ||
handlers.push(h); | ||
} | ||
for (i = 0, l = this._handlersComplex.length; i < l; i++) { | ||
h = this._handlersComplex[i]; | ||
if (typeof event === 'string' && ~event.search(h.pattern)) { | ||
complex = true; | ||
handlers.push(h); | ||
} | ||
} | ||
// If we have any complex handlers, we need to sort | ||
if (this._handlersAll.length || complex) | ||
return handlers.sort(function(a, b) { | ||
return a.order - b.order; | ||
}); | ||
else | ||
return handlers.slice(0); | ||
}; | ||
// If we have any complex handlers, we need to sort | ||
if (this._handlersAll.length || complex) | ||
return handlers.sort(function(a, b) { | ||
return a.order - b.order; | ||
}); | ||
else | ||
return handlers.slice(0); | ||
}; | ||
/** | ||
* This method emits the specified event(s), and executes every handlers bound | ||
* to the event(s). | ||
* | ||
* Use cases: | ||
* ********** | ||
* > myEmitter.emit('myEvent'); | ||
* > myEmitter.emit('myEvent', myData); | ||
* > myEmitter.emit(['myEvent1', 'myEvent2']); | ||
* > myEmitter.emit(['myEvent1', 'myEvent2'], myData); | ||
* > myEmitter.emit({myEvent1: myData1, myEvent2: myData2}); | ||
* | ||
* @param {string|array} events The event(s) to emit. | ||
* @param {object?} data The data. | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.emit = function(events, data) { | ||
/** | ||
* This method emits the specified event(s), and executes every handlers bound | ||
* to the event(s). | ||
* | ||
* Variant 1: | ||
* ********** | ||
* > myEmitter.emit('myEvent'); | ||
* > myEmitter.emit('myEvent', myData); | ||
* | ||
* @param {EventName} event The event to emit. | ||
* @param {object?} data Some data. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 2: | ||
* ********** | ||
* > myEmitter.emit(['myEvent1', 'myEvent2']); | ||
* > myEmitter.emit(['myEvent1', 'myEvent2'], myData); | ||
* | ||
* @param {EventName[]} events The events to emit. | ||
* @param {object?} data Some data. | ||
* @return {Emitter} Returns this. | ||
* | ||
* Variant 3: | ||
* ********** | ||
* > myEmitter.emit({myEvent1: myData1, myEvent2: myData2}); | ||
* | ||
* @param {Object<EventName, any>} events The events to emit. | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.emit = function(events, data) { | ||
// Short exit if the emitter is disabled | ||
if (!this._enabled) | ||
return this; | ||
// Short exit if the emitter is disabled | ||
if (!this._enabled) | ||
return this; | ||
// Object variant | ||
if (isPlainObject(events)) { | ||
forIn(events, this.emit, this); | ||
return this; | ||
} | ||
// Object variant | ||
if (isPlainObject(events)) { | ||
forIn(events, this.emit, this); | ||
return this; | ||
} | ||
var eArray = [].concat(events), | ||
onces = [], | ||
event, | ||
parent, | ||
handlers, | ||
handler, | ||
i, | ||
j, | ||
l, | ||
m; | ||
var eArray = [].concat(events), | ||
onces = [], | ||
event, | ||
parent, | ||
handlers, | ||
handler, | ||
i, | ||
j, | ||
l, | ||
m; | ||
for (i = 0, l = eArray.length; i < l; i++) { | ||
handlers = this.listeners(eArray[i]); | ||
for (i = 0, l = eArray.length; i < l; i++) { | ||
handlers = this.listeners(eArray[i]); | ||
for (j = 0, m = handlers.length; j < m; j++) { | ||
handler = handlers[j]; | ||
event = { | ||
type: eArray[i], | ||
target: this | ||
}; | ||
for (j = 0, m = handlers.length; j < m; j++) { | ||
handler = handlers[j]; | ||
event = { | ||
type: eArray[i], | ||
target: this | ||
}; | ||
if (arguments.length > 1) | ||
event.data = data; | ||
if (arguments.length > 1) | ||
event.data = data; | ||
handler.fn.call('scope' in handler ? handler.scope : this, event); | ||
handler.fn.call('scope' in handler ? handler.scope : this, event); | ||
if (handler.once) | ||
onces.push(handler); | ||
} | ||
if (handler.once) | ||
onces.push(handler); | ||
} | ||
// Cleaning onces | ||
for (j = onces.length - 1; j >= 0; j--) { | ||
parent = onces[j].type ? | ||
this._handlers[onces[j].type] : | ||
onces[j].pattern ? | ||
this._handlersComplex : | ||
this._handlersAll; | ||
// Cleaning onces | ||
for (j = onces.length - 1; j >= 0; j--) { | ||
if (onces[j].type) | ||
parent = this._handlers[onces[j].type]; | ||
else if (onces[j].pattern) | ||
parent = this._handlersComplex; | ||
else | ||
parent = this._handlersAll; | ||
var onceIndex = parent.indexOf(onces[j]); | ||
if (onceIndex !== -1) { | ||
parent.splice(onceIndex, 1); | ||
} | ||
var onceIndex = parent.indexOf(onces[j]); | ||
if (onceIndex !== -1) { | ||
parent.splice(onceIndex, 1); | ||
} | ||
} | ||
} | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* This method will unbind all listeners and make it impossible to ever | ||
* rebind any listener to any event. | ||
*/ | ||
Emitter.prototype.kill = function() { | ||
/** | ||
* This method will unbind all listeners and make it impossible to ever rebind | ||
* any listener to any event. | ||
*/ | ||
Emitter.prototype.kill = function() { | ||
this.unbindAll(); | ||
this._handlers = null; | ||
this._handlersAll = null; | ||
this._handlersComplex = null; | ||
this._enabled = false; | ||
this.unbindAll(); | ||
this._handlers = null; | ||
this._handlersAll = null; | ||
this._handlersComplex = null; | ||
this._enabled = false; | ||
// Nooping methods | ||
this.unbindAll = | ||
this.on = | ||
this.once = | ||
this.off = | ||
this.emit = | ||
this.listeners = Function.prototype; | ||
}; | ||
// Nooping methods | ||
this.unbindAll = | ||
this.on = | ||
this.once = | ||
this.off = | ||
this.emit = | ||
this.listeners = Function.prototype; | ||
}; | ||
/** | ||
* This method disabled the emitter, which means its emit method will do | ||
* nothing. | ||
* | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.disable = function() { | ||
this._enabled = false; | ||
/** | ||
* This method disabled the emitter, which means its emit method will do | ||
* nothing. | ||
* | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.disable = function() { | ||
this._enabled = false; | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* This method enables the emitter. | ||
* | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.enable = function() { | ||
this._enabled = true; | ||
/** | ||
* This method enables the emitter. | ||
* | ||
* @return {Emitter} Returns this. | ||
*/ | ||
Emitter.prototype.enable = function() { | ||
this._enabled = true; | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* Version: | ||
*/ | ||
Emitter.version = '3.1.3'; | ||
/** | ||
* Version: | ||
*/ | ||
Emitter.version = '3.2.0'; | ||
// Export: | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) | ||
exports = module.exports = Emitter; | ||
exports.Emitter = Emitter; | ||
} else if (typeof define === 'function' && define.amd) | ||
define('emmett', [], function() { | ||
return Emitter; | ||
}); | ||
else | ||
this.Emitter = Emitter; | ||
}).call(this); | ||
/** | ||
* Export: | ||
*/ | ||
module.exports = Emitter; |
The MIT License (MIT) | ||
Copyright (c) 2014-2015 Alexis Jacomy - @jacomyal | ||
Copyright (c) 2014-2020 Alexis Jacomy - @jacomyal | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "emmett", | ||
"version": "3.1.3", | ||
"version": "3.2.0", | ||
"description": "A custom event emitter for Node.js and the browser.", | ||
"main": "emmett.js", | ||
"scripts": { | ||
"test": "gulp test", | ||
"build": "gulp build" | ||
"lint": "eslint ./*.js ./test", | ||
"prepublish": "npm run lint && npm test && npm run test:types", | ||
"test": "mocha", | ||
"test:types": "tsc --target es2015 --noEmit --noImplicitAny --noImplicitReturns ./test/types.ts" | ||
}, | ||
"files": [ | ||
"emmett.js", | ||
"emmett.d.ts" | ||
], | ||
"repository": { | ||
@@ -34,13 +40,17 @@ "type": "git", | ||
"devDependencies": { | ||
"gulp": "^3.8.9", | ||
"gulp-browserify": "^0.5.0", | ||
"gulp-gjslint": "^0.1.4", | ||
"gulp-header": "^1.2.2", | ||
"gulp-jshint": "^1.8.6", | ||
"gulp-mocha": "^1.1.1", | ||
"gulp-mocha-phantomjs": "^0.5.1", | ||
"gulp-rename": "^1.2.0", | ||
"gulp-uglify": "^1.0.1", | ||
"jshint-stylish": "^1.0.0" | ||
"@yomguithereal/eslint-config": "^4.0.0", | ||
"eslint": "^6.8.0", | ||
"mocha": "^7.0.1", | ||
"typescript": "^3.7.5" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@yomguithereal/eslint-config", | ||
"globals": { | ||
"define": true, | ||
"Symbol": true | ||
}, | ||
"rules": { | ||
"no-new": 0 | ||
} | ||
} | ||
} |
@@ -15,4 +15,2 @@ # Emmett | ||
Or you can just drop the [`emmett.min.js`](./emmett.min.js) file in your project (will work with many popular module systems). | ||
## Usage | ||
@@ -184,7 +182,4 @@ | ||
# Build a minified version | ||
npm build | ||
# Lint the code | ||
gulp lint | ||
npm run lint | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4
529
20370
184