@@ -15,6 +15,8 @@ import {
1515 Logger ,
1616} from '../types' ;
1717import { checkIsConnectionClosed , calcBitrate } from './utils' ;
18+ import { scheduleTask } from '../utils/tasks' ;
19+ import { CLEANUP_PREV_STATS_TTL_MS } from '../utils/constants' ;
1820
1921interface PrevStatsItem {
2022 stats : WebRTCStatsParsed ;
@@ -23,14 +25,11 @@ interface PrevStatsItem {
2325
2426interface WebRTCStatsParserParams {
2527 logger : Logger ;
26- prevConnectionStatsTtlMs ?: number ;
2728}
2829
2930class RTCStatsParser implements StatsParser {
3031 private readonly prevStats = new Map < string , PrevStatsItem | undefined > ( ) ;
3132
32- private readonly prevStatsCleanupTimers = new Map < string , NodeJS . Timer > ( ) ;
33-
3433 private readonly allowedReportTypes : Set < RTCStatsType > = new Set < RTCStatsType > ( [
3534 'candidate-pair' ,
3635 'inbound-rtp' ,
@@ -43,11 +42,8 @@ class RTCStatsParser implements StatsParser {
4342
4443 private readonly logger : Logger ;
4544
46- private readonly prevConnectionStatsTtlMs : number ;
47-
4845 constructor ( params : WebRTCStatsParserParams ) {
4946 this . logger = params . logger ;
50- this . prevConnectionStatsTtlMs = params . prevConnectionStatsTtlMs ?? 55_000 ;
5147 }
5248
5349 get previouslyParsedStatsConnectionsIds ( ) : string [ ] {
@@ -128,7 +124,11 @@ class RTCStatsParser implements StatsParser {
128124 ts : Date . now ( ) ,
129125 } ) ;
130126
131- this . resetStatsCleanupTimer ( connectionId ) ;
127+ scheduleTask ( {
128+ taskId : connectionId ,
129+ delayMs : CLEANUP_PREV_STATS_TTL_MS ,
130+ callback : ( ) => ( this . prevStats . delete ( connectionId ) ) ,
131+ } ) ;
132132
133133 return mappedStats ;
134134 }
@@ -287,21 +287,6 @@ class RTCStatsParser implements StatsParser {
287287
288288 return connectionStats as ParsedConnectionStats ;
289289 }
290-
291- private resetStatsCleanupTimer ( connectionId : string ) : void {
292- const existingTimer = this . prevStatsCleanupTimers . get ( connectionId ) ;
293-
294- if ( existingTimer ) {
295- clearTimeout ( existingTimer ) ;
296- }
297-
298- const timer = setTimeout ( ( ) => {
299- this . prevStats . delete ( connectionId ) ;
300- this . prevStatsCleanupTimers . delete ( connectionId ) ;
301- } , this . prevConnectionStatsTtlMs ) ;
302-
303- this . prevStatsCleanupTimers . set ( connectionId , timer ) ;
304- }
305290}
306291
307292export default RTCStatsParser ;
0 commit comments