@@ -231,8 +231,8 @@ export class ReplayContainer implements ReplayContainerInterface {
231
231
__DEBUG_BUILD__ && logger . log ( '[Replay] Stopping Replays' ) ;
232
232
this . _isEnabled = false ;
233
233
this . _removeListeners ( ) ;
234
- this . _stopRecording ?. ( ) ;
235
- this . eventBuffer ? .destroy ( ) ;
234
+ this . _stopRecording && this . _stopRecording ( ) ;
235
+ this . eventBuffer && this . eventBuffer . destroy ( ) ;
236
236
this . eventBuffer = null ;
237
237
this . _debouncedFlush . cancel ( ) ;
238
238
} catch ( err ) {
@@ -278,7 +278,7 @@ export class ReplayContainer implements ReplayContainerInterface {
278
278
*/
279
279
public addUpdate ( cb : AddUpdateCallback ) : void {
280
280
// We need to always run `cb` (e.g. in the case of `this.recordingMode == 'error'`)
281
- const cbResult = cb ?. ( ) ;
281
+ const cbResult = cb ( ) ;
282
282
283
283
// If this option is turned on then we will only want to call `flush`
284
284
// explicitly
@@ -335,6 +335,11 @@ export class ReplayContainer implements ReplayContainerInterface {
335
335
return this . _debouncedFlush . flush ( ) as Promise < void > ;
336
336
}
337
337
338
+ /** Get the current sesion (=replay) ID */
339
+ public getSessionId ( ) : string | undefined {
340
+ return this . session && this . session . id ;
341
+ }
342
+
338
343
/** A wrapper to conditionally capture exceptions. */
339
344
private _handleException ( error : unknown ) : void {
340
345
__DEBUG_BUILD__ && logger . error ( '[Replay]' , error ) ;
@@ -363,8 +368,9 @@ export class ReplayContainer implements ReplayContainerInterface {
363
368
this . _setInitialState ( ) ;
364
369
}
365
370
366
- if ( session . id !== this . session ?. id ) {
367
- session . previousSessionId = this . session ?. id ;
371
+ const currentSessionId = this . getSessionId ( ) ;
372
+ if ( session . id !== currentSessionId ) {
373
+ session . previousSessionId = currentSessionId ;
368
374
}
369
375
370
376
this . session = session ;
@@ -405,7 +411,9 @@ export class ReplayContainer implements ReplayContainerInterface {
405
411
if ( ! this . _hasInitializedCoreListeners ) {
406
412
// Listeners from core SDK //
407
413
const scope = getCurrentHub ( ) . getScope ( ) ;
408
- scope ?. addScopeListener ( this . _handleCoreBreadcrumbListener ( 'scope' ) ) ;
414
+ if ( scope ) {
415
+ scope . addScopeListener ( this . _handleCoreBreadcrumbListener ( 'scope' ) ) ;
416
+ }
409
417
addInstrumentationHandler ( 'dom' , this . _handleCoreBreadcrumbListener ( 'dom' ) ) ;
410
418
addInstrumentationHandler ( 'fetch' , handleFetchSpanListener ( this ) ) ;
411
419
addInstrumentationHandler ( 'xhr' , handleXhrSpanListener ( this ) ) ;
@@ -492,7 +500,7 @@ export class ReplayContainer implements ReplayContainerInterface {
492
500
// of the previous session. Do not immediately flush in this case
493
501
// to avoid capturing only the checkout and instead the replay will
494
502
// be captured if they perform any follow-up actions.
495
- if ( this . session ? .previousSessionId ) {
503
+ if ( this . session && this . session . previousSessionId ) {
496
504
return true ;
497
505
}
498
506
@@ -707,7 +715,7 @@ export class ReplayContainer implements ReplayContainerInterface {
707
715
* Returns true if session is not expired, false otherwise.
708
716
*/
709
717
private _checkAndHandleExpiredSession ( { expiry = SESSION_IDLE_DURATION } : { expiry ?: number } = { } ) : boolean | void {
710
- const oldSessionId = this . session ?. id ;
718
+ const oldSessionId = this . getSessionId ( ) ;
711
719
712
720
// Prevent starting a new session if the last user activity is older than
713
721
// MAX_SESSION_LIFE. Otherwise non-user activity can trigger a new
@@ -724,7 +732,7 @@ export class ReplayContainer implements ReplayContainerInterface {
724
732
this . _loadSession ( { expiry } ) ;
725
733
726
734
// Session was expired if session ids do not match
727
- const expired = oldSessionId !== this . session ?. id ;
735
+ const expired = oldSessionId !== this . getSessionId ( ) ;
728
736
729
737
if ( ! expired ) {
730
738
return true ;
@@ -788,20 +796,26 @@ export class ReplayContainer implements ReplayContainerInterface {
788
796
* Should never be called directly, only by `flush`
789
797
*/
790
798
private async _runFlush ( ) : Promise < void > {
791
- if ( ! this . session ) {
792
- __DEBUG_BUILD__ && logger . error ( '[Replay] No session found to flush.' ) ;
799
+ if ( ! this . session || ! this . eventBuffer ) {
800
+ __DEBUG_BUILD__ && logger . error ( '[Replay] No session or eventBuffer found to flush.' ) ;
793
801
return ;
794
802
}
795
803
796
804
await this . _addPerformanceEntries ( ) ;
797
805
798
- if ( ! this . eventBuffer ?. pendingLength ) {
806
+ // Check eventBuffer again, as it could have been stopped in the meanwhile
807
+ if ( ! this . eventBuffer || ! this . eventBuffer . pendingLength ) {
799
808
return ;
800
809
}
801
810
802
811
// Only attach memory event if eventBuffer is not empty
803
812
await addMemoryEntry ( this ) ;
804
813
814
+ // Check eventBuffer again, as it could have been stopped in the meanwhile
815
+ if ( ! this . eventBuffer ) {
816
+ return ;
817
+ }
818
+
805
819
try {
806
820
// Note this empties the event buffer regardless of outcome of sending replay
807
821
const recordingData = await this . eventBuffer . finish ( ) ;
@@ -853,13 +867,13 @@ export class ReplayContainer implements ReplayContainerInterface {
853
867
return ;
854
868
}
855
869
856
- if ( ! this . session ?. id ) {
870
+ if ( ! this . session ) {
857
871
__DEBUG_BUILD__ && logger . error ( '[Replay] No session found to flush.' ) ;
858
872
return ;
859
873
}
860
874
861
875
// A flush is about to happen, cancel any queued flushes
862
- this . _debouncedFlush ? .cancel ( ) ;
876
+ this . _debouncedFlush . cancel ( ) ;
863
877
864
878
// this._flushLock acts as a lock so that future calls to `_flush()`
865
879
// will be blocked until this promise resolves
0 commit comments