Skip to content

Commit 6f762db

Browse files
authored
Create custom_event_target.ts
1 parent dd13a85 commit 6f762db

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

src/custom_event_target.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* A function that handles a {@link CustomEvent} with a specific detail type.
3+
*
4+
* @template Detail Type of the `detail` property in the CustomEvent
5+
*/
6+
export type CustomEventListener<Detail = unknown> =
7+
(event : CustomEvent<Detail>) => void;
8+
9+
/**
10+
* An object that can handle {@link CustomEvent} events via its `handleEvent` method.
11+
*
12+
* @template Detail Type of the `detail` property in the CustomEvent
13+
*/
14+
export interface CustomEventListenerObject<Detail = unknown> {
15+
/**
16+
* Called when an event of the specified type is dispatched.
17+
*
18+
* @param event Custom event being handled
19+
*/
20+
handleEvent(event : CustomEvent<Detail>) : void;
21+
}
22+
23+
/**
24+
* Union type that represents either a {@link CustomEventListener} function or a {@link CustomEventListenerObject}
25+
* object.
26+
*
27+
* @template Detail Type of the `detail` property in the CustomEvent
28+
*/
29+
export type CustomEventListenerOrCustomEventListenerObject<Detail = unknown> =
30+
| CustomEventListener<Detail>
31+
| CustomEventListenerObject<Detail>
32+
33+
/**
34+
* An extended version of `EventTarget` that expects listeners to receive {@link CustomEvent} instances
35+
* (instead of plain `Event`).
36+
*
37+
* This interface is mainly useful for better type safety when working exclusively with CustomEvent in your application.
38+
*
39+
* @see {@link CustomEventTarget} the constructor/cast helper
40+
*/
41+
export interface CustomEventTarget<EventType extends string = string> extends EventTarget {
42+
/**
43+
* Registers an event handler of a specific event type.
44+
*
45+
* @param type String representing the event type to listen for
46+
* @param listener Object or function that receives a notification
47+
* @param options Object specifying characteristics about the event listener, or a boolean indicating whether the
48+
* listener should be passive
49+
*/
50+
addEventListener(
51+
type : EventType,
52+
listener : CustomEventListenerOrCustomEventListenerObject | null,
53+
options? : boolean | AddEventListenerOptions
54+
) : void;
55+
56+
/**
57+
* Removes an event listener previously registered with addEventListener.
58+
*
59+
* @param type String that specifies the event type of the listener to remove
60+
* @param listener Event listener handler to remove
61+
* @param options Object that specifies characteristics about the event listener to remove
62+
*/
63+
removeEventListener(
64+
type : EventType,
65+
listener : CustomEventListenerOrCustomEventListenerObject | null,
66+
options? : boolean | EventListenerOptions
67+
) : void;
68+
69+
dispatchEvent(event : CustomEvent) : boolean;
70+
}
71+
72+
/**
73+
* A constructor cast that allows creating instances with the {@link CustomEventTarget} interface.
74+
*
75+
* @example
76+
* ```ts
77+
* class MyClass extends CustomEventTarget { ... }
78+
* ```
79+
* @example
80+
* ```ts
81+
* const target = new CustomEventTarget();
82+
* ```
83+
*/
84+
export class CustomEventTarget extends EventTarget implements CustomEventTarget {}

0 commit comments

Comments
 (0)