Skip to content

Commit

Permalink
Output modern code
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jun 23, 2022
1 parent 4f3d7ea commit 6ac66e2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
45 changes: 21 additions & 24 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@ import type {ParseSelector} from 'typed-query-selector/parser';

export type DelegateOptions = boolean | Omit<AddEventListenerOptions, 'once'>;
export type EventType = keyof GlobalEventHandlersEventMap;
type GlobalEvent = Event;

namespace delegate {
export type EventHandler<
TEvent extends GlobalEvent = GlobalEvent,
TElement extends Element = Element,
> = (event: Event<TEvent, TElement>) => void;

export type Event<
TEvent extends GlobalEvent = GlobalEvent,
TElement extends Element = Element,
> = TEvent & {
delegateTarget: TElement;
};
}

export type DelegateEventHandler<
TEvent extends Event = Event,
TElement extends Element = Element,
> = (event: DelegateEvent<TEvent, TElement>) => void;

export type DelegateEvent<
TEvent extends Event = Event,
TElement extends Element = Element,
> = TEvent & {
delegateTarget: TElement;
};

/** Keeps track of raw listeners added to the base elements to avoid duplication */
const ledger = new WeakMap<
EventTarget,
WeakMap<delegate.EventHandler, Set<string>>
WeakMap<DelegateEventHandler, Set<string>>
>();

function editLedger(
wanted: boolean,
baseElement: EventTarget | Document,
callback: delegate.EventHandler<any, any>,
callback: DelegateEventHandler<any, any>,
setup: string,
): boolean {
if (!wanted && !ledger.has(baseElement)) {
Expand All @@ -36,7 +33,7 @@ function editLedger(

const elementMap
= ledger.get(baseElement)
?? new WeakMap<delegate.EventHandler, Set<string>>();
?? new WeakMap<DelegateEventHandler, Set<string>>();
ledger.set(baseElement, elementMap);

if (!wanted && !ledger.has(baseElement)) {
Expand Down Expand Up @@ -89,7 +86,7 @@ function delegate<
base: EventTarget | Document | ArrayLike<Element> | string,
selector: Selector,
type: TEventType,
callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
callback: DelegateEventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: DelegateOptions
): AbortController;

Expand All @@ -100,7 +97,7 @@ function delegate<
base: EventTarget | Document | ArrayLike<Element> | string,
selector: string,
type: TEventType,
callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
callback: DelegateEventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: DelegateOptions
): AbortController;

Expand All @@ -112,7 +109,7 @@ function delegate<
base: EventTarget | Document | ArrayLike<Element> | string,
selector: string,
type: TEventType,
callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
callback: DelegateEventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: DelegateOptions,
): AbortController {
const internalController = new AbortController();
Expand Down Expand Up @@ -152,11 +149,11 @@ function delegate<

// Handle the regular Element usage
const capture = Boolean(typeof options === 'object' ? options.capture : options);
const listenerFn: EventListener = (event: Event): void => {
const listenerFn = (event: Event): void => {
const delegateTarget = safeClosest(event, selector);
if (delegateTarget) {
(event as any).delegateTarget = delegateTarget;
callback.call(baseElement, event as delegate.Event<GlobalEventHandlersEventMap[TEventType], TElement>);
const delegateEvent = Object.assign(event, {delegateTarget});
callback.call(baseElement, delegateEvent as DelegateEvent<GlobalEventHandlersEventMap[TEventType], TElement>);
}
};

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
},
"devDependencies": {
"@sindresorhus/tsconfig": "^2.0.0",
"ava": "^4.2.0",
"ava": "^4.3.0",
"jsdom": "^20.0.0",
"npm-run-all": "^4.1.5",
"sinon": "^14.0.0",
"typescript": "^4.6.4",
"xo": "^0.48.0"
"typescript": "^4.7.4",
"xo": "^0.50.0"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
This is a fork of the popular [`delegate`](https://github.com/zenorocha/delegate) with some improvements:

- modern: ES6, TypeScript, Edge 15+ (it uses `WeakMap` and `Element.closest()`)
- modern: ES2021, TypeScript, Edge 16+ (it uses `WeakMap` and `Element.closest()`)
- idempotent: identical listeners aren't added multiple times, just like the native `addEventListener`
- debugged ([2d54c11](https://github.com/fregante/delegate-it/commit/2d54c1182aefd3ec9d8250fda76290971f5d7166), [c6bb88c](https://github.com/fregante/delegate-it/commit/c6bb88c2aa8097b25f22993a237cf09c96bcbfb8))
- supports [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
Expand Down
14 changes: 7 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {JSDOM} from 'jsdom';
import delegate from './index.js';

const {window} = new JSDOM(`
<ul>
<li><a>Item 1</a></li>
<li><a>Item 2</a></li>
<li><a>Item 3</a></li>
<li><a>Item 4</a></li>
<li><a>Item 5</a></li>
</ul>
<ul>
<li><a>Item 1</a></li>
<li><a>Item 2</a></li>
<li><a>Item 3</a></li>
<li><a>Item 4</a></li>
<li><a>Item 5</a></li>
</ul>
`);

global.Text = window.Text;
Expand Down
5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"target": "es2015",
"lib": [
"DOM"
]
"target": "es2021"
},
"files": [
"index.ts"
Expand Down

0 comments on commit 6ac66e2

Please sign in to comment.