event-emitter
Advanced tools
Comparing version 0.1.5 to 0.2.0
138
lib/index.js
'use strict'; | ||
var slice = Array.prototype.slice | ||
, defineProperty = Object.defineProperty | ||
, defineProperties = Object.defineProperties | ||
, copy = require('es5-ext/lib/Array/prototype/copy') | ||
, d = require('es5-ext/lib/Object/descriptor') | ||
, on, once, off, allOff, emit, getMethods, pname; | ||
pname = '- ee -'; | ||
on = function (type, listener) { | ||
if (!this[pname]) { | ||
defineProperty(this, pname, d('c', {})); | ||
} | ||
if (!this[pname][type]) { | ||
(this[pname][type] = []).copy = copy; | ||
} | ||
this[pname][type].push(listener); | ||
return this; | ||
}; | ||
once = function (type, listener) { | ||
var offt = off.bind(this), nlistener; | ||
if (!this[pname + ' once']) { | ||
defineProperty(this, pname + ' once', d('c', {})); | ||
} | ||
if (!this[pname + ' once'][type]) { | ||
this[pname + ' once'][type] = [[], []]; | ||
} | ||
nlistener = function nlistener() { | ||
offt(type, listener); | ||
listener.apply(this, arguments); | ||
}; | ||
this[pname + ' once'][type][0].push(nlistener[pname + ' listener'] = listener); | ||
this[pname + ' once'][type][1].push(nlistener); | ||
return on.call(this, type, nlistener); | ||
}; | ||
off = function (type, listener) { | ||
var index, ls = this[pname] && this[pname][type]; | ||
if (ls && ((index = ls.indexOf(listener)) !== -1)) { | ||
ls.splice(index, 1); | ||
} | ||
ls = this[pname + ' once'] && this[pname + ' once'][type]; | ||
if (ls && ((index = ls[0].indexOf(listener)) !== -1)) { | ||
ls[0].splice(index, 1); | ||
off.call(this, type, ls[1][index]); | ||
ls[1].splice(index, 1); | ||
} | ||
return this; | ||
}; | ||
allOff = d('', function () { | ||
delete this[pname]; | ||
delete this[pname + ' once']; | ||
}); | ||
emit = function (a, b) { | ||
var args, emitter, type, listeners; | ||
if (typeof a === 'string') { | ||
emitter = null; | ||
type = a; | ||
args = slice.call(arguments, 1); | ||
} else { | ||
emitter = a; | ||
type = b; | ||
args = slice.call(arguments, 2); | ||
} | ||
if (this[pname] && this[pname][type]) { | ||
listeners = this[pname][type].copy(); | ||
listeners.forEach(emitter ? function (listener) { | ||
if ((listener[pname + ' listener'] || listener) !== emitter) { | ||
listener.apply(this, args); | ||
} | ||
} : function (listener) { | ||
listener.apply(this, args); | ||
}.bind(this), this); | ||
} | ||
return this; | ||
}; | ||
getMethods = (function () { | ||
var hidden, exposed, lExposed, eExposed; | ||
hidden = { | ||
on: d('', on), | ||
once: d('', once), | ||
off: d('', off), | ||
emit: d('', emit), | ||
allOff: allOff | ||
}; | ||
exposed = { | ||
on: d('e', on), | ||
once: d('e', once), | ||
off: d('e', off), | ||
emit: d('e', emit), | ||
allOff: allOff | ||
}; | ||
lExposed = { | ||
on: d('e', on), | ||
once: d('e', once), | ||
off: d('e', off), | ||
emit: d('', emit), | ||
allOff: allOff | ||
}; | ||
eExposed = { | ||
on: d('', on), | ||
once: d('', once), | ||
off: d('', off), | ||
emit: d('e', emit), | ||
allOff: allOff | ||
}; | ||
return function (l, e) { | ||
if (l) { | ||
if (e) { | ||
return exposed; | ||
} else { | ||
return lExposed; | ||
} | ||
} else if (e) { | ||
return eExposed; | ||
} else { | ||
return hidden; | ||
} | ||
}; | ||
}()); | ||
module.exports = exports = function (o, lExpose, eExpose) { | ||
return defineProperties(o || {}, getMethods(lExpose, eExpose)); | ||
}; | ||
exports.getMethods = getMethods; | ||
var ee = module.exports = require('./core'); | ||
ee.allOff = require('./all-off'); | ||
ee.pipe = require('./pipe'); |
{ | ||
"name": "event-emitter", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "Basic event emitter for Node.js and browser", | ||
@@ -24,3 +24,3 @@ "keywords": [ | ||
"dependencies": { | ||
"es5-ext": "0.8.x" | ||
"es5-ext": "0.9.x" | ||
}, | ||
@@ -27,0 +27,0 @@ "scripts": { |
@@ -1,39 +0,76 @@ | ||
# Event emitter | ||
# EventEmitter – Cross-environment event emitter solution for JavaScript | ||
Basic event emitter for Node.js and Browsers | ||
## Installation | ||
### NPM | ||
Node & npm: | ||
In your project path: | ||
$ npm install event-emitter | ||
### Browser | ||
Browser bundle can be easily created with help of [modules-webmake](https://github.com/medikoo/modules-webmake). Mind that it relies on some EcmaScript5 features, so for older browsers you need as well [es5-shim](https://github.com/kriskowal/es5-shim). | ||
## Usage | ||
var ee = require('event-emitter'); | ||
```javascript | ||
var ee = require('event-emitter'); | ||
var MyCostructor = function () {}; | ||
ee(MyConstructor.prototype); | ||
var emitter = ee({}), listener; | ||
var myObj = new MyConstructor(); | ||
emitter.on('test', listener = function (args) { | ||
// …emitter logic | ||
}); | ||
// Register listener: | ||
var listener; | ||
myObj.on('name', listener = function (args) { | ||
// ... whatever | ||
}); | ||
emitter.once('test', function (args) { | ||
// …invoked only once(!) | ||
}); | ||
// Register listener that would be removed after first emit: | ||
myObj.once('name', function (args) { | ||
// ... whatever | ||
}); | ||
emitter.emit('test', arg1, arg2/*…args*/); // Two above listeners invoked | ||
emitter.emit('test', arg1, arg2/*…args*/); // Only first listener invoked | ||
// Remove registered listener | ||
myObj.off('name', listener); | ||
emitter.off('test', listener); // Removed first listener | ||
emitter.emit('test', arg1, arg2/*…args*/); // No listeners invoked | ||
``` | ||
// Emit event | ||
myObj.emit('name', arg1/*, arg2, arg3*/); | ||
## Additional functionalities (provided as separate modules) | ||
### allOff | ||
Remove all listeners | ||
```javascript | ||
var eeAllOff = require('event-emitter/lib/all-off'); | ||
eeAllOff(emitter); // Removed all registered listeners on emitter | ||
``` | ||
### pipe | ||
Pipe events from one emitter to other | ||
```javascript | ||
var eePipe =require('event-emitter/lib/pipe'); | ||
var emitter1 = ee(), listener1; | ||
var emitter2 = ee(), listener2; | ||
emitter1.on('test', listener1 = function () { }); | ||
emitter2.on('test', listener2 = function () { }); | ||
emitter1.emit('test'); // Invoked listener1 | ||
emitter2.emit('test'); // Invoked listener2 | ||
var pipe = eePipe(emitter1, emitter2); | ||
emitter1.emit('test'); // Invoked listener1 and listener2 | ||
emitter2.emit('test'); // Invoked just listener2 | ||
pipe.close(); | ||
emitter1.emit('test'); // Invoked listener1 | ||
emitter2.emit('test'); // Invoked listener2 | ||
``` | ||
## Tests [![Build Status](https://secure.travis-ci.org/medikoo/event-emitter.png?branch=master)](https://secure.travis-ci.org/medikoo/event-emitter) | ||
$ npm test |
'use strict'; | ||
module.exports = function (t, a) { | ||
var o = Object.create(t({})) | ||
, removed = false, listener, count = 0; | ||
var x = {}, y, count, count2; | ||
a(o.propertyIsEnumerable('on'), false, "Properties not enumerable"); | ||
o.on('test', function (x, y, z) { | ||
if (count === 1) { | ||
a(x + y + z, 7, "Emitted"); | ||
} else if (count === 2) { | ||
a(x + y + z, 11, "Emitted second time"); | ||
} | ||
// Basic check | ||
count = 0; | ||
t(x); | ||
x.on('foo', function () { | ||
++count; | ||
}); | ||
o.on('test', listener = function (x, y, z) { | ||
if (count === 1) { | ||
a(x + y + z, 7, "Emitted second listener"); | ||
} else { | ||
a.never("Removed listener"); | ||
} | ||
}); | ||
x.emit('foo'); | ||
o.on('other-test', function (x, y, z) { | ||
if (count === 1) { | ||
a(x + y + z, 5, "Other event"); | ||
} | ||
}); | ||
a(count, 1, "Emitted"); | ||
o.once('test', function (x, y, z) { | ||
if (count === 1) { | ||
a(x + y + z, 7, "Once"); | ||
} else { | ||
a.never("Once run once"); | ||
} | ||
}); | ||
t.allOff(x); | ||
x.emit('foo'); | ||
a(count, 1, "All Off"); | ||
++count; | ||
o.emit({}, 'test', 1, 2, 4); | ||
o.emit({}, 'other-test', 1, 3, 1); | ||
o.off('test', listener); | ||
++count; | ||
o.emit('test', 1, 7, 3); | ||
y = t(); | ||
count = 0; | ||
o.once('off-mid', function () { | ||
count2 = 0; | ||
x.on('foo', function () { | ||
++count; | ||
}); | ||
o.on('off-mid', listener = function () { | ||
++count; | ||
y.on('foo', function () { | ||
++count2; | ||
}); | ||
o.on('off-mid', listener = function () { | ||
++count; | ||
}); | ||
o.emit('off-mid'); | ||
a(count, 3, "Run all listeners"); | ||
o.once('test', a.never); | ||
o.off('test', a.never); | ||
o.emit('test'); | ||
t.pipe(x, y); | ||
x.emit('foo'); | ||
a(count, 1, "Pipe: x emitted"); | ||
a(count2, 1, "Pipe: y emitted"); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
18109
18
530
77
0
1
+ Addedes5-ext@0.9.2(transitive)
- Removedes5-ext@0.8.2(transitive)
Updatedes5-ext@0.9.x