Skip to content

Commit

Permalink
Don't return AbortController (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Aug 31, 2022
1 parent 00b85c8 commit 8a6c2f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
26 changes: 8 additions & 18 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function delegate<
type: TEventType,
callback: DelegateEventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: DelegateOptions
): AbortController;
): void;

function delegate<
TElement extends Element = HTMLElement,
Expand All @@ -99,7 +99,7 @@ function delegate<
type: TEventType,
callback: DelegateEventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: DelegateOptions
): AbortController;
): void;

// This type isn't exported as a declaration, so it needs to be duplicated above
function delegate<
Expand All @@ -111,23 +111,15 @@ function delegate<
type: TEventType,
callback: DelegateEventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: DelegateOptions,
): AbortController {
const internalController = new AbortController();
): void {
const listenerOptions: AddEventListenerOptions = typeof options === 'object' ? options : {capture: options};
// Drop unsupported `once` option https://github.com/fregante/delegate-it/pull/28#discussion_r863467939
delete listenerOptions.once;

if (listenerOptions.signal) {
if (listenerOptions.signal.aborted) {
internalController.abort();
return internalController;
}
const {signal} = listenerOptions;

listenerOptions.signal.addEventListener('abort', () => {
internalController.abort();
});
} else {
listenerOptions.signal = internalController.signal;
if (signal?.aborted) {
return;
}

// Handle Selector-based usage
Expand All @@ -141,7 +133,7 @@ function delegate<
delegate(element, selector, type, callback, listenerOptions);
}

return internalController;
return;
}

// `document` should never be the base, it's just an easy way to define "global event listeners"
Expand All @@ -163,11 +155,9 @@ function delegate<
baseElement.addEventListener(type, listenerFn, listenerOptions);
}

internalController.signal.addEventListener('abort', () => {
signal?.addEventListener('abort', () => {
editLedger(false, baseElement, callback, setup);
});

return internalController;
}

export default delegate;
9 changes: 6 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ test.serial('should handle events on text nodes', t => {

test.serial('should remove an event listener', t => {
const spy = sinon.spy();
const controller = delegate(container, 'a', 'click', spy);
const controller = new AbortController();
delegate(container, 'a', 'click', spy, {signal: controller.signal});
controller.abort();

const anchor = document.querySelector('a');
Expand Down Expand Up @@ -84,7 +85,8 @@ test.serial('should add event listeners to all the elements in a base selector',

test.serial('should remove the event listeners from all the elements in a base selector', t => {
const spy = sinon.spy();
const controller = delegate('li', 'a', 'click', spy);
const controller = new AbortController();
delegate('li', 'a', 'click', spy, {signal: controller.signal});
controller.abort();

for (const anchor of document.querySelectorAll('a')) {
Expand Down Expand Up @@ -121,7 +123,8 @@ test.serial('should add event listeners to all the elements in a base array', t
test.serial('should remove the event listeners from all the elements in a base array', t => {
const spy = sinon.spy();
const items = document.querySelectorAll('li');
const controller = delegate(items, 'a', 'click', spy);
const controller = new AbortController();
delegate(items, 'a', 'click', spy, {signal: controller.signal});
controller.abort();

for (const anchor of document.querySelectorAll('a')) {
Expand Down

0 comments on commit 8a6c2f2

Please sign in to comment.