@@ -19,6 +19,7 @@ import type {
1919 PopEventContext ,
2020 RecordingOptions ,
2121 ReplayContainer as ReplayContainerInterface ,
22+ ReplayFlushOptions ,
2223 ReplayPluginOptions ,
2324 Session ,
2425 Timeouts ,
@@ -86,11 +87,6 @@ export class ReplayContainer implements ReplayContainerInterface {
8687 */
8788 private _isEnabled : boolean = false ;
8889
89- /**
90- * If true, will flush regardless of `_isEnabled` property
91- */
92- private _shouldFinalFlush : boolean = false ;
93-
9490 /**
9591 * Paused is a state where:
9692 * - DOM Recording is not listening at all
@@ -259,16 +255,16 @@ export class ReplayContainer implements ReplayContainerInterface {
259255 log ( msg ) ;
260256 }
261257
262- // Set this property so that it ignores `_isEnabled` = false
263258 // We can't move `_isEnabled` after awaiting a flush, otherwise we can
264259 // enter into an infinite loop when `stop()` is called while flushing.
265- this . _shouldFinalFlush = true ;
266260 this . _isEnabled = false ;
267261 this . _removeListeners ( ) ;
268262 this . stopRecording ( ) ;
269263
270- // Flush event buffer before stopping
271- await this . flushImmediate ( ) ;
264+ this . _debouncedFlush . cancel ( ) ;
265+ // See comment above re: `_isEnabled`, we "force" a flush, ignoring the
266+ // `_isEnabled` state of the plugin since it was disabled above.
267+ await this . _flush ( { force : true } )
272268
273269 // After flush, destroy event buffer
274270 this . eventBuffer && this . eventBuffer . destroy ( ) ;
@@ -279,8 +275,6 @@ export class ReplayContainer implements ReplayContainerInterface {
279275 clearSession ( this ) ;
280276 } catch ( err ) {
281277 this . _handleException ( err ) ;
282- } finally {
283- this . _shouldFinalFlush = false ;
284278 }
285279 }
286280
@@ -794,8 +788,8 @@ export class ReplayContainer implements ReplayContainerInterface {
794788 * Flush recording data to Sentry. Creates a lock so that only a single flush
795789 * can be active at a time. Do not call this directly.
796790 */
797- private _flush : ( ) => Promise < void > = async ( ) => {
798- if ( ! this . _isEnabled && ! this . _shouldFinalFlush ) {
791+ private _flush = async ( { force = false } : ReplayFlushOptions = { } ) : Promise < void > => {
792+ if ( ! this . _isEnabled && ! force ) {
799793 // This can happen if e.g. the replay was stopped because of exceeding the retry limit
800794 return ;
801795 }
0 commit comments