From b49da0e533de7771ccc50465d15a51f157bde568 Mon Sep 17 00:00:00 2001 From: Dmitry Shovchko Date: Tue, 7 May 2024 12:33:23 +0300 Subject: [PATCH] feat(esl-utils): add ability to reject promisifyEvent by using AbortSignal --- src/modules/esl-utils/async/promise/event.ts | 3 +++ src/modules/esl-utils/async/test/promise/event.test.ts | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/modules/esl-utils/async/promise/event.ts b/src/modules/esl-utils/async/promise/event.ts index 892e8d062..0763d5225 100644 --- a/src/modules/esl-utils/async/promise/event.ts +++ b/src/modules/esl-utils/async/promise/event.ts @@ -33,6 +33,9 @@ export function promisifyEvent( if (typeof timeout === 'number' && timeout >= 0) { setTimeout(eventCallback, timeout); } + if (typeof options === 'object') { + options?.signal?.addEventListener('abort', () => reject(new Error('Rejected by abort signal')), {once: true}); + } }); } diff --git a/src/modules/esl-utils/async/test/promise/event.test.ts b/src/modules/esl-utils/async/test/promise/event.test.ts index 381731aa0..634971f7c 100644 --- a/src/modules/esl-utils/async/test/promise/event.test.ts +++ b/src/modules/esl-utils/async/test/promise/event.test.ts @@ -37,6 +37,13 @@ describe('async/promise/event', () => { expect(spy).toBeCalledWith('test', expect.any(Function), undefined); } }); + test('Rejected by abort signal', async () => { + const el = document.createElement('div'); + const controller = new AbortController(); + const promise$ = promisifyEvent(el, 'test', null, {signal: controller.signal}); + controller.abort(); + await expect(promise$).rejects.toBeInstanceOf(Error); + }); }); describe('promisifyMarker', () => {