@@ -26,6 +26,9 @@ export class RequestLogManager {
2626 private requestLogs : RequestLogEntry [ ] = [ ]
2727 private config : RequestLogConfig
2828 private initialized = false
29+ private persistTimer : NodeJS . Timeout | null = null
30+ private dirty = false
31+ private readonly persistDelayMs = 2000
2932
3033 constructor ( options : RequestLogManagerOptions ) {
3134 this . storageDir = options . storageDir
@@ -44,7 +47,7 @@ export class RequestLogManager {
4447 setConfig ( config : Partial < RequestLogConfig > ) : void {
4548 this . config = normalizeRequestLogConfig ( config )
4649 this . requestLogs = trimRequestLogsToMaxEntries ( this . requestLogs , this . config )
47- this . persist ( )
50+ this . schedulePersist ( )
4851 }
4952
5053 async migrateLegacyLogs ( legacyLogs : RequestLogEntry [ ] ) : Promise < boolean > {
@@ -63,7 +66,7 @@ export class RequestLogManager {
6366 } ) ) ,
6467 this . config ,
6568 )
66- this . persist ( )
69+ this . schedulePersist ( )
6770
6871 return true
6972 }
@@ -82,7 +85,7 @@ export class RequestLogManager {
8285
8386 this . requestLogs . push ( logEntry )
8487 this . requestLogs = trimRequestLogsToMaxEntries ( this . requestLogs , this . config )
85- this . persist ( )
88+ this . schedulePersist ( )
8689
8790 return logEntry
8891 }
@@ -102,7 +105,7 @@ export class RequestLogManager {
102105 ...this . requestLogs [ index ] ,
103106 ...sanitizeRequestLogUpdates ( updates , this . config ) ,
104107 }
105- this . persist ( )
108+ this . schedulePersist ( )
106109 return true
107110 }
108111
@@ -135,7 +138,7 @@ export class RequestLogManager {
135138 clearRequestLogs ( ) : void {
136139 this . ensureInitialized ( )
137140 this . requestLogs = [ ]
138- this . persist ( )
141+ this . schedulePersist ( )
139142 }
140143
141144 getRequestLogStats ( ) : RequestLogStats {
@@ -188,6 +191,19 @@ export class RequestLogManager {
188191 return [ ...this . requestLogs ]
189192 }
190193
194+ flushSync ( ) : void {
195+ if ( ! this . initialized ) {
196+ return
197+ }
198+
199+ if ( this . persistTimer ) {
200+ clearTimeout ( this . persistTimer )
201+ this . persistTimer = null
202+ }
203+
204+ this . persistNow ( )
205+ }
206+
191207 private ensureInitialized ( ) : void {
192208 if ( ! this . initialized ) {
193209 throw new Error ( 'RequestLogManager is not initialized' )
@@ -217,9 +233,27 @@ export class RequestLogManager {
217233 . filter ( ( entry ) : entry is RequestLogEntry => entry !== null )
218234 }
219235
220- private persist ( ) : void {
236+ private schedulePersist ( ) : void {
237+ this . dirty = true
238+
239+ if ( this . persistTimer ) {
240+ clearTimeout ( this . persistTimer )
241+ }
242+
243+ this . persistTimer = setTimeout ( ( ) => {
244+ this . persistTimer = null
245+ this . persistNow ( )
246+ } , this . persistDelayMs )
247+ }
248+
249+ private persistNow ( ) : void {
250+ if ( ! this . dirty ) {
251+ return
252+ }
253+
221254 const content = this . requestLogs . map ( ( entry ) => JSON . stringify ( entry ) ) . join ( '\n' )
222255 writeFileSync ( this . logFile , content , 'utf-8' )
256+ this . dirty = false
223257 }
224258}
225259
0 commit comments