Skip to content

Commit

Permalink
Drop support for boolean-only options (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Apr 25, 2023
1 parent 5b5138b commit 7b3dc8a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
1 change: 0 additions & 1 deletion delegate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ test('should add a specific event listener only once', () => {
delegate(container, 'a', 'click', spy, {capture: false});

// Capture: true
delegate(container, 'a', 'click', spy, true);
delegate(container, 'a', 'click', spy, {capture: true});

// Once
Expand Down
16 changes: 4 additions & 12 deletions delegate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {ParseSelector} from 'typed-query-selector/parser.d.js';

export type DelegateOptions = boolean | AddEventListenerOptions;
export type DelegateOptions = AddEventListenerOptions;
export type EventType = keyof GlobalEventHandlersEventMap;

export type DelegateEventHandler<
Expand All @@ -15,12 +15,6 @@ export type DelegateEvent<
delegateTarget: TElement;
};

export function castAddEventListenerOptions(
options: DelegateOptions | undefined,
): AddEventListenerOptions {
return typeof options === 'object' ? options : {capture: options};
}

/** Keeps track of raw listeners added to the base elements to avoid duplication */
const ledger = new WeakMap<
EventTarget,
Expand Down Expand Up @@ -106,18 +100,16 @@ function delegate<
selector: string,
type: TEventType,
callback: DelegateEventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: DelegateOptions,
options: DelegateOptions = {},
): void {
const listenerOptions = castAddEventListenerOptions(options);

const {signal} = listenerOptions;
const {signal} = options;

if (signal?.aborted) {
return;
}

// Don't pass `once` to `addEventListener` because it needs to be handled in `delegate-it`
const {once, ...nativeListenerOptions} = listenerOptions;
const {once, ...nativeListenerOptions} = options;

// `document` should never be the base, it's just an easy way to define "global event listeners"
const baseElement = base instanceof Document ? base.documentElement : base;
Expand Down
12 changes: 5 additions & 7 deletions one-event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {ParseSelector} from 'typed-query-selector/parser.d.js';
import delegate, {
castAddEventListenerOptions,
type DelegateEvent,
type DelegateOptions,
type EventType,
Expand Down Expand Up @@ -38,17 +37,16 @@ async function oneEvent<
base: EventTarget,
selector: string,
type: TEventType,
options?: DelegateOptions,
options: DelegateOptions = {},
): Promise<DelegateEvent<GlobalEventHandlersEventMap[TEventType], TElement> | undefined> {
return new Promise(resolve => {
const listenerOptions = castAddEventListenerOptions(options);
listenerOptions.once = true;
options.once = true;

if (listenerOptions.signal?.aborted) {
if (options.signal?.aborted) {
resolve(undefined);
}

listenerOptions.signal?.addEventListener('abort', () => {
options.signal?.addEventListener('abort', () => {
resolve(undefined);
});

Expand All @@ -58,7 +56,7 @@ async function oneEvent<
type,
// @ts-expect-error Seems to work fine
resolve,
listenerOptions,
options,
);
});
}
Expand Down
17 changes: 5 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

> Lightweight event delegation
This is a fork of the popular [`delegate`](https://github.com/zenorocha/delegate) with some improvements:
This is a fork of the popular but abandoned [`delegate`](https://github.com/zenorocha/delegate) with some improvements:

- modern: ES2021, TypeScript, Edge 16+ (it uses `WeakMap` and `Element.closest()`)
- idempotent: identical listeners aren't added multiple times, just like the native `addEventListener`
Expand All @@ -29,22 +29,15 @@ import delegate from 'delegate-it';

### Add event delegation

#### With an element as base

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

#### With listener options
### With listener options

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

// Or equivalent:
delegate(document, '.btn', 'click', event => {
console.log(event.delegateTarget);
}, {
Expand All @@ -65,7 +58,7 @@ delegate(document, '.btn', 'click', event => {
controller.abort();
```

#### Listen to one event only
### Listen to one event only

```js
delegate(document, '.btn', 'click', event => {
Expand All @@ -75,7 +68,7 @@ delegate(document, '.btn', 'click', event => {
});
```

#### Listen to one event only, with a promise
### Listen to one event only, with a promise

```js
import {oneEvent} from 'delegate-it';
Expand All @@ -84,7 +77,7 @@ await oneEvent(document, '.btn', 'click');
console.log('The body was clicked');
```

### Custom event types in Typescript
## TypeScript

If you're using TypeScript and have event types that are custom, you can override the global `GlobalEventHandlersEventMap` interface via declaration merging. e.g. say you have a `types/globals.d.ts` file, you can add the following.

Expand Down

0 comments on commit 7b3dc8a

Please sign in to comment.