11/**
2- * Copyright 2019-2020, Optimizely
2+ * Copyright 2019-2020, 2023 Optimizely
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -36,31 +36,41 @@ const logger = getLogger('LogTierV1EventProcessor')
3636
3737export class LogTierV1EventProcessor implements EventProcessor {
3838 private dispatcher : EventDispatcher
39+ private closingDispatcher ?: EventDispatcher
3940 private queue : EventQueue < ProcessableEvent >
4041 private notificationCenter ?: NotificationCenter
4142 private requestTracker : RequestTracker
4243
4344 constructor ( {
4445 dispatcher,
46+ closingDispatcher,
4547 flushInterval = DEFAULT_FLUSH_INTERVAL ,
4648 batchSize = DEFAULT_BATCH_SIZE ,
4749 notificationCenter,
4850 } : {
4951 dispatcher : EventDispatcher
52+ closingDispatcher ?: EventDispatcher
5053 flushInterval ?: number
5154 batchSize ?: number
5255 notificationCenter ?: NotificationCenter
5356 } ) {
5457 this . dispatcher = dispatcher
58+ this . closingDispatcher = closingDispatcher
5559 this . notificationCenter = notificationCenter
5660 this . requestTracker = new RequestTracker ( )
5761
5862 flushInterval = validateAndGetFlushInterval ( flushInterval )
5963 batchSize = validateAndGetBatchSize ( batchSize )
60- this . queue = getQueue ( batchSize , flushInterval , this . drainQueue . bind ( this ) , areEventContextsEqual )
64+ this . queue = getQueue (
65+ batchSize ,
66+ flushInterval ,
67+ areEventContextsEqual ,
68+ this . drainQueue . bind ( this , false ) ,
69+ this . drainQueue . bind ( this , true ) ,
70+ )
6171 }
6272
63- drainQueue ( buffer : ProcessableEvent [ ] ) : Promise < void > {
73+ private drainQueue ( useClosingDispatcher : boolean , buffer : ProcessableEvent [ ] ) : Promise < void > {
6474 const reqPromise = new Promise < void > ( resolve => {
6575 logger . debug ( 'draining queue with %s events' , buffer . length )
6676
@@ -70,7 +80,10 @@ export class LogTierV1EventProcessor implements EventProcessor {
7080 }
7181
7282 const formattedEvent = formatEvents ( buffer )
73- this . dispatcher . dispatchEvent ( formattedEvent , ( ) => {
83+ const dispatcher = useClosingDispatcher && this . closingDispatcher
84+ ? this . closingDispatcher : this . dispatcher ;
85+
86+ dispatcher . dispatchEvent ( formattedEvent , ( ) => {
7487 resolve ( )
7588 } )
7689 sendEventNotification ( this . notificationCenter , formattedEvent )
0 commit comments