-
Notifications
You must be signed in to change notification settings - Fork 2
/
event-emitter.ts
106 lines (100 loc) · 2.94 KB
/
event-emitter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import type { LitElement } from 'lit';
import type { AbstractConstructor, Constructor } from './constructor';
export type UnpackCustomEvent<T> = T extends CustomEvent<infer U> ? U : never;
export declare class EventEmitterInterface<E> {
public addEventListener<K extends keyof M, M extends E & HTMLElementEventMap>(
type: K,
listener: (this: HTMLElement, ev: M[K]) => any,
options?: boolean | AddEventListenerOptions
): void;
public addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions
): void;
public removeEventListener<
K extends keyof M,
M extends E & HTMLElementEventMap,
>(
type: K,
listener: (this: HTMLElement, ev: M[K]) => any,
options?: boolean | EventListenerOptions
): void;
public removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions
): void;
public emitEvent<K extends keyof E, D extends UnpackCustomEvent<E[K]>>(
type: K,
eventInitDict?: CustomEventInit<D>
): boolean;
}
export function EventEmitterMixin<E, T extends AbstractConstructor<LitElement>>(
superClass: T
): Constructor<EventEmitterInterface<E>> & T;
export function EventEmitterMixin<E, T extends Constructor<LitElement>>(
superClass: T
) {
class EventEmitterElement extends superClass {
/**
* @private
*/
public override addEventListener<
K extends keyof M,
M extends E & HTMLElementEventMap,
>(
type: K,
listener: (this: HTMLElement, ev: M[K]) => any,
options?: boolean | AddEventListenerOptions
): void;
public override addEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions
): void {
super.addEventListener(type, listener, options);
}
/**
* @private
*/
public override removeEventListener<
K extends keyof M,
M extends E & HTMLElementEventMap,
>(
type: K,
listener: (this: HTMLElement, ev: M[K]) => any,
options?: boolean | EventListenerOptions
): void;
public override removeEventListener(
type: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | EventListenerOptions
): void {
super.removeEventListener(type, listener, options);
}
/**
* @private
*/
public emitEvent<K extends keyof E, D extends UnpackCustomEvent<E[K]>>(
type: K,
eventInitDict?: CustomEventInit<D>
): boolean {
return this.dispatchEvent(
new CustomEvent<D>(
type as string,
Object.assign(
{
bubbles: true,
cancelable: false,
composed: true,
detail: {},
},
eventInitDict
)
)
);
}
}
return EventEmitterElement as Constructor<EventEmitterInterface<E>> & T;
}