1.3.5 • Published 4 years ago
event-emitte-ts v1.3.5
EventEmitter in TypeScript
npm i --save es-event-emitter
or
git clone https://github.com/Zlobin/es-event-emitter.git
cd es-event-emitter && npm i && webpack
Benchmark
Run npm start.
const EM = new EventEmitter();
EM
.on('foo', () => 'response')
.emit('foo');Dependencies
There are no dependencies. You need only npm installed and just run npm i to grab the development dependencies.
Examples
var EM = require('es-event-emitter');html include:
<script src="<PATH/TO/LIBRARY>/dist/bundle.js">Or ES2015 import:
import EM from 'es-event-emitter';
// ...Creating an instance.
var EM = new EventEmitter();An usual example.
EM.on('foo', function() {
// some code...
});
EM.emit('foo');It will be triggered only once and then callbacks will be removed.
EM.once('foo', function() {
// some code...
});
EM.emit('foo');
// Nothing happend.
EM.emit('foo');Callback with parameters.
EM.once('foo', function(bar, baz) {
// some code...
});
EM.emit('foo', 'var 1 for bar', 'var 2 for baz');Callback's call can be ordered by "weight" parameter.
EM.on('foo', function() {
console.log('3');
}, null, 1);
EM.on('foo', function() {
console.log('1');
}, null, 3);
EM.on('foo', function() {
console.log('2');
}, null, 2);
EM.emit('foo');
// 3
// 2
// 1Chaining.
EM.on('foo', function() {
// some code...
});
EM
.emit('foo')
.emit('foo')
.off('foo');Set maxNumberOfListeners as a parameter when creating new instance.
const EM = new EventEmitter(1);
EM.on('foo', function() {
// some code...
});
// Note: it will show notification in console.
EM.on('foo', function() {
// some other code...
});Testing
Tests are performed using mocha and expect library npm test.
Building the documentation
You can use JSDoc comments found within the source code.
Todo
- Add event's namespace:
EM.on('foo.*', function() {
// some code...
});- Add events through comma:
EM.on('foo,bar,baz', function() {
// some code...
});- Add method "onAny" for listening each event:
EM.onAny(function() {
// some code...
});