Skip to content

Commit

Permalink
Node: Add 'listenererror' in all entities
Browse files Browse the repository at this point in the history
- 'listenererror' event is emitted when an event listener throws (only if it's a synchronous error, otherwise an unhandled rejection will happen anyway).
- Usage:
  ```ts
  consumer.on('listenererror', (eventName, error) => { ... });
  ```
- Related to ongoing issue #1188 (but it's not fixing it at all, this is just a tool that should be helpful in that issue).
  • Loading branch information
ibc committed Nov 17, 2023
1 parent 93bbccf commit eaafcc7
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions node/src/Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export type ConsumerEvents =
layerschange: [ConsumerLayers?];
trace: [ConsumerTraceEventData];
rtp: [Buffer];
listenererror: [string, Error];
// Private events.
'@close': [];
'@producerclose': [];
Expand Down
1 change: 1 addition & 0 deletions node/src/DataConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export type DataConsumerEvents =
message: [Buffer, number];
sctpsendbufferfull: [];
bufferedamountlow: [number];
listenererror: [string, Error];
// Private events.
'@close': [];
'@dataproducerclose': [];
Expand Down
1 change: 1 addition & 0 deletions node/src/DataProducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type DataProducerType = 'sctp' | 'direct';
export type DataProducerEvents =
{
transportclose: [];
listenererror: [string, Error];
// Private events.
'@close': [];
};
Expand Down
18 changes: 13 additions & 5 deletions node/src/EnhancedEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export class EnhancedEventEmitter<E extends Events = Events> extends EventEmitte
*/
safeEmit<K extends keyof E & string>(eventName: K, ...args: E[K]): boolean
{
const numListeners = super.listenerCount(eventName);

try
{
return super.emit(eventName, ...args);
Expand All @@ -33,9 +31,19 @@ export class EnhancedEventEmitter<E extends Events = Events> extends EventEmitte
{
logger.error(
'safeEmit() | event listener threw an error [eventName:%s]:%o',
eventName, error);

return Boolean(numListeners);
eventName, error
);

try
{
super.emit('listenererror', eventName, error);
}
catch (error2)
{
// Ignore it.
}

return Boolean(super.listenerCount(eventName));
}
}

Expand Down
1 change: 1 addition & 0 deletions node/src/Producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export type ProducerEvents =
score: [ProducerScore[]];
videoorientationchange: [ProducerVideoOrientation];
trace: [ProducerTraceEventData];
listenererror: [string, Error];
// Private events.
'@close': [];
};
Expand Down
1 change: 1 addition & 0 deletions node/src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ type PipeTransportPair =
export type RouterEvents =
{
workerclose: [];
listenererror: [string, Error];
// Private events.
'@close': [];
};
Expand Down
1 change: 1 addition & 0 deletions node/src/RtpObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as FbsRtpObserver from './fbs/rtp-observer';
export type RtpObserverEvents =
{
routerclose: [];
listenererror: [string, Error];
// Private events.
'@close': [];
};
Expand Down
1 change: 1 addition & 0 deletions node/src/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export type TransportEvents =
routerclose: [];
listenserverclose: [];
trace: [TransportTraceEventData];
listenererror: [string, Error];
// Private events.
'@close': [];
'@newproducer': [Producer];
Expand Down
1 change: 1 addition & 0 deletions node/src/WebRtcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type WebRtcServerListenInfo = TransportListenInfo;
export type WebRtcServerEvents =
{
workerclose: [];
listenererror: [string, Error];
// Private events.
'@close': [];
};
Expand Down
1 change: 1 addition & 0 deletions node/src/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export type WorkerDump =
export type WorkerEvents =
{
died: [Error];
listenererror: [string, Error];
// Private events.
'@success': [];
'@failure': [Error];
Expand Down
18 changes: 18 additions & 0 deletions node/src/tests/test-DirectTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ test('dataProducer.send() succeeds', async () =>
let lastSentMessageId = 0;
let lastRecvMessageId = 0;

console.log('TODO: REMOVE');

Check failure on line 133 in node/src/tests/test-DirectTransport.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, 20)

Unexpected console statement

Check failure on line 133 in node/src/tests/test-DirectTransport.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, 20)

Unexpected console statement
transport2.on('listenererror', (eventName, error) =>
{
throw new Error(`------ transport2 listener error [eventName:${eventName}]: ${error}`);
});

console.log('TODO: REMOVE');

Check failure on line 139 in node/src/tests/test-DirectTransport.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, 20)

Unexpected console statement

Check failure on line 139 in node/src/tests/test-DirectTransport.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, 20)

Unexpected console statement
dataProducer.on('listenererror', (eventName, error) =>
{
throw new Error(`------ dataProducer listener error [eventName:${eventName}]: ${error}`);
});

console.log('TODO: REMOVE');

Check failure on line 145 in node/src/tests/test-DirectTransport.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, 20)

Unexpected console statement

Check failure on line 145 in node/src/tests/test-DirectTransport.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, 20)

Unexpected console statement
dataConsumer.on('listenererror', (eventName, error) =>
{
throw new Error(`------ dataConsumer listener error [eventName:${eventName}]: ${error}`);
});

// eslint-disable-next-line no-async-promise-executor
await new Promise<void>(async (resolve) =>
{
Expand Down

0 comments on commit eaafcc7

Please sign in to comment.