micro event emitter in TypeScript.
Before v1.2.0, micro-emitter made by es6. If you want see that, please check it.
I wanted a simple emitter for my apps I created. So, I made an independent, no-dependency emitter under 100 lines.
$ npm install micro-emitter
import MicroEmitter from 'micro-emitter';
let emitter = new MicroEmitter();
var MicroEmitter = require('micro-emitter');
var emitter = new MicroEmitter();
It has 4 API and some alias only.
- addListener(on)
- addOnceListener(once)
- removeLister(off)
- emit(trigger)
const CHANGE_EVENT = 'CHANGE_EVENT';
emitter.addListener(CHANGE_EVENT, callbak);
emitter.on(CHANGE_EVENT, callbak);
emitter.addOnceListener(CHANGE_EVENT, callbak); // only first time
emitter.once(CHANGE_EVENT, callbak); // only first time
const CHANGE_EVENT = 'CHANGE_EVENT';
emitter.removeListener(CHANGE_EVENT);
emitter.off(CHANGE_EVENT);
const CHANGE_EVENT = 'CHANGE_EVENT';
emitter.addListener(CHANGE_EVENT, (payload) => {
console.log(payload); // { message: 'Hello MicroEmitter!' }
});
emitter.emit(CHANGE_EVENT, { message: 'Hello MicroEmitter!' });
import MicroEmitter from 'micro-emitter';
const CHANGE_EVENT = 'CHANGE_EVENT';
const emitter = new MicroEmitter();
emitter.addListener(CHANGE_EVENT, (payload) => {
alert(payload.message);
});
setTimeout(() => {
emitter.emit(CHANGE_EVENT, { message: 'Hello MicroEmitter!' }});
}, 1000);