Skip to content

Commit

Permalink
Remove Logger from EnhancedEventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Nov 9, 2024
1 parent 7dd9e61 commit dc614c1
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 16 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const config = tsEslint.config(
2,
{ vars: 'all', args: 'after-used', caughtErrors: 'none' },
],
'no-unused-private-class-members': 2,
'no-use-before-define': 0,
'no-useless-call': 2,
'no-useless-computed-key': 2,
Expand Down
10 changes: 10 additions & 0 deletions node/src/ActiveSpeakerObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class ActiveSpeakerObserverImpl<
super(options, observer);

this.handleWorkerNotifications();
this.handleListenerError();
}

/**
Expand Down Expand Up @@ -85,4 +86,13 @@ export class ActiveSpeakerObserverImpl<
}
);
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}
10 changes: 10 additions & 0 deletions node/src/AudioLevelObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class AudioLevelObserverImpl<
super(options, observer);

this.handleWorkerNotifications();
this.handleListenerError();
}

/**
Expand Down Expand Up @@ -108,6 +109,15 @@ export class AudioLevelObserverImpl<
}
);
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

function parseVolume(binary: FbsAudioLevelObserver.Volume): {
Expand Down
10 changes: 10 additions & 0 deletions node/src/Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class ConsumerImpl<ConsumerAppData extends AppData = AppData>
this.#appData = appData ?? ({} as ConsumerAppData);

this.handleWorkerNotifications();
this.handleListenerError();
}

get id(): string {
Expand Down Expand Up @@ -598,6 +599,15 @@ export class ConsumerImpl<ConsumerAppData extends AppData = AppData>
}
);
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

export function parseTraceEventData(
Expand Down
10 changes: 10 additions & 0 deletions node/src/DataConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class DataConsumerImpl<DataConsumerAppData extends AppData = AppData>
this.#appData = appData ?? ({} as DataConsumerAppData);

this.handleWorkerNotifications();
this.handleListenerError();
}

get id(): string {
Expand Down Expand Up @@ -554,6 +555,15 @@ export class DataConsumerImpl<DataConsumerAppData extends AppData = AppData>
}
);
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

export function dataConsumerTypeToFbs(
Expand Down
10 changes: 10 additions & 0 deletions node/src/DataProducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class DataProducerImpl<DataProducerAppData extends AppData = AppData>
this.#appData = appData ?? ({} as DataProducerAppData);

this.handleWorkerNotifications();
this.handleListenerError();
}

get id(): string {
Expand Down Expand Up @@ -333,6 +334,15 @@ export class DataProducerImpl<DataProducerAppData extends AppData = AppData>
private handleWorkerNotifications(): void {
// No need to subscribe to any event.
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

export function dataProducerTypeToFbs(
Expand Down
10 changes: 10 additions & 0 deletions node/src/DirectTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class DirectTransportImpl<
};

this.handleWorkerNotifications();
this.handleListenerError();
}

get observer(): DirectTransportObserver {
Expand Down Expand Up @@ -214,6 +215,15 @@ export class DirectTransportImpl<
}
);
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

export function parseDirectTransportDumpResponse(
Expand Down
2 changes: 1 addition & 1 deletion node/src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debug from 'debug';
import { EnhancedEventEmitter } from './enhancedEvents';
import type { EnhancedEventEmitter } from './enhancedEvents';

const APP_NAME = 'mediasoup';

Expand Down
10 changes: 10 additions & 0 deletions node/src/PipeTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class PipeTransportImpl<PipeTransportAppData extends AppData = AppData>
};

this.handleWorkerNotifications();
this.handleListenerError();
}

get observer(): PipeTransportObserver {
Expand Down Expand Up @@ -328,6 +329,15 @@ export class PipeTransportImpl<PipeTransportAppData extends AppData = AppData>
}
);
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

/*
Expand Down
10 changes: 10 additions & 0 deletions node/src/PlainTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class PlainTransportImpl<PlainTransportAppData extends AppData = AppData>
};

this.handleWorkerNotifications();
this.handleListenerError();
}

get observer(): PlainTransportObserver {
Expand Down Expand Up @@ -294,6 +295,15 @@ export class PlainTransportImpl<PlainTransportAppData extends AppData = AppData>
}
);
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

export function parsePlainTransportDumpResponse(
Expand Down
10 changes: 10 additions & 0 deletions node/src/Producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class ProducerImpl<ProducerAppData extends AppData = AppData>
this.#appData = appData ?? ({} as ProducerAppData);

this.handleWorkerNotifications();
this.handleListenerError();
}

get id(): string {
Expand Down Expand Up @@ -397,6 +398,15 @@ export class ProducerImpl<ProducerAppData extends AppData = AppData>
}
);
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

export function producerTypeFromFbs(type: FbsRtpParameters.Type): ProducerType {
Expand Down
11 changes: 11 additions & 0 deletions node/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ export class RouterImpl<RouterAppData extends AppData = AppData>
this.#data = data;
this.#channel = channel;
this.#appData = appData ?? ({} as RouterAppData);

this.handleListenerError();
}

get id(): string {
Expand Down Expand Up @@ -1364,6 +1366,15 @@ export class RouterImpl<RouterAppData extends AppData = AppData>
return false;
}
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

export function parseRouterDumpResponse(
Expand Down
2 changes: 1 addition & 1 deletion node/src/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ function createProduceRequest({
producerId: string;
kind: MediaKind;
rtpParameters: RtpParameters;
rtpMapping: ortc.RtpMapping;
rtpMapping: ortc.RtpCodecsEncodingsMapping;
keyFrameRequestDelay?: number;
paused: boolean;
}): number {
Expand Down
11 changes: 11 additions & 0 deletions node/src/WebRtcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export class WebRtcServerImpl<WebRtcServerAppData extends AppData = AppData>
this.#internal = internal;
this.#channel = channel;
this.#appData = appData ?? ({} as WebRtcServerAppData);

this.handleListenerError();
}

get id(): string {
Expand Down Expand Up @@ -179,6 +181,15 @@ export class WebRtcServerImpl<WebRtcServerAppData extends AppData = AppData>
this.#observer.safeEmit('webrtctransportunhandled', webRtcTransport);
});
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

function parseIpPort(binary: FbsWebRtcServer.IpPort): IpPort {
Expand Down
10 changes: 10 additions & 0 deletions node/src/WebRtcTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export class WebRtcTransportImpl<
};

this.handleWorkerNotifications();
this.handleListenerError();
}

get observer(): WebRtcTransportObserver {
Expand Down Expand Up @@ -395,6 +396,15 @@ export class WebRtcTransportImpl<
}
);
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

function iceStateFromFbs(fbsIceState: FbsIceState): IceState {
Expand Down
11 changes: 11 additions & 0 deletions node/src/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ export class WorkerImpl<WorkerAppData extends AppData = AppData>
}
}
});

this.handleListenerError();
}

get pid(): number {
Expand Down Expand Up @@ -584,6 +586,15 @@ export class WorkerImpl<WorkerAppData extends AppData = AppData>
// Emit observer event.
this.#observer.safeEmit('close');
}

private handleListenerError(): void {
this.on('listenererror', (eventName, error) => {
logger.error(
`event listener threw an error [eventName:${eventName}]:`,
error
);
});
}
}

export function parseWorkerDumpResponse(
Expand Down
9 changes: 1 addition & 8 deletions node/src/enhancedEvents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { EventEmitter, once } from 'node:events';
import { Logger } from './Logger';

const enhancedEventEmitterLogger = new Logger('EnhancedEventEmitter');

type Events = Record<string, any[]>;

Expand All @@ -10,6 +7,7 @@ export class EnhancedEventEmitter<
> extends EventEmitter {
constructor() {
super();

this.setMaxListeners(Infinity);
}

Expand All @@ -24,11 +22,6 @@ export class EnhancedEventEmitter<
try {
return super.emit(eventName, ...args);
} catch (error) {
enhancedEventEmitterLogger.error(
`safeEmit() | event listener threw an error [eventName:${eventName}]:`,
error as Error
);

try {
super.emit('listenererror', eventName, error);
} catch (error2) {
Expand Down
Loading

0 comments on commit dc614c1

Please sign in to comment.