2019-03-10 22:38:44 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview A set of helper functions to make it easier to work
|
|
|
|
* with events in async/await manner.
|
|
|
|
*/
|
|
|
|
|
2023-06-15 14:42:27 +00:00
|
|
|
import { on } from 'node:events';
|
2019-03-10 22:38:44 +00:00
|
|
|
|
2019-09-05 17:56:06 +00:00
|
|
|
export const emittedNTimes = async (emitter: NodeJS.EventEmitter, eventName: string, times: number, trigger?: () => void) => {
|
2019-03-10 22:38:44 +00:00
|
|
|
const events: any[][] = [];
|
2023-02-23 23:53:53 +00:00
|
|
|
const iter = on(emitter, eventName);
|
|
|
|
if (trigger) await Promise.resolve(trigger());
|
|
|
|
for await (const args of iter) {
|
|
|
|
events.push(args);
|
|
|
|
if (events.length === times) { break; }
|
2019-09-05 17:56:06 +00:00
|
|
|
}
|
2023-02-23 23:53:53 +00:00
|
|
|
return events;
|
2019-03-10 22:38:44 +00:00
|
|
|
};
|
2019-10-18 19:57:34 +00:00
|
|
|
|
|
|
|
export const emittedUntil = async (emitter: NodeJS.EventEmitter, eventName: string, untilFn: Function) => {
|
2023-02-23 23:53:53 +00:00
|
|
|
for await (const args of on(emitter, eventName)) {
|
|
|
|
if (untilFn(...args)) { return args; }
|
|
|
|
}
|
2019-10-18 19:57:34 +00:00
|
|
|
};
|