-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
- Loading branch information
Showing
11 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
indent_style = tab | ||
|
||
[*.yml] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: CI | ||
on: | ||
- push | ||
- pull_request | ||
jobs: | ||
test: | ||
name: Node.js ${{ matrix.node-version }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
node-version: | ||
- 18 | ||
- 16 | ||
- 14 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm install | ||
- run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
package-lock.json | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type {ValueOf} from 'type-fest'; | ||
|
||
type EventListenerOrEventListenerObject<Value extends Event | CustomEvent> = (value: Value) => void | { | ||
handleEvent(value: Value): void; | ||
}; | ||
|
||
/** | ||
Strictly typed definitions for [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) | ||
Only used for type-checking; compiles to no code. | ||
@example | ||
``` | ||
import type StrictEventTarget from 'strict-event-target'; | ||
const target = new (EventTarget as typeof StrictEventTarget<{ | ||
foo: string; // Event with data | ||
}, [ | ||
'bar', // Event without data | ||
]>); | ||
target.dispatchEvent(new CustomEvent('foo', {detail: 'The'})); | ||
target.dispatchEvent(new Event('bar')); | ||
``` | ||
@example | ||
``` | ||
import type StrictEventTarget from 'strict-event-target'; | ||
class Foo extends (EventTarget as typeof StrictEventTarget<{ | ||
foo: string; // Event with data | ||
}, [ | ||
'bar', // Event without data | ||
]>) {} | ||
``` | ||
*/ | ||
export default class StrictEventTarget<EventsWithData extends Record<string, unknown> = {}, EventsWithoutData extends string[] = []> implements EventTarget { // eslint-disable-line @typescript-eslint/ban-types | ||
addEventListener<Name extends keyof EventsWithData>(name: Name, callback: EventListenerOrEventListenerObject<CustomEvent<EventsWithData[Name]>> | null, options?: AddEventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types | ||
addEventListener<Name extends ValueOf<EventsWithoutData>>(name: Name, callback: EventListenerOrEventListenerObject<Event> | null, options?: AddEventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types | ||
|
||
dispatchEvent(event: Event | CustomEvent<ValueOf<EventsWithData>>): boolean; | ||
|
||
removeEventListener<Name extends keyof EventsWithData>(name: Name, callback: EventListenerOrEventListenerObject<CustomEvent<EventsWithData[Name]>> | null, options?: EventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types | ||
removeEventListener<Name extends ValueOf<EventsWithoutData>>(name: Name, callback: EventListenerOrEventListenerObject<Event> | null, options?: EventListenerOptions | boolean): void; // eslint-disable-line @typescript-eslint/ban-types | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type StrictEventTarget from './index.js'; | ||
|
||
type EventsWithData = { | ||
a: string; | ||
}; | ||
|
||
type EventsWithoutData = ['b']; | ||
|
||
const target: StrictEventTarget<EventsWithData, EventsWithoutData> = new (EventTarget as typeof StrictEventTarget<EventsWithData, EventsWithoutData>)(); | ||
|
||
function aCallback(_event: CustomEvent<string>) {} // eslint-disable-line @typescript-eslint/no-empty-function | ||
function bCallback(_event: Event) {} // eslint-disable-line @typescript-eslint/no-empty-function | ||
|
||
target.addEventListener('a', aCallback); | ||
target.addEventListener('b', bCallback); | ||
|
||
target.dispatchEvent(new CustomEvent('a', {detail: 'foo'})); | ||
target.dispatchEvent(new Event('b')); | ||
|
||
target.removeEventListener('a', aCallback); | ||
target.removeEventListener('b', bCallback); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Richie Bendall | ||
|
||
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 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "strict-event-target", | ||
"version": "0.0.0", | ||
"description": "Strictly typed definitions for EventTarget", | ||
"license": "MIT", | ||
"repository": "Richienb/strict-event-target", | ||
"author": { | ||
"name": "Richie Bendall", | ||
"email": "richiebendall@gmail.com" | ||
}, | ||
"type": "module", | ||
"types": "index.d.ts", | ||
"engines": { | ||
"node": ">=14" | ||
}, | ||
"scripts": { | ||
"test": "xo && tsd" | ||
}, | ||
"files": [ | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"EventTarget", | ||
"TypeScript" | ||
], | ||
"dependencies": { | ||
"type-fest": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^3.0.1", | ||
"tsd": "^0.24.1", | ||
"typescript": "^4.8.3", | ||
"xo": "^0.52.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# strict-event-target | ||
|
||
> Strictly typed definitions for [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) | ||
Only used for type-checking; compiles to no code. | ||
|
||
## Install | ||
|
||
```sh | ||
npm install strict-event-target | ||
``` | ||
|
||
## Usage | ||
|
||
```ts | ||
import type StrictEventTarget from 'strict-event-target'; | ||
|
||
const target = new (EventTarget as typeof StrictEventTarget<{ | ||
foo: string; // Event with data | ||
}, [ | ||
'bar', // Event without data | ||
]>); | ||
|
||
target.dispatchEvent(new CustomEvent('foo', {detail: 'The'})); | ||
|
||
target.dispatchEvent(new Event('bar')); | ||
``` | ||
|
||
```ts | ||
import type StrictEventTarget from 'strict-event-target'; | ||
|
||
class Foo extends (EventTarget as typeof StrictEventTarget<{ | ||
foo: string; // Event with data | ||
}, [ | ||
'bar', // Event without data | ||
]>) {} | ||
``` | ||
|
||
## API | ||
|
||
### StrictEventTarget<EventsWithData?, EventsWithoutData?> | ||
|
||
#### EventsWithData | ||
|
||
Type: `Record<string, unknown>`\ | ||
Default: `{}` | ||
|
||
Events that will be created with [`CustomEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent), thus being able to contain custom data. | ||
|
||
#### EventsWithoutData | ||
|
||
Type: `string[]`\ | ||
Default: `[]` | ||
|
||
Events that will be created with [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@sindresorhus/tsconfig" | ||
} |