2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-10-02 08:13:57 -04:00
|
|
|
|
2024-09-05 03:22:52 -04:00
|
|
|
// @ts-expect-error: @types/node is outdated
|
|
|
|
import events, { addAbortListener, EventEmitter } from "node:events";
|
2023-10-02 08:13:57 -04:00
|
|
|
|
|
|
|
EventEmitter.captureRejections = true;
|
|
|
|
|
|
|
|
Deno.test("regression #20441", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-10-02 08:13:57 -04:00
|
|
|
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
|
|
|
|
ee.on("foo", function () {
|
|
|
|
const p = new Promise((_resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
reject();
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
return p;
|
|
|
|
});
|
|
|
|
|
|
|
|
ee.on("error", function (_) {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-10-02 08:13:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
ee.emit("foo");
|
|
|
|
await promise;
|
|
|
|
});
|
2024-03-20 01:50:18 -04:00
|
|
|
|
|
|
|
Deno.test("eventemitter async resource", () => {
|
|
|
|
// @ts-ignore: @types/node is outdated
|
|
|
|
class Foo extends events.EventEmitterAsyncResource {}
|
|
|
|
|
|
|
|
const foo = new Foo();
|
|
|
|
// @ts-ignore: @types/node is outdated
|
|
|
|
foo.emit("bar");
|
|
|
|
});
|
2024-09-05 03:22:52 -04:00
|
|
|
|
|
|
|
Deno.test("addAbortListener", async () => {
|
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
|
|
|
const abortController = new AbortController();
|
|
|
|
addAbortListener(abortController.signal, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
abortController.abort();
|
|
|
|
await promise;
|
|
|
|
});
|