ev-emitter
Advanced tools
Comparing version 1.1.1 to 2.0.0
/** | ||
* EvEmitter v1.1.0 | ||
* EvEmitter v2.0.0 | ||
* Lil' event emitter | ||
@@ -7,11 +7,5 @@ * MIT License | ||
/* jshint unused: true, undef: true, strict: true */ | ||
( function( global, factory ) { | ||
// universal module definition | ||
/* jshint strict: false */ /* globals define, module, window */ | ||
if ( typeof define == 'function' && define.amd ) { | ||
// AMD - RequireJS | ||
define( factory ); | ||
} else if ( typeof module == 'object' && module.exports ) { | ||
if ( typeof module == 'object' && module.exports ) { | ||
// CommonJS - Browserify, Webpack | ||
@@ -26,89 +20,83 @@ module.exports = factory(); | ||
"use strict"; | ||
class EvEmitter { | ||
function EvEmitter() {} | ||
on( eventName, listener ) { | ||
if ( !eventName || !listener ) return this; | ||
var proto = EvEmitter.prototype; | ||
// set events hash | ||
let events = this._events = this._events || {}; | ||
// set listeners array | ||
let listeners = events[ eventName ] = events[ eventName ] || []; | ||
// only add once | ||
if ( !listeners.includes( listener ) ) { | ||
listeners.push( listener ); | ||
} | ||
proto.on = function( eventName, listener ) { | ||
if ( !eventName || !listener ) { | ||
return; | ||
return this; | ||
} | ||
// set events hash | ||
var events = this._events = this._events || {}; | ||
// set listeners array | ||
var listeners = events[ eventName ] = events[ eventName ] || []; | ||
// only add once | ||
if ( listeners.indexOf( listener ) == -1 ) { | ||
listeners.push( listener ); | ||
} | ||
return this; | ||
}; | ||
once( eventName, listener ) { | ||
if ( !eventName || !listener ) return this; | ||
proto.once = function( eventName, listener ) { | ||
if ( !eventName || !listener ) { | ||
return; | ||
// add event | ||
this.on( eventName, listener ); | ||
// set once flag | ||
// set onceEvents hash | ||
let onceEvents = this._onceEvents = this._onceEvents || {}; | ||
// set onceListeners object | ||
let onceListeners = onceEvents[ eventName ] = onceEvents[ eventName ] || {}; | ||
// set flag | ||
onceListeners[ listener ] = true; | ||
return this; | ||
} | ||
// add event | ||
this.on( eventName, listener ); | ||
// set once flag | ||
// set onceEvents hash | ||
var onceEvents = this._onceEvents = this._onceEvents || {}; | ||
// set onceListeners object | ||
var onceListeners = onceEvents[ eventName ] = onceEvents[ eventName ] || {}; | ||
// set flag | ||
onceListeners[ listener ] = true; | ||
return this; | ||
}; | ||
off( eventName, listener ) { | ||
let listeners = this._events && this._events[ eventName ]; | ||
if ( !listeners || !listeners.length ) return this; | ||
proto.off = function( eventName, listener ) { | ||
var listeners = this._events && this._events[ eventName ]; | ||
if ( !listeners || !listeners.length ) { | ||
return; | ||
let index = listeners.indexOf( listener ); | ||
if ( index != -1 ) { | ||
listeners.splice( index, 1 ); | ||
} | ||
return this; | ||
} | ||
var index = listeners.indexOf( listener ); | ||
if ( index != -1 ) { | ||
listeners.splice( index, 1 ); | ||
} | ||
return this; | ||
}; | ||
emitEvent( eventName, args ) { | ||
let listeners = this._events && this._events[ eventName ]; | ||
if ( !listeners || !listeners.length ) return this; | ||
proto.emitEvent = function( eventName, args ) { | ||
var listeners = this._events && this._events[ eventName ]; | ||
if ( !listeners || !listeners.length ) { | ||
return; | ||
} | ||
// copy over to avoid interference if .off() in listener | ||
listeners = listeners.slice(0); | ||
args = args || []; | ||
// once stuff | ||
var onceListeners = this._onceEvents && this._onceEvents[ eventName ]; | ||
// copy over to avoid interference if .off() in listener | ||
listeners = listeners.slice( 0 ); | ||
args = args || []; | ||
// once stuff | ||
let onceListeners = this._onceEvents && this._onceEvents[ eventName ]; | ||
for ( var i=0; i < listeners.length; i++ ) { | ||
var listener = listeners[i] | ||
var isOnce = onceListeners && onceListeners[ listener ]; | ||
if ( isOnce ) { | ||
// remove listener | ||
// remove before trigger to prevent recursion | ||
this.off( eventName, listener ); | ||
// unset once flag | ||
delete onceListeners[ listener ]; | ||
for ( let listener of listeners ) { | ||
let isOnce = onceListeners && onceListeners[ listener ]; | ||
if ( isOnce ) { | ||
// remove listener | ||
// remove before trigger to prevent recursion | ||
this.off( eventName, listener ); | ||
// unset once flag | ||
delete onceListeners[ listener ]; | ||
} | ||
// trigger listener | ||
listener.apply( this, args ); | ||
} | ||
// trigger listener | ||
listener.apply( this, args ); | ||
return this; | ||
} | ||
return this; | ||
}; | ||
allOff() { | ||
delete this._events; | ||
delete this._onceEvents; | ||
return this; | ||
} | ||
proto.allOff = function() { | ||
delete this._events; | ||
delete this._onceEvents; | ||
}; | ||
} | ||
return EvEmitter; | ||
})); | ||
} ) ); |
{ | ||
"name": "ev-emitter", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "lil' event emitter", | ||
"main": "ev-emitter.js", | ||
"devDependencies": { | ||
"ava": "^3.14.0", | ||
"eslint": "^7.15.0", | ||
"eslint-plugin-metafizzy": "^1.1.1" | ||
}, | ||
"scripts": { | ||
"test": "mocha test/test" | ||
"test": "npm run lint && ava", | ||
"lint": "npx eslint ." | ||
}, | ||
"eslintConfig": { | ||
"plugins": [ | ||
"metafizzy" | ||
], | ||
"extends": "plugin:metafizzy/base", | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
}, | ||
"env": { | ||
"browser": true, | ||
"commonjs": true | ||
} | ||
}, | ||
"repository": { | ||
@@ -10,0 +29,0 @@ "type": "git", |
@@ -5,3 +5,3 @@ # EvEmitter | ||
EvEmitter adds publish/subscribe pattern to a browser class. It's a smaller version of [Olical/EventEmitter](https://github.com/Olical/EventEmitter). That EventEmitter is full featured, widely used, and great. This EvEmitter has just the base event functionality to power the event API in libraries like [Isotope](http://isotope.metafizzy.co), [Flickity](http://flickity.metafizzy.co), [Masonry](http://masonry.desandro.com), and [imagesLoaded](http://imagesloaded.desandro.com). | ||
EvEmitter adds publish/subscribe pattern to a browser class. It's a smaller version of [Olical/EventEmitter](https://github.com/Olical/EventEmitter). That EventEmitter is full featured, widely used, and great. This EvEmitter has just the base event functionality to power the event API in libraries like [Isotope](https://isotope.metafizzy.co), [Flickity](https://flickity.metafizzy.co), [Masonry](https://masonry.desandro.com), and [imagesLoaded](https://imagesloaded.desandro.com). | ||
@@ -11,13 +11,10 @@ ## API | ||
``` js | ||
// Inherit prototype, IE8+ | ||
MyClass.prototype = new EvEmitter(); | ||
// class inheritence | ||
class MyClass extends EvEmitter {} | ||
// Inherit prototype, IE9+ | ||
MyClass.prototype = Object.create( EvEmitter.prototype ); | ||
// mixin prototype | ||
Object.assign( MyClass.prototype, EvEmitter.prototype ); | ||
// Mixin prototype | ||
_.extend( MyClass.prototype, EvEmitter.prototype ); | ||
// single instance | ||
var emitter = new EvEmitter(); | ||
let emitter = new EvEmitter(); | ||
``` | ||
@@ -75,3 +72,3 @@ | ||
// create event emitter | ||
var emitter = new EventEmitter(); | ||
var emitter = new EvEmitter(); | ||
@@ -93,5 +90,5 @@ // listeners | ||
emitter.on( 'rock', hey ) | ||
emitter.once( 'rock', ho ) | ||
emitter.on( 'rock', ho ) | ||
// trigger letsGo once | ||
emitter.on( 'rock', letsGo ) | ||
emitter.once( 'rock', letsGo ) | ||
@@ -111,4 +108,10 @@ // emit event | ||
## Browser support | ||
EvEmitter v2 uses ES6 features like [for...of loops](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) and [class definition](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) as such it supports Chrome 49+, Firefox 45+, Safari 9+, and Edge 13+. | ||
For older browser support, use [EvEmitter v1](https://github.com/metafizzy/ev-emitter/releases/tag/v1.1.1). | ||
## License | ||
EvEmitter is released under the [MIT License](http://desandro.mit-license.org/). Have at it. |
125
test/test.js
@@ -1,20 +0,17 @@ | ||
/* jshint node: true, unused: true, undef: true */ | ||
/* globals it */ | ||
const test = require('ava'); | ||
const EvEmitter = require('../ev-emitter.js'); | ||
var assert = require('assert'); | ||
var EvEmitter = require('../ev-emitter'); | ||
it( 'should emitEvent', function() { | ||
var emitter = new EvEmitter(); | ||
var didPop; | ||
test( 'should emitEvent', function( t ) { | ||
let emitter = new EvEmitter(); | ||
let didPop; | ||
emitter.on( 'pop', function() { | ||
didPop = true; | ||
}); | ||
emitter.emitEvent( 'pop' ); | ||
assert.ok( didPop, 'event emitted' ); | ||
}); | ||
} ); | ||
emitter.emitEvent('pop'); | ||
t.truthy( didPop, 'event emitted' ); | ||
} ); | ||
it( 'emitEvent should pass argument to listener', function() { | ||
var emitter = new EvEmitter(); | ||
var result; | ||
test( 'emitEvent should pass argument to listener', function( t ) { | ||
let emitter = new EvEmitter(); | ||
let result; | ||
function onPop( arg ) { | ||
@@ -25,8 +22,8 @@ result = arg; | ||
emitter.emitEvent( 'pop', [ 1 ] ); | ||
assert.equal( result, 1, 'event emitted, arg passed' ); | ||
}); | ||
t.is( result, 1, 'event emitted, arg passed' ); | ||
} ); | ||
it( 'does not allow same listener to be added', function() { | ||
var emitter = new EvEmitter(); | ||
var ticks = 0; | ||
test( 'does not allow same listener to be added', function( t ) { | ||
let emitter = new EvEmitter(); | ||
let ticks = 0; | ||
function onPop() { | ||
@@ -37,12 +34,12 @@ ticks++; | ||
emitter.on( 'pop', onPop ); | ||
var _onPop = onPop; | ||
let _onPop = onPop; | ||
emitter.on( 'pop', _onPop ); | ||
emitter.emitEvent('pop'); | ||
assert.equal( ticks, 1, '1 tick for same listener' ); | ||
}); | ||
t.is( ticks, 1, '1 tick for same listener' ); | ||
} ); | ||
it( 'should remove listener with .off()', function() { | ||
var emitter = new EvEmitter(); | ||
var ticks = 0; | ||
test( 'should remove listener with .off()', function( t ) { | ||
let emitter = new EvEmitter(); | ||
let ticks = 0; | ||
function onPop() { | ||
@@ -55,6 +52,6 @@ ticks++; | ||
emitter.emitEvent('pop'); | ||
assert.equal( ticks, 1, '.off() removed listener' ); | ||
t.is( ticks, 1, '.off() removed listener' ); | ||
// reset | ||
var ary = []; | ||
let ary = []; | ||
ticks = 0; | ||
@@ -80,23 +77,23 @@ emitter.allOff(); | ||
assert.equal( ary.join(','), 'a,b,a,b,b', '.off in listener does not interfer' ); | ||
t.is( ary.join(','), 'a,b,a,b,b', '.off in listener does not interfer' ); | ||
}); | ||
} ); | ||
it( 'should handle once()', function() { | ||
var emitter = new EvEmitter(); | ||
var ary = []; | ||
test( 'should handle once()', function( t ) { | ||
let emitter = new EvEmitter(); | ||
let ary = []; | ||
emitter.on( 'pop', function() { | ||
ary.push('a'); | ||
}); | ||
} ); | ||
emitter.once( 'pop', function() { | ||
ary.push('b'); | ||
}); | ||
} ); | ||
emitter.on( 'pop', function() { | ||
ary.push('c'); | ||
}); | ||
} ); | ||
emitter.emitEvent('pop'); | ||
emitter.emitEvent('pop'); | ||
assert.equal( ary.join(','), 'a,b,c,a,c', 'once listener triggered once' ); | ||
t.is( ary.join(','), 'a,b,c,a,c', 'once listener triggered once' ); | ||
@@ -110,16 +107,17 @@ // reset | ||
ary.push('a'); | ||
}); | ||
} ); | ||
emitter.once( 'pop', function() { | ||
ary.push('a'); | ||
}); | ||
} ); | ||
emitter.emitEvent('pop'); | ||
emitter.emitEvent('pop'); | ||
assert.equal( ary.join(','), 'a,a,a', 'identical listeners do not interfere with once' ); | ||
t.is( ary.join(','), 'a,a,a', | ||
'identical listeners do not interfere with once' ); | ||
}); | ||
} ); | ||
it( 'does not infinite loop in once()', function() { | ||
var emitter = new EvEmitter(); | ||
var ticks = 0; | ||
test( 'does not infinite loop in once()', function( t ) { | ||
let emitter = new EvEmitter(); | ||
let ticks = 0; | ||
function onPop() { | ||
@@ -134,10 +132,10 @@ ticks++; | ||
emitter.emitEvent('pop'); | ||
assert.equal( ticks, 1, '1 tick with emitEvent in once' ); | ||
}); | ||
t.is( ticks, 1, '1 tick with emitEvent in once' ); | ||
} ); | ||
it( 'handles emitEvent with no listeners', function() { | ||
var emitter = new EvEmitter(); | ||
assert.doesNotThrow( function() { | ||
test( 'handles emitEvent with no listeners', function( t ) { | ||
let emitter = new EvEmitter(); | ||
t.notThrows( function() { | ||
emitter.emitEvent( 'pop', [ 1, 2, 3 ] ); | ||
}); | ||
} ); | ||
@@ -149,5 +147,5 @@ function onPop() {} | ||
assert.doesNotThrow( function() { | ||
t.notThrows( function() { | ||
emitter.emitEvent( 'pop', [ 1, 2, 3 ] ); | ||
}); | ||
} ); | ||
@@ -158,20 +156,19 @@ emitter.on( 'pop', onPop ); | ||
assert.doesNotThrow( function() { | ||
t.notThrows( function() { | ||
emitter.emitEvent( 'pop', [ 1, 2, 3 ] ); | ||
}); | ||
}); | ||
} ); | ||
} ); | ||
it( 'removes all listeners after allOff', function() { | ||
var emitter = new EvEmitter(); | ||
var ary = []; | ||
test( 'removes all listeners after allOff', function( t ) { | ||
let emitter = new EvEmitter(); | ||
let ary = []; | ||
emitter.on( 'pop', function() { | ||
ary.push('a'); | ||
}); | ||
} ); | ||
emitter.on( 'pop', function() { | ||
ary.push('b'); | ||
}); | ||
} ); | ||
emitter.once( 'pop', function() { | ||
ary.push('c'); | ||
}); | ||
} ); | ||
@@ -182,3 +179,3 @@ emitter.emitEvent('pop'); | ||
assert.equal( ary.join(','), 'a,b,c', 'allOff removed listeners' ); | ||
}); | ||
t.is( ary.join(','), 'a,b,c', 'allOff removed listeners' ); | ||
} ); |
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
113
10191
3
5
253