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