event-mitt
A mini and light event emitter
apis
name | params | description |
---|
on | name/*,handler | register an event |
off | name,handler | unregister an event |
emit | name,obj | fire an event |
one | name,obj | fire an event,only can register once |
once | name,obj | fire an event,only can execute once |
installation
npm install -S @feizheng/event-mitt
usage
import EventMitt from '@feizheng/event-mitt';
const Person = class { };
Object.assign(Person.prototype, EventMitt);
const p1 = new Person();
const sum = 0;
const total = 0;
var res = p1.on('ev1', () => {
console.log('ev1', sum);
sum = sum + 1;
});
p1.on('ev2', () => {
console.log('ev2', sum);
sum = sum + 3;
});
p1.on('ev3', () => {
console.log('ev3', sum);
sum = sum + 5;
});
p1.on('*', (name) => {
console.log('just a log', name);
total++;
});
p1.emit('ev1');
p1.emit('ev2');
p1.emit('ev3');
res.destroy();
resources
license
Code released under the MIT license.