Skip to content

Commit

Permalink
Ignore unsupported once option (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheap-glitch authored May 13, 2022
1 parent ec81f44 commit 0081742
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
15 changes: 11 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type {ParseSelector} from 'typed-query-selector/parser';

// eslint-disable-next-line @typescript-eslint/ban-types -- It's a single property, no mistakes possible
export type DelegateOptions = boolean | Omit<AddEventListenerOptions, 'once'>;
export type EventType = keyof GlobalEventHandlersEventMap;
type GlobalEvent = Event;

Expand Down Expand Up @@ -82,7 +84,7 @@ function safeClosest(event: Event, selector: string): Element | void {

/**
* Delegates event to a selector.
* @param options A boolean value setting options.capture or an options object of type AddEventListenerOptions
* @param options A boolean value setting options.capture or an options object of type AddEventListenerOptions without the `once` option
*/
function delegate<
Selector extends string,
Expand All @@ -93,7 +95,7 @@ function delegate<
selector: Selector,
type: TEventType,
callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: boolean | AddEventListenerOptions
options?: DelegateOptions
): delegate.Subscription;

function delegate<
Expand All @@ -104,7 +106,7 @@ function delegate<
selector: string,
type: TEventType,
callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: boolean | AddEventListenerOptions
options?: DelegateOptions
): delegate.Subscription;

// This type isn't exported as a declaration, so it needs to be duplicated above
Expand All @@ -116,7 +118,7 @@ function delegate<
selector: string,
type: TEventType,
callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: boolean | AddEventListenerOptions
options?: DelegateOptions
): delegate.Subscription {
// Handle Selector-based usage
if (typeof base === 'string') {
Expand Down Expand Up @@ -160,6 +162,11 @@ function delegate<
}
};

// Drop unsupported `once` option https://github.com/fregante/delegate-it/pull/28#discussion_r863467939
if (typeof options === 'object') {
delete (options as AddEventListenerOptions).once;
}

const setup = JSON.stringify({selector, type, capture});
const isAlreadyListening = editLedger(true, baseElement, callback, setup);
const delegateSubscription = {
Expand Down
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ delegate(document.querySelectorAll('.container'), '.btn', 'click', event => {
});
```

#### With listener options

```js
delegate(document.body, '.btn', 'click', event => {
console.log(event.delegateTarget);
}, true);

// Or equivalent:
delegate(document.body, '.btn', 'click', event => {
console.log(event.delegateTarget);
}, {
capture: true
});
```

**Note:** the `once` option is currently not supported.

### Remove event delegation

```js
Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,12 @@ test.serial('should not fire when the selector matches an ancestor of the base e
anchor.click();
t.true(spy.notCalled);
});

test.serial('should not consider the `once` option', t => {
const spy = sinon.spy();
delegate(container, 'a', 'click', spy, {once: true});

anchor.click();
anchor.click();
t.true(spy.calledTwice);
});

0 comments on commit 0081742

Please sign in to comment.