1.1.0 • Published 6 years ago
asynciterable-pipeable-chainable-event-hub v1.1.0
asyncIterable-pipeable-chainable-event-hub
chainable, asyncIterable, pipeable with type infer
It will be useful when work with EventEmitter, rxjs, Socket (Socket extends EventEmitter) or other event driven bussiness.
Example
npm install asyncIterable-pipeable-chainable-event-hub
#or
yarn add asyncIterable-pipeable-chainable-event-hubCreate an instance
create an EventLite instance
import { EventLite } from "asyncIterable-pipeable-chainable-event-hub";
const eventLite = new EventLite();optional but useful event hub
const someEvent = eventLite.eventHandle("eventName")<[number, string]>();Emit
emit on anywhere
let i = 0;
setInterval(() => {
// with type check
someEvent.handleEmit(++i, i + "");
// or without type check
eventLite.emit("eventName", ++i, i + "");
}, 1000);Add listener
chainable and with type infer
// or
someEvent.handleOn(console.info).handleOnce(console.log).handleRemove();
// or
eventLite
.on("eventName", (n: number, s: string) => {
console.log(n, s);
})
.handleOn(console.info)
.handleOnce(console.log)
.handleRemove(undefined);
// or
eventLite
.eventHandle("eventName")<[number, string]>()
.handleOn(console.info)
.handleOnce(console.log)
.handleRemove(undefined);Async Iterable
asyncIterable and with type infer
for await (const { data, cancel } of someEvent.iterable()) {
console.log(data);
}Pipeable
pipeable and with type infer
const followEvent = someEvent
.handlePipe((n, s) => {
return n;
})
.handleOn(console.info)
.handleOnce(console.log)
.handleRemove(undefined);connect and pipe to a another or a new EventLite instance
const followEventLite = someEvent
.handleConnect()
.handleOn(console.info)
.handleOnce(console.log)
.handleRemove(undefined).eventLite;Note: about remove
remove function have two arg, first one is event key,secondone is event listener callback, typedRemove just need the second arg of remove.
// just remove eventListener from listeners list of the event for `event key`
eventLite.remove("event key", eventListener);
// remove all eventListener from listeners list of the event for `event key`
eventLite.remove("event key", undefined);
// remove eventListener from listeners list of all event
eventLite.remove(undefined, eventListener);