Skip to content

Commit

Permalink
Update dev dependencies + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed May 13, 2022
1 parent 0081742 commit 9bbdb15
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
43 changes: 20 additions & 23 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 All @@ -12,12 +11,12 @@ namespace delegate {

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

export type Event<
TEvent extends GlobalEvent = GlobalEvent,
TElement extends Element = Element
TElement extends Element = Element,
> = TEvent & {
delegateTarget: TElement;
};
Expand All @@ -33,15 +32,15 @@ function editLedger(
wanted: boolean,
baseElement: EventTarget | Document,
callback: delegate.EventHandler<any, any>,
setup: string
setup: string,
): boolean {
if (!wanted && !ledger.has(baseElement)) {
return false;
}

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

if (!wanted && !ledger.has(baseElement)) {
Expand All @@ -62,7 +61,7 @@ function editLedger(
}

function isEventTarget(
elements: EventTarget | Document | ArrayLike<Element> | string
elements: EventTarget | Document | ArrayLike<Element> | string,
): elements is EventTarget {
return typeof (elements as EventTarget).addEventListener === 'function';
}
Expand All @@ -89,7 +88,7 @@ function safeClosest(event: Event, selector: string): Element | void {
function delegate<
Selector extends string,
TElement extends Element = ParseSelector<Selector, HTMLElement>,
TEventType extends EventType = EventType
TEventType extends EventType = EventType,
>(
base: EventTarget | Document | ArrayLike<Element> | string,
selector: Selector,
Expand All @@ -100,7 +99,7 @@ function delegate<

function delegate<
TElement extends Element = HTMLElement,
TEventType extends EventType = EventType
TEventType extends EventType = EventType,
>(
base: EventTarget | Document | ArrayLike<Element> | string,
selector: string,
Expand All @@ -112,13 +111,13 @@ function delegate<
// This type isn't exported as a declaration, so it needs to be duplicated above
function delegate<
TElement extends Element,
TEventType extends EventType = EventType
TEventType extends EventType = EventType,
>(
base: EventTarget | Document | ArrayLike<Element> | string,
selector: string,
type: TEventType,
callback: delegate.EventHandler<GlobalEventHandlersEventMap[TEventType], TElement>,
options?: DelegateOptions
options?: DelegateOptions,
): delegate.Subscription {
// Handle Selector-based usage
if (typeof base === 'string') {
Expand All @@ -129,23 +128,21 @@ function delegate<
if (!isEventTarget(base)) {
const subscriptions = Array.prototype.map.call(
base,
(element: EventTarget) => {
return delegate(
element,
selector,
type,
callback,
options
);
}
(element: EventTarget) => delegate(
element,
selector,
type,
callback,
options,
),
) as delegate.Subscription[];

return {
destroy(): void {
for (const subscription of subscriptions) {
subscription.destroy();
}
}
},
};
}

Expand Down Expand Up @@ -173,7 +170,7 @@ function delegate<
destroy() {
baseElement.removeEventListener(type, listenerFn, options);
editLedger(false, baseElement, callback, setup);
}
},
};

if (!isAlreadyListening) {
Expand Down
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,21 @@
"browser"
],
"rules": {
"import/extensions": "off",
"import/no-useless-path-segments": "off",
"max-params": "off",
"unicorn/import-index": "off"
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/naming-convention": "off"
}
},
"dependencies": {
"typed-query-selector": "^2.4.1"
"typed-query-selector": "^2.6.1"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^1.0.1",
"ava": "^3.15.0",
"jsdom": "^16.5.2",
"@sindresorhus/tsconfig": "^2.0.0",
"ava": "^4.2.0",
"jsdom": "^19.0.0",
"npm-run-all": "^4.1.5",
"sinon": "^10.0.0",
"typescript": "^4.2.4",
"xo": "^0.38.2"
"sinon": "^14.0.0",
"typescript": "^4.6.4",
"xo": "^0.48.0"
}
}
13 changes: 3 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import test from 'ava';
import sinon from 'sinon';
import {createRequire} from 'module';
import {JSDOM} from 'jsdom';
import delegate from './index.js';

const require = createRequire(import.meta.url);
export const {JSDOM} = require('jsdom');

const {window} = new JSDOM(`
<ul>
<li><a>Item 1</a></li>
Expand Down Expand Up @@ -75,9 +72,7 @@ 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 items = document.querySelectorAll('li');
const spies = Array.prototype.map.call(items, li => {
return sinon.spy(li, 'removeEventListener');
});
const spies = Array.prototype.map.call(items, li => sinon.spy(li, 'removeEventListener'));

const delegation = delegate('li', 'a', 'click', () => {});
delegation.destroy();
Expand All @@ -102,9 +97,7 @@ 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 items = document.querySelectorAll('li');
const spies = Array.prototype.map.call(items, li => {
return sinon.spy(li, 'removeEventListener');
});
const spies = Array.prototype.map.call(items, li => sinon.spy(li, 'removeEventListener'));

const delegation = delegate(items, 'a', 'click', () => {});
delegation.destroy();
Expand Down

0 comments on commit 9bbdb15

Please sign in to comment.