event-emitter-lite
v1.6.3
Published
simple event emitter to use with typescript and pure javascript no depences
Downloads
55
Readme
event-emitter-lite
npm install event-emitter-lite
use
import to use
let eventemitter = require("event-emitter-lite");
API
let onTest = new eventemitter.EventEmitter();
subscribe
onTest.subscribe(msg => console.log(msg));
once
onTest.once(msg => console.log(`One more time: ${msg}`));
cancel the next call
let egoistSubscribe = onTest.subscribe(msg =>{
console.log(`the Dalek sad: ${msg}`);
onTest.cancel();
});
onTest.subscribe(msg => console.log(`Im not a Dalek: ${msg}`));
emit
onTest.emit('go go go!!!');
unsubscribe
onTest.unsubscribe(egoistSubscribe);
cancel a event
onTest.cancel();
cancel a subscribe
egoistSubscribe.cancel();
emit again and again ....
onTest.emit('go go go!!!');
getting emitted value
onTest.emit('last chance...');
console.log(`Would it recieve the last msg? ${onTest.emittedValue}`);
//yes :)
working with errors
with javascript
let onTest = new eventemitter.EventEmitter();
onTest.subscribe(
msg => console.log(msg)
, err => console.log(err)
);
onTest.error('It´s is a multiverse erro!');
with typescript
let onTest = new EventEmitter<string,string>();
onTest.subscribe(
msg => console.log(msg)
, err => console.log(err)
);
onTest.error('It´s is a multiverse erro!');
emiting a value (can do a promise) after a emit
onTest.subscribe(msg => 'finished!');
let afterEmit = onTest
.emit('its ok');
// ['finished!']
with promise
onTest.subscribe(msg => {
//...do any thing with 'msg';
return new Promise(sucess => {
setTimeout(() => sucess('finished!'),2000);
});
});
Promise.all(
onTest
.emit('its ok')
)
.then(() => console.log('finish!!!'));