another event emiiter.
features:
- can listen async/conjoined event.
- support to mixed async/sync handlers.
- return unscriber when you subscribe an event.
npm install xevt
Then:
import { Xevt } from "xevt";
const emitter = new Xevt();
let result = 0;
emitter.on("event", () => {
result++;
});
emitter.emit("event");
const emitter = new Xevt();
let result = 0;
emitter.onAsync("event", async () => {
result++;
});
await emitter.emit("event");
IMPORTANT: conjoined events are not supported any arguments in handlers.
const emitter = new Xevt();
let count = 0;
emitter.on(["event1", "event2"], () => {
count++;
});
emitter.emit("event1");
emitter.emit("event2");
console.log(count); // 1
const emitter = new Xevt();
const result: number[] = [];
emitter.on("event", (data) => {
result.push(data);
});
emitter.onAsync(
"event",
async (data) =>
new Promise((res) => {
setTimeout(() => {
result.push(data);
res(true);
}, 10);
}),
);
for (let i = 0; i < 5; i++) {
emitter.emit("event", i);
}
// [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
const emitter = new Xevt();
const result: number[] = [];
emitter.conjoin(["event1", "event2"], async () => {
result.push(1);
});
emitter.conjoinAsync(["event1", "event2"], async () => {
result.push(2);
});
for (let i = 0; i < 5; i++) {
emitter.emit("event1");
emitter.emit("event2");
}
// [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
const emitter = new Xevt();
const result: number[] = [];
const unscribe = emitter.on("event", (arg: number) => {
result.push(arg);
});
unscribe();
const emitter = new Xevt();
const result: number[] = [];
const unscribe = emitter.conjoin(["event1", "event2"], () => {
result.push(1);
});
unscribe();
const emitter = new Xevt();
emitter.debug = true;
Then you can see the debug information in the console.