| 1279 | * @param name - An event name to wait for |
| 1280 | */ |
| 1281 | export async function once<T>(ee: EventEmitter, name: string, options?: Abortable): Promise<T> { |
| 1282 | options?.signal?.throwIfAborted(); |
| 1283 | |
| 1284 | const { promise, resolve, reject } = promiseWithResolvers<T>(); |
| 1285 | const onEvent = (data: T) => resolve(data); |
| 1286 | const onError = (error: Error) => reject(error); |
| 1287 | const abortListener = addAbortListener(options?.signal, function () { |
| 1288 | reject(this.reason); |
| 1289 | }); |
| 1290 | |
| 1291 | ee.once(name, onEvent).once('error', onError); |
| 1292 | |
| 1293 | try { |
| 1294 | return await promise; |
| 1295 | } finally { |
| 1296 | ee.off(name, onEvent); |
| 1297 | ee.off('error', onError); |
| 1298 | abortListener?.[kDispose](); |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | export function maybeAddIdToDocuments( |
| 1303 | collection: Collection, |