UNPKG

abort-utils

Version:

Utility functions to use and combine `AbortSignal` and `AbortController` with Promises

29 lines (28 loc) 1.29 kB
/** * Create an abort signal that will be aborted when the specified event is triggered on the target. * @param target The target to attach the listener to. This can be any `Element` or other object that implements the `EventTarget` interface. * @param event The event to listen for. * @param options Options to pass to the `addEventListener` method (except `once`, which is always set to `true`). This can be used to specify the `capture` option or `passive` option or to specify a `signal`. * @param options.filter A function that will be called with the event. The signal will only be aborted if this function returns `true`. */ export function signalFromEvent(target, event, { filter, ...options } = {}) { const controller = new AbortController(); const listener = filter ? async (event) => { let shouldAbort = filter(event); if (typeof shouldAbort === 'object' && 'then' in shouldAbort) { shouldAbort = await shouldAbort; } if (shouldAbort) { controller.abort(event); } } : () => { controller.abort(); }; target.addEventListener(event, listener, { ...options, once: !filter, }); return controller.signal; }