From 46a07f71e9664bf725cde40da1d0f27f6ddaf91d Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Mon, 8 Mar 2021 02:45:41 +0100 Subject: [PATCH 1/5] Improve type inference for the listener callback (#19) --- index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.ts b/index.ts index 6de01a9..48b9aeb 100644 --- a/index.ts +++ b/index.ts @@ -83,12 +83,12 @@ function safeClosest(event: Event, selector: string): Element | void { */ function delegate< TElement extends Element = Element, - TEvent extends Event = Event + TEventType extends EventType = EventType >( base: EventTarget | Document | ArrayLike | string, selector: string, - type: EventType, - callback: delegate.EventHandler, + type: TEventType, + callback: delegate.EventHandler, options?: boolean | AddEventListenerOptions ): delegate.Subscription { // Handle Selector-based usage @@ -101,7 +101,7 @@ function delegate< const subscriptions = Array.prototype.map.call( base, (element: EventTarget) => { - return delegate( + return delegate( element, selector, type, @@ -127,7 +127,7 @@ function delegate< const delegateTarget = safeClosest(event, selector); if (delegateTarget) { (event as any).delegateTarget = delegateTarget; - callback.call(baseElement, event as delegate.Event); + callback.call(baseElement, event as delegate.Event); } }; From ce9fe394d6e6f638c7525822765b2355bc33803b Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Tue, 16 Mar 2021 22:02:13 -0600 Subject: [PATCH 2/5] Meta and lint --- index.ts | 4 +++- license | 22 +++++----------------- package.json | 12 ++++++------ 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/index.ts b/index.ts index 48b9aeb..58cb811 100644 --- a/index.ts +++ b/index.ts @@ -113,7 +113,9 @@ function delegate< return { destroy(): void { - subscriptions.forEach(subscription => subscription.destroy()); + for (const subscription of subscriptions) { + subscription.destroy(); + } } }; } diff --git a/license b/license index 7ebf4d0..e342b5c 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -The MIT License (MIT) +MIT License -Copyright (c) Federico Brigante (bfred.it) +Copyright (c) Federico Brigante (https://fregante.com) -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json index ac5bc7f..9dc1521 100644 --- a/package.json +++ b/package.json @@ -36,12 +36,12 @@ } }, "devDependencies": { - "@sindresorhus/tsconfig": "^0.7.0", - "ava": "^3.12.1", - "jsdom": "^16.4.0", + "@sindresorhus/tsconfig": "^0.9.0", + "ava": "^3.15.0", + "jsdom": "^16.5.1", "npm-run-all": "^4.1.5", - "sinon": "^9.0.3", - "typescript": "^4.0.2", - "xo": "^0.34.2" + "sinon": "^9.2.4", + "typescript": "^4.2.3", + "xo": "^0.38.2" } } From cceddeb7440706dc6294c6c204cc83d8460ca698 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sat, 10 Apr 2021 18:03:40 +0700 Subject: [PATCH 3/5] Make it more strongly-typed when used with TypeScript (#25) --- index.ts | 30 ++++++++++++++++++++++++++++-- package.json | 5 ++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index 58cb811..05e9cd6 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,5 @@ +import type {ParseSelector} from 'typed-query-selector/parser'; + export type EventType = keyof GlobalEventHandlersEventMap; type GlobalEvent = Event; @@ -82,7 +84,31 @@ function safeClosest(event: Event, selector: string): Element | void { * Delegates event to a selector. */ function delegate< - TElement extends Element = Element, + Selector extends string, + TElement extends Element = ParseSelector, + TEventType extends EventType = EventType +>( + base: EventTarget | Document | ArrayLike | string, + selector: Selector, + type: TEventType, + callback: delegate.EventHandler, + options?: boolean | AddEventListenerOptions +): delegate.Subscription; + +function delegate< + TElement extends Element = HTMLElement, + TEventType extends EventType = EventType +>( + base: EventTarget | Document | ArrayLike | string, + selector: string, + type: TEventType, + callback: delegate.EventHandler, + options?: boolean | AddEventListenerOptions +): delegate.Subscription; + +// This type isn't exported as a declaration, so it needs to be duplicated above +function delegate< + TElement extends Element, TEventType extends EventType = EventType >( base: EventTarget | Document | ArrayLike | string, @@ -101,7 +127,7 @@ function delegate< const subscriptions = Array.prototype.map.call( base, (element: EventTarget) => { - return delegate( + return delegate( element, selector, type, diff --git a/package.json b/package.json index 9dc1521..25b310c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "delegate-it", - "version": "2.0.2", + "version": "3.0.0-1", "description": "Lightweight event delegation", "keywords": [ "delegate", @@ -43,5 +43,8 @@ "sinon": "^9.2.4", "typescript": "^4.2.3", "xo": "^0.38.2" + }, + "dependencies": { + "typed-query-selector": "^2.4.1" } } From ecd2d2df99feafb78ea468f087c525ab4426d099 Mon Sep 17 00:00:00 2001 From: Federico Date: Sat, 10 Apr 2021 19:07:17 +0700 Subject: [PATCH 4/5] Dev dependency updates and meta fixes --- package.json | 25 +++++++++++++++++-------- tsconfig.json | 4 +++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 25b310c..3527bd4 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,23 @@ { "name": "delegate-it", "version": "3.0.0-1", - "description": "Lightweight event delegation", + "description": "Lightweight and modern event delegation in the browser", "keywords": [ "delegate", + "browser", + "dom", + "live", + "selector", "delegation", + "chrome", + "electron", + "firefox", + "safari", "event" ], "repository": "fregante/delegate-it", "license": "MIT", + "author": "Federico Brigante (https://fregante.com)", "type": "module", "main": "index.js", "module": "index.js", @@ -35,16 +44,16 @@ "unicorn/import-index": "off" } }, + "dependencies": { + "typed-query-selector": "^2.4.1" + }, "devDependencies": { - "@sindresorhus/tsconfig": "^0.9.0", + "@sindresorhus/tsconfig": "^1.0.1", "ava": "^3.15.0", - "jsdom": "^16.5.1", + "jsdom": "^16.5.2", "npm-run-all": "^4.1.5", - "sinon": "^9.2.4", - "typescript": "^4.2.3", + "sinon": "^10.0.0", + "typescript": "^4.2.4", "xo": "^0.38.2" - }, - "dependencies": { - "typed-query-selector": "^2.4.1" } } diff --git a/tsconfig.json b/tsconfig.json index 55a66bf..8fc0bbe 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,9 @@ "extends": "@sindresorhus/tsconfig", "compilerOptions": { "target": "es2015", - "module": "es2015" + "lib": [ + "DOM" + ] }, "files": [ "index.ts" From ac02ad015c5051abbd71302acf11015b3a5c526a Mon Sep 17 00:00:00 2001 From: Federico Date: Sat, 10 Apr 2021 19:08:31 +0700 Subject: [PATCH 5/5] 3.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3527bd4..f224ad6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "delegate-it", - "version": "3.0.0-1", + "version": "3.0.0", "description": "Lightweight and modern event delegation in the browser", "keywords": [ "delegate",