@axetroy/event-emitter.js
v1.2.4
Published
A Javascript event emitter implementation without any dependencies. only 1.4Kb
Downloads
502
Readme
event emitter for Javascript
A Javascript event emitter implementation without any dependencies.
API
const EventEmitter = require('@axetroy/event-emitter.js');
const event = new EventEmitter();
const cancelSayHello = event.on('greet', function(name) {
console.log(`hello ${name}`);
});
const cancelSayHi = event.on('greet', function(name) {
console.log(`hi ${name}`);
});
event.emit('greet', 'Axetroy');
// hello Axetroy
// hi Axetroy
cancelSayHello(); // remove this listener
event.emit('greet', 'Axetroy');
// hi Axetroy
event.off('greet');
event.emit('greet', 'Axetroy'); // nothing happen
event.on(String event, Function listener(...data))
listen an event, return a Function to remove this listener.
event.once(String event, Function listener(...data))
listen an event only once, it will remove the listener once it trigger
event.off(String event)
remove all listener about this event
event.clear()
clean all listener
event.emit(String event, ...data)
fire an event, without an optional data, data can be multiple arguments
event.emitting(String event, Array dataArray, Function listener)
do not invoke this method in manual.
use this method extend your event emitter
class MyEventEmitter extends EventEmitter {
emitting(event, dataArray, listener) {
const start = Date.now();
listener.apply(this, dataArray); // the default method is only contain this line
const time = Date.now() - start;
console.log('time: ' + time);
}
}
Test
yarn run test
Contribute
git clone https://github.com/axetroy/event-emitter.js.git
cd ./event-emitter.js
yarn run watch
You can flow Contribute Guide
License
The MIT License