Skip to content

Commit 40e10bb

Browse files
ortasaschanaz
andauthored
Bring back the EventListenerOrEventListenerObject and EventListener as an extendable interface (#110)
* Bring back the EventListenerOrEventListenerObject and EventListener as an extendable interface * Merge main * Use th existing build infra to add the eventlistener * Docs * Eslint fix * No circular reference * Remove call signature code * Final removal * Remove the hack for EventListenerOrEventListenerObject * Update src/build.ts Co-authored-by: Kagami Sascha Rosylight <[email protected]> * User override types for EventTarget * Update src/build/emitter.ts Co-authored-by: Kagami Sascha Rosylight <[email protected]>
1 parent 5288d88 commit 40e10bb

13 files changed

+768
-625
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,17 @@ Not yet. A small part of things still needs manual modification, which is done b
6767
```sh
6868
npm test
6969
```
70+
71+
* For larger changes, create a split terminal with:
72+
73+
```sh
74+
npx tsc --watch
75+
```
76+
77+
Running in the background updating your TypeScript files. Then run:
78+
79+
```sh
80+
node ./lib/build.js && node ./lib/test.js
81+
```
82+
83+
To trigger the build and tests.

baselines/audioworklet.generated.d.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ interface UnderlyingSource<R = any> {
109109
type?: undefined;
110110
}
111111

112-
type EventListener = ((event: Event) => void) | { handleEvent(event: Event): void; };
113-
114112
interface AudioWorkletGlobalScope extends WorkletGlobalScope {
115113
readonly currentFrame: number;
116114
readonly currentTime: number;
@@ -234,6 +232,14 @@ declare var Event: {
234232
readonly NONE: number;
235233
};
236234

235+
interface EventListener {
236+
(evt: Event): void;
237+
}
238+
239+
interface EventListenerObject {
240+
handleEvent(object: Event): void;
241+
}
242+
237243
/** EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. */
238244
interface EventTarget {
239245
/**
@@ -251,15 +257,15 @@ interface EventTarget {
251257
*
252258
* The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
253259
*/
254-
addEventListener(type: string, callback: EventListener | null, options?: AddEventListenerOptions | boolean): void;
260+
addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: AddEventListenerOptions | boolean): void;
255261
/**
256262
* Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
257263
*/
258264
dispatchEvent(event: Event): boolean;
259265
/**
260266
* Removes the event listener in target's event listener list with the same type, callback, and options.
261267
*/
262-
removeEventListener(type: string, callback: EventListener | null, options?: EventListenerOptions | boolean): void;
268+
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
263269
}
264270

265271
declare var EventTarget: {
@@ -323,9 +329,9 @@ interface MessagePort extends EventTarget {
323329
*/
324330
start(): void;
325331
addEventListener<K extends keyof MessagePortEventMap>(type: K, listener: (this: MessagePort, ev: MessagePortEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
326-
addEventListener(type: string, listener: EventListener, options?: boolean | AddEventListenerOptions): void;
332+
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
327333
removeEventListener<K extends keyof MessagePortEventMap>(type: K, listener: (this: MessagePort, ev: MessagePortEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
328-
removeEventListener(type: string, listener: EventListener, options?: boolean | EventListenerOptions): void;
334+
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
329335
}
330336

331337
declare var MessagePort: {
@@ -648,6 +654,7 @@ declare var sampleRate: number;
648654
declare function registerProcessor(name: string, processorCtor: AudioWorkletProcessorConstructor): void;
649655
type BufferSource = ArrayBufferView | ArrayBuffer;
650656
type DOMHighResTimeStamp = number;
657+
type EventListenerOrEventListenerObject = EventListener | EventListenerObject;
651658
type MessageEventSource = MessagePort;
652659
type ReadableStreamController<T> = ReadableStreamDefaultController<T>;
653660
type ReadableStreamDefaultReadResult<T> = ReadableStreamDefaultReadValueResult<T> | ReadableStreamDefaultReadDoneResult;

baselines/dom.generated.d.ts

Lines changed: 437 additions & 430 deletions
Large diffs are not rendered by default.

baselines/serviceworker.generated.d.ts

Lines changed: 53 additions & 46 deletions
Large diffs are not rendered by default.

baselines/sharedworker.generated.d.ts

Lines changed: 61 additions & 54 deletions
Large diffs are not rendered by default.

baselines/webworker.generated.d.ts

Lines changed: 65 additions & 58 deletions
Large diffs are not rendered by default.

inputfiles/addedTypes.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,41 @@
557557
}
558558
}
559559
},
560+
/*
561+
Keeping EventListener and EventListenerObject isn't the most elegant way to handle
562+
the event listeners, but we need to keep the EventListener as an extendable interface
563+
for libraries like angular.
564+
*/
565+
"EventListener": {
566+
"name": "EventListener",
567+
"noInterfaceObject": true,
568+
"methods": {
569+
"method": {
570+
// This is a hack to add a call signature, but I think it's reasonable
571+
// as it means we don't have to add a call signatures section to the
572+
// emitter for this one case.
573+
"callable": {
574+
"overrideSignatures": [
575+
"(evt: Event): void"
576+
]
577+
}
578+
}
579+
},
580+
581+
},
582+
"EventListenerObject": {
583+
"name": "EventListenerObject",
584+
"noInterfaceObject": true,
585+
"methods": {
586+
"method": {
587+
"handleEvent": {
588+
"overrideSignatures": [
589+
"handleEvent(object: Event): void"
590+
]
591+
}
592+
}
593+
},
594+
},
560595
"Document": {
561596
"methods": {
562597
"method": {
@@ -1154,6 +1189,10 @@
11541189
}
11551190
],
11561191
"overrideType": "ReadableStreamDefaultReader<T>"
1192+
},
1193+
{
1194+
"name": "EventListenerOrEventListenerObject",
1195+
"overrideType": "EventListener | EventListenerObject"
11571196
}
11581197
]
11591198
}

inputfiles/knownTypes.json

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
"ComputedKeyframe",
1414
"CryptoKeyPair",
1515
"DisplayCaptureSurfaceType",
16+
"EcdhKeyDeriveParams",
17+
"EcdsaParams",
1618
"EcKeyAlgorithm",
1719
"EcKeyGenParams",
1820
"EcKeyImportParams",
19-
"EcdhKeyDeriveParams",
20-
"EcdsaParams",
21+
"EventListenerOrEventListenerObject",
2122
"HashAlgorithmIdentifier",
2223
"HkdfParams",
2324
"HmacImportParams",
@@ -30,6 +31,13 @@
3031
"NamedCurve",
3132
"Pbkdf2Params",
3233
"PropertyIndexedKeyframes",
34+
"RsaHashedImportParams",
35+
"RsaHashedKeyAlgorithm",
36+
"RsaHashedKeyGenParams",
37+
"RsaKeyAlgorithm",
38+
"RsaKeyGenParams",
39+
"RsaOaepParams",
40+
"RsaPssParams",
3341
"RTCCertificateExpiration",
3442
"RTCIceCandidatePairStats",
3543
"RTCInboundRtpStreamStats",
@@ -41,13 +49,6 @@
4149
"RTCStatsIceCandidatePairState",
4250
"RTCStatsType",
4351
"RTCTransportStats",
44-
"RsaHashedImportParams",
45-
"RsaHashedKeyAlgorithm",
46-
"RsaHashedKeyGenParams",
47-
"RsaKeyAlgorithm",
48-
"RsaKeyGenParams",
49-
"RsaOaepParams",
50-
"RsaPssParams",
5152
"Transferable",
5253
"VideoFacingModeEnum"
5354
],
@@ -66,6 +67,7 @@
6667
"EcdsaParams",
6768
"EcKeyGenParams",
6869
"EcKeyImportParams",
70+
"EventListenerOrEventListenerObject",
6971
"HashAlgorithmIdentifier",
7072
"HkdfParams",
7173
"HmacImportParams",
@@ -77,11 +79,12 @@
7779
"RsaHashedImportParams",
7880
"RsaHashedKeyGenParams",
7981
"RsaKeyGenParams",
80-
"RsaPssParams",
8182
"RsaOaepParams",
83+
"RsaPssParams",
8284
"Transferable"
8385
],
8486
"Worklet": [
87+
"EventListenerOrEventListenerObject",
8588
"Transferable"
8689
]
8790
}

inputfiles/overridingTypes.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,36 @@
26592659
}
26602660
}
26612661
},
2662+
"EventTarget": {
2663+
"methods": {
2664+
"method": {
2665+
"addEventListener": {
2666+
"signature": {
2667+
"0": {
2668+
"param": [
2669+
{
2670+
"name": "callback",
2671+
"overrideType": "EventListenerOrEventListenerObject"
2672+
}
2673+
]
2674+
}
2675+
}
2676+
},
2677+
"removeEventListener": {
2678+
"signature": {
2679+
"0": {
2680+
"param": [
2681+
{
2682+
"name": "callback",
2683+
"overrideType": "EventListenerOrEventListenerObject"
2684+
}
2685+
]
2686+
}
2687+
}
2688+
}
2689+
}
2690+
}
2691+
},
26622692
"WorkerGlobalScope": {
26632693
"properties": {
26642694
"property": {

package-lock.json

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)