Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't return AbortController #32

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Drop internal controller
  • Loading branch information
fregante committed Aug 3, 2022
commit 67a2ae25ac09ab9ed6d34d75cde60ed5eb43780f
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