@@ -50,7 +50,6 @@ export class DefaultEventQueue implements IEventQueue {
5050 private _queue : EventQueueItem [ ] = [ ] ;
5151 private _loadPersistedEvents = true ;
5252
53- // TODO: Implement support for max queue items.
5453 constructor (
5554 private config : Configuration ,
5655 private maxItems : number = 250
@@ -277,12 +276,21 @@ export class DefaultEventQueue implements IEventQueue {
277276 this . _lastFileTimestamp = Math . max ( Date . now ( ) , this . _lastFileTimestamp + 1 ) ;
278277 const file = `${ this . QUEUE_PREFIX } ${ this . _lastFileTimestamp } .json` ;
279278
280- this . _queue . push ( { file, event } ) ;
281- if ( this . config . usePersistedQueueStorage ) {
279+ const { storage, log } = this . config . services ;
280+ const useStorage : boolean = this . config . usePersistedQueueStorage ;
281+ if ( this . _queue . push ( { file, event } ) > this . maxItems ) {
282+ log . trace ( "Removing oldest queue entry: maxItems exceeded" ) ;
283+ const item = this . _queue . shift ( ) ;
284+ if ( useStorage ) {
285+ await storage . removeItem ( item . file ) ;
286+ }
287+ }
288+
289+ if ( useStorage ) {
282290 try {
283- await this . config . services . storage . setItem ( file , JSON . stringify ( event ) ) ;
291+ await storage . setItem ( file , JSON . stringify ( event ) ) ;
284292 } catch ( ex ) {
285- this . config . services . log . error ( `Error saving queue item to storage: ${ ex . message } ` )
293+ log . error ( `Error saving queue item to storage: ${ ex . message } ` )
286294 }
287295 }
288296
0 commit comments