From 3280091023609a1e845e79852c08cef3baf95752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Thu, 25 Jul 2024 19:26:11 +0200 Subject: [PATCH] Fix TS Observers type ### Details - Export `WorkerObserver`, `ProducerObserver` types. - Many changes done because things were wrong at TS level despite it didn't show up (things showed up to me while doing this). --- node/src/ActiveSpeakerObserver.ts | 15 ++++++++++++--- node/src/AudioLevelObserver.ts | 18 ++++++++++++++--- node/src/Consumer.ts | 7 +++++-- node/src/DataConsumer.ts | 8 ++++++-- node/src/DataProducer.ts | 8 ++++++-- node/src/DirectTransport.ts | 20 +++++++++++++++++-- node/src/PipeTransport.ts | 20 +++++++++++++++++-- node/src/PlainTransport.ts | 20 +++++++++++++++++-- node/src/Producer.ts | 7 +++++-- node/src/Router.ts | 7 +++++-- node/src/RtpObserver.ts | 26 ++++++++++++++++--------- node/src/Transport.ts | 32 ++++++++++++++++++------------- node/src/WebRtcServer.ts | 8 ++++++-- node/src/WebRtcTransport.ts | 20 +++++++++++++++++-- node/src/Worker.ts | 7 +++++-- node/src/index.ts | 4 +++- 16 files changed, 176 insertions(+), 51 deletions(-) diff --git a/node/src/ActiveSpeakerObserver.ts b/node/src/ActiveSpeakerObserver.ts index 88229731dc..9187175115 100644 --- a/node/src/ActiveSpeakerObserver.ts +++ b/node/src/ActiveSpeakerObserver.ts @@ -33,6 +33,9 @@ export type ActiveSpeakerObserverEvents = RtpObserverEvents & { dominantspeaker: [ActiveSpeakerObserverDominantSpeaker]; }; +export type ActiveSpeakerObserverObserver = + EnhancedEventEmitter; + export type ActiveSpeakerObserverObserverEvents = RtpObserverObserverEvents & { dominantspeaker: [ActiveSpeakerObserverDominantSpeaker]; }; @@ -46,7 +49,8 @@ export class ActiveSpeakerObserver< ActiveSpeakerObserverAppData extends AppData = AppData, > extends RtpObserver< ActiveSpeakerObserverAppData, - ActiveSpeakerObserverEvents + ActiveSpeakerObserverEvents, + ActiveSpeakerObserverObserver > { /** * @private @@ -54,15 +58,20 @@ export class ActiveSpeakerObserver< constructor( options: RtpObserverObserverConstructorOptions ) { - super(options); + const observer: ActiveSpeakerObserverObserver = + new EnhancedEventEmitter(); + + super(options, observer); this.handleWorkerNotifications(); } /** * Observer. + * + * @override */ - get observer(): EnhancedEventEmitter { + get observer(): ActiveSpeakerObserverObserver { return super.observer; } diff --git a/node/src/AudioLevelObserver.ts b/node/src/AudioLevelObserver.ts index f24e5d6b16..e0a91e408d 100644 --- a/node/src/AudioLevelObserver.ts +++ b/node/src/AudioLevelObserver.ts @@ -55,6 +55,9 @@ export type AudioLevelObserverEvents = RtpObserverEvents & { silence: []; }; +export type AudioLevelObserverObserver = + EnhancedEventEmitter; + export type AudioLevelObserverObserverEvents = RtpObserverObserverEvents & { volumes: [AudioLevelObserverVolume[]]; silence: []; @@ -67,22 +70,31 @@ const logger = new Logger('AudioLevelObserver'); export class AudioLevelObserver< AudioLevelObserverAppData extends AppData = AppData, -> extends RtpObserver { +> extends RtpObserver< + AudioLevelObserverAppData, + AudioLevelObserverEvents, + AudioLevelObserverObserver +> { /** * @private */ constructor( options: AudioLevelObserverConstructorOptions ) { - super(options); + const observer: AudioLevelObserverObserver = + new EnhancedEventEmitter(); + + super(options, observer); this.handleWorkerNotifications(); } /** * Observer. + * + * @override */ - get observer(): EnhancedEventEmitter { + get observer(): AudioLevelObserverObserver { return super.observer; } diff --git a/node/src/Consumer.ts b/node/src/Consumer.ts index 8ada889603..8de9fc4cff 100644 --- a/node/src/Consumer.ts +++ b/node/src/Consumer.ts @@ -179,6 +179,8 @@ export type ConsumerEvents = { '@producerclose': []; }; +export type ConsumerObserver = EnhancedEventEmitter; + export type ConsumerObserverEvents = { close: []; pause: []; @@ -318,7 +320,8 @@ export class Consumer< #currentLayers?: ConsumerLayers; // Observer instance. - readonly #observer = new EnhancedEventEmitter(); + readonly #observer: ConsumerObserver = + new EnhancedEventEmitter(); /** * @private @@ -459,7 +462,7 @@ export class Consumer< /** * Observer. */ - get observer(): EnhancedEventEmitter { + get observer(): ConsumerObserver { return this.#observer; } diff --git a/node/src/DataConsumer.ts b/node/src/DataConsumer.ts index d0ff2ace7c..8a987f143d 100644 --- a/node/src/DataConsumer.ts +++ b/node/src/DataConsumer.ts @@ -92,6 +92,9 @@ export type DataConsumerEvents = { '@dataproducerclose': []; }; +export type DataConsumerObserver = + EnhancedEventEmitter; + export type DataConsumerObserverEvents = { close: []; pause: []; @@ -148,7 +151,8 @@ export class DataConsumer< #appData: DataConsumerAppData; // Observer instance. - readonly #observer = new EnhancedEventEmitter(); + readonly #observer: DataConsumerObserver = + new EnhancedEventEmitter(); /** * @private @@ -272,7 +276,7 @@ export class DataConsumer< /** * Observer. */ - get observer(): EnhancedEventEmitter { + get observer(): DataConsumerObserver { return this.#observer; } diff --git a/node/src/DataProducer.ts b/node/src/DataProducer.ts index af7ef5c716..5a6ced5aa0 100644 --- a/node/src/DataProducer.ts +++ b/node/src/DataProducer.ts @@ -67,6 +67,9 @@ export type DataProducerEvents = { '@close': []; }; +export type DataProducerObserver = + EnhancedEventEmitter; + export type DataProducerObserverEvents = { close: []; pause: []; @@ -113,7 +116,8 @@ export class DataProducer< #appData: DataProducerAppData; // Observer instance. - readonly #observer = new EnhancedEventEmitter(); + readonly #observer: DataProducerObserver = + new EnhancedEventEmitter(); /** * @private @@ -210,7 +214,7 @@ export class DataProducer< /** * Observer. */ - get observer(): EnhancedEventEmitter { + get observer(): DataProducerObserver { return this.#observer; } diff --git a/node/src/DirectTransport.ts b/node/src/DirectTransport.ts index f37edd379b..3417e0bb46 100644 --- a/node/src/DirectTransport.ts +++ b/node/src/DirectTransport.ts @@ -1,4 +1,5 @@ import { Logger } from './Logger'; +import { EnhancedEventEmitter } from './enhancedEvents'; import { UnsupportedError } from './errors'; import { BaseTransportDump, @@ -44,6 +45,9 @@ export type DirectTransportEvents = TransportEvents & { rtcp: [Buffer]; }; +export type DirectTransportObserver = + EnhancedEventEmitter; + export type DirectTransportObserverEvents = TransportObserverEvents & { rtcp: [Buffer]; }; @@ -64,7 +68,7 @@ export class DirectTransport< > extends Transport< DirectTransportAppData, DirectTransportEvents, - DirectTransportObserverEvents + DirectTransportObserver > { // DirectTransport data. readonly #data: DirectTransportData; @@ -75,7 +79,10 @@ export class DirectTransport< constructor( options: DirectTransportConstructorOptions ) { - super(options); + const observer: DirectTransportObserver = + new EnhancedEventEmitter(); + + super(options, observer); logger.debug('constructor()'); @@ -86,6 +93,15 @@ export class DirectTransport< this.handleWorkerNotifications(); } + /** + * Observer. + * + * @override + */ + get observer(): DirectTransportObserver { + return super.observer; + } + /** * Close the DirectTransport. * diff --git a/node/src/PipeTransport.ts b/node/src/PipeTransport.ts index 3bd9eb0dd1..6fea0d56ad 100644 --- a/node/src/PipeTransport.ts +++ b/node/src/PipeTransport.ts @@ -1,5 +1,6 @@ import * as flatbuffers from 'flatbuffers'; import { Logger } from './Logger'; +import { EnhancedEventEmitter } from './enhancedEvents'; import * as ortc from './ortc'; import { BaseTransportDump, @@ -131,6 +132,9 @@ export type PipeTransportEvents = TransportEvents & { sctpstatechange: [SctpState]; }; +export type PipeTransportObserver = + EnhancedEventEmitter; + export type PipeTransportObserverEvents = TransportObserverEvents & { sctpstatechange: [SctpState]; }; @@ -161,7 +165,7 @@ export class PipeTransport< > extends Transport< PipeTransportAppData, PipeTransportEvents, - PipeTransportObserverEvents + PipeTransportObserver > { // PipeTransport data. readonly #data: PipeTransportData; @@ -170,7 +174,10 @@ export class PipeTransport< * @private */ constructor(options: PipeTransportConstructorOptions) { - super(options); + const observer: PipeTransportObserver = + new EnhancedEventEmitter(); + + super(options, observer); logger.debug('constructor()'); @@ -187,6 +194,15 @@ export class PipeTransport< this.handleWorkerNotifications(); } + /** + * Observer. + * + * @override + */ + get observer(): PipeTransportObserver { + return super.observer; + } + /** * Transport tuple. */ diff --git a/node/src/PlainTransport.ts b/node/src/PlainTransport.ts index 7d118207c0..c4202eec7e 100644 --- a/node/src/PlainTransport.ts +++ b/node/src/PlainTransport.ts @@ -1,5 +1,6 @@ import * as flatbuffers from 'flatbuffers'; import { Logger } from './Logger'; +import { EnhancedEventEmitter } from './enhancedEvents'; import { parseSctpState, BaseTransportDump, @@ -130,6 +131,9 @@ export type PlainTransportEvents = TransportEvents & { sctpstatechange: [SctpState]; }; +export type PlainTransportObserver = + EnhancedEventEmitter; + export type PlainTransportObserverEvents = TransportObserverEvents & { tuple: [TransportTuple]; rtcptuple: [TransportTuple]; @@ -166,7 +170,7 @@ export class PlainTransport< > extends Transport< PlainTransportAppData, PlainTransportEvents, - PlainTransportObserverEvents + PlainTransportObserver > { // PlainTransport data. readonly #data: PlainTransportData; @@ -177,7 +181,10 @@ export class PlainTransport< constructor( options: PlainTransportConstructorOptions ) { - super(options); + const observer: PlainTransportObserver = + new EnhancedEventEmitter(); + + super(options, observer); logger.debug('constructor()'); @@ -196,6 +203,15 @@ export class PlainTransport< this.handleWorkerNotifications(); } + /** + * Observer. + * + * @override + */ + get observer(): PlainTransportObserver { + return super.observer; + } + /** * Transport tuple. */ diff --git a/node/src/Producer.ts b/node/src/Producer.ts index afadbc8255..317091e0d8 100644 --- a/node/src/Producer.ts +++ b/node/src/Producer.ts @@ -140,6 +140,8 @@ export type ProducerEvents = { '@close': []; }; +export type ProducerObserver = EnhancedEventEmitter; + export type ProducerObserverEvents = { close: []; pause: []; @@ -198,7 +200,8 @@ export class Producer< #score: ProducerScore[] = []; // Observer instance. - readonly #observer = new EnhancedEventEmitter(); + readonly #observer: ProducerObserver = + new EnhancedEventEmitter(); /** * @private @@ -304,7 +307,7 @@ export class Producer< /** * Observer. */ - get observer(): EnhancedEventEmitter { + get observer(): ProducerObserver { return this.#observer; } diff --git a/node/src/Router.ts b/node/src/Router.ts index c604768053..fb05298187 100644 --- a/node/src/Router.ts +++ b/node/src/Router.ts @@ -198,6 +198,8 @@ export type RouterEvents = { '@close': []; }; +export type RouterObserver = EnhancedEventEmitter; + export type RouterObserverEvents = { close: []; newtransport: [Transport]; @@ -252,7 +254,8 @@ export class Router< > = new Map(); // Observer instance. - readonly #observer = new EnhancedEventEmitter(); + readonly #observer: RouterObserver = + new EnhancedEventEmitter(); /** * @private @@ -316,7 +319,7 @@ export class Router< /** * Observer. */ - get observer(): EnhancedEventEmitter { + get observer(): RouterObserver { return this.#observer; } diff --git a/node/src/RtpObserver.ts b/node/src/RtpObserver.ts index 1756880755..9bc808e5ce 100644 --- a/node/src/RtpObserver.ts +++ b/node/src/RtpObserver.ts @@ -15,6 +15,9 @@ export type RtpObserverEvents = { '@close': []; }; +export type RtpObserverObserver = + EnhancedEventEmitter; + export type RtpObserverObserverEvents = { close: []; pause: []; @@ -43,9 +46,10 @@ export type RtpObserverAddRemoveProducerOptions = { producerId: string; }; -export class RtpObserver< +export abstract class RtpObserver< RtpObserverAppData extends AppData = AppData, Events extends RtpObserverEvents = RtpObserverEvents, + Observer extends RtpObserverObserver = RtpObserverObserver, > extends EnhancedEventEmitter { // Internal data. protected readonly internal: RtpObserverObserverInternal; @@ -68,18 +72,21 @@ export class RtpObserver< ) => Producer | undefined; // Observer instance. - readonly #observer = new EnhancedEventEmitter(); + readonly #observer: Observer; /** * @private * @interface */ - constructor({ - internal, - channel, - appData, - getProducerById, - }: RtpObserverConstructorOptions) { + constructor( + { + internal, + channel, + appData, + getProducerById, + }: RtpObserverConstructorOptions, + observer: Observer + ) { super(); logger.debug('constructor()'); @@ -88,6 +95,7 @@ export class RtpObserver< this.channel = channel; this.#appData = appData || ({} as RtpObserverAppData); this.getProducerById = getProducerById; + this.#observer = observer; } /** @@ -128,7 +136,7 @@ export class RtpObserver< /** * Observer. */ - get observer(): EnhancedEventEmitter { + get observer(): Observer { return this.#observer; } diff --git a/node/src/Transport.ts b/node/src/Transport.ts index f748b9e9c1..2b6bced94d 100644 --- a/node/src/Transport.ts +++ b/node/src/Transport.ts @@ -223,6 +223,8 @@ export type TransportEvents = { '@listenserverclose': []; }; +export type TransportObserver = EnhancedEventEmitter; + export type TransportObserverEvents = { close: []; newproducer: [Producer]; @@ -317,10 +319,10 @@ type RecvRtpHeaderExtensions = { const logger = new Logger('Transport'); -export class Transport< +export abstract class Transport< TransportAppData extends AppData = AppData, Events extends TransportEvents = TransportEvents, - ObserverEvents extends TransportObserverEvents = TransportObserverEvents, + Observer extends TransportObserver = TransportObserver, > extends EnhancedEventEmitter { // Internal data. protected readonly internal: TransportInternal; @@ -375,21 +377,24 @@ export class Transport< #nextSctpStreamId = 0; // Observer instance. - readonly #observer = new EnhancedEventEmitter(); + readonly #observer: Observer; /** * @private * @interface */ - constructor({ - internal, - data, - channel, - appData, - getRouterRtpCapabilities, - getProducerById, - getDataProducerById, - }: TransportConstructorOptions) { + constructor( + { + internal, + data, + channel, + appData, + getRouterRtpCapabilities, + getProducerById, + getDataProducerById, + }: TransportConstructorOptions, + observer: Observer + ) { super(); logger.debug('constructor()'); @@ -401,6 +406,7 @@ export class Transport< this.#getRouterRtpCapabilities = getRouterRtpCapabilities; this.getProducerById = getProducerById; this.getDataProducerById = getDataProducerById; + this.#observer = observer; } /** @@ -434,7 +440,7 @@ export class Transport< /** * Observer. */ - get observer(): EnhancedEventEmitter { + get observer(): Observer { return this.#observer; } diff --git a/node/src/WebRtcServer.ts b/node/src/WebRtcServer.ts index a820bfda3d..29082d80c8 100644 --- a/node/src/WebRtcServer.ts +++ b/node/src/WebRtcServer.ts @@ -34,6 +34,9 @@ export type WebRtcServerEvents = { '@close': []; }; +export type WebRtcServerObserver = + EnhancedEventEmitter; + export type WebRtcServerObserverEvents = { close: []; webrtctransporthandled: [WebRtcTransport]; @@ -89,7 +92,8 @@ export class WebRtcServer< readonly #webRtcTransports: Map = new Map(); // Observer instance. - readonly #observer = new EnhancedEventEmitter(); + readonly #observer: WebRtcServerObserver = + new EnhancedEventEmitter(); /** * @private @@ -143,7 +147,7 @@ export class WebRtcServer< /** * Observer. */ - get observer(): EnhancedEventEmitter { + get observer(): WebRtcServerObserver { return this.#observer; } diff --git a/node/src/WebRtcTransport.ts b/node/src/WebRtcTransport.ts index 1e0a8d5b49..11977b6cfd 100644 --- a/node/src/WebRtcTransport.ts +++ b/node/src/WebRtcTransport.ts @@ -1,5 +1,6 @@ import * as flatbuffers from 'flatbuffers'; import { Logger } from './Logger'; +import { EnhancedEventEmitter } from './enhancedEvents'; import { parseSctpState, parseBaseTransportDump, @@ -215,6 +216,9 @@ export type WebRtcTransportEvents = TransportEvents & { sctpstatechange: [SctpState]; }; +export type WebRtcTransportObserver = + EnhancedEventEmitter; + export type WebRtcTransportObserverEvents = TransportObserverEvents & { icestatechange: [IceState]; iceselectedtuplechange: [TransportTuple]; @@ -258,7 +262,7 @@ export class WebRtcTransport< > extends Transport< WebRtcTransportAppData, WebRtcTransportEvents, - WebRtcTransportObserverEvents + WebRtcTransportObserver > { // WebRtcTransport data. readonly #data: WebRtcTransportData; @@ -269,7 +273,10 @@ export class WebRtcTransport< constructor( options: WebRtcTransportConstructorOptions ) { - super(options); + const observer: WebRtcTransportObserver = + new EnhancedEventEmitter(); + + super(options, observer); logger.debug('constructor()'); @@ -291,6 +298,15 @@ export class WebRtcTransport< this.handleWorkerNotifications(); } + /** + * Observer. + * + * @override + */ + get observer(): WebRtcTransportObserver { + return super.observer; + } + /** * ICE role. */ diff --git a/node/src/Worker.ts b/node/src/Worker.ts index b69d2c9c29..151211fa61 100644 --- a/node/src/Worker.ts +++ b/node/src/Worker.ts @@ -210,6 +210,8 @@ export type WorkerEvents = { '@failure': [Error]; }; +export type WorkerObserver = EnhancedEventEmitter; + export type WorkerObserverEvents = { close: []; newwebrtcserver: [WebRtcServer]; @@ -275,7 +277,8 @@ export class Worker< readonly #routers: Set = new Set(); // Observer instance. - readonly #observer = new EnhancedEventEmitter(); + readonly #observer: WorkerObserver = + new EnhancedEventEmitter(); /** * @private @@ -545,7 +548,7 @@ export class Worker< /** * Observer. */ - get observer(): EnhancedEventEmitter { + get observer(): WorkerObserver { return this.#observer; } diff --git a/node/src/index.ts b/node/src/index.ts index 28c41f361d..38410c419a 100644 --- a/node/src/index.ts +++ b/node/src/index.ts @@ -22,11 +22,13 @@ export const version: string = require('../../package.json').version; */ export { parse as parseScalabilityMode } from './scalabilityModes'; +export type Observer = EnhancedEventEmitter; + export type ObserverEvents = { newworker: [Worker]; }; -const observer = new EnhancedEventEmitter(); +const observer: Observer = new EnhancedEventEmitter(); /** * Observer.