@@ -155,7 +155,9 @@ export class ReplayContainer implements ReplayContainerInterface {
155155 public start ( ) : void {
156156 this . _setInitialState ( ) ;
157157
158- this . _loadSession ( { expiry : SESSION_IDLE_DURATION } ) ;
158+ if ( ! this . _loadAndCheckSession ( ) ) {
159+ return ;
160+ }
159161
160162 // If there is no session, then something bad has happened - can't continue
161163 if ( ! this . session ) {
@@ -234,6 +236,10 @@ export class ReplayContainer implements ReplayContainerInterface {
234236 * does not support a teardown
235237 */
236238 public stop ( ) : void {
239+ if ( ! this . _isEnabled ) {
240+ return ;
241+ }
242+
237243 try {
238244 __DEBUG_BUILD__ && logger . log ( '[Replay] Stopping Replays' ) ;
239245 this . _isEnabled = false ;
@@ -264,8 +270,7 @@ export class ReplayContainer implements ReplayContainerInterface {
264270 * new DOM checkout.`
265271 */
266272 public resume ( ) : void {
267- if ( ! this . session || ! this . session . sampled ) {
268- this . stop ( ) ;
273+ if ( ! this . _loadAndCheckSession ( ) ) {
269274 return ;
270275 }
271276
@@ -315,12 +320,7 @@ export class ReplayContainer implements ReplayContainerInterface {
315320 if ( ! this . _stopRecording ) {
316321 // Create a new session, otherwise when the user action is flushed, it
317322 // will get rejected due to an expired session.
318- this . _loadSession ( { expiry : SESSION_IDLE_DURATION } ) ;
319-
320- // We know this is set, because it is always set in _loadSession
321- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
322- if ( ! this . session ! . sampled ) {
323- this . stop ( ) ;
323+ if ( ! this . _loadAndCheckSession ( ) ) {
324324 return ;
325325 }
326326
@@ -360,7 +360,7 @@ export class ReplayContainer implements ReplayContainerInterface {
360360 * Returns true if session is not expired, false otherwise.
361361 * @hidden
362362 */
363- public checkAndHandleExpiredSession ( { expiry = SESSION_IDLE_DURATION } : { expiry ?: number } = { } ) : boolean | void {
363+ public checkAndHandleExpiredSession ( expiry ?: number ) : boolean | void {
364364 const oldSessionId = this . getSessionId ( ) ;
365365
366366 // Prevent starting a new session if the last user activity is older than
@@ -375,12 +375,7 @@ export class ReplayContainer implements ReplayContainerInterface {
375375
376376 // --- There is recent user activity --- //
377377 // This will create a new session if expired, based on expiry length
378- this . _loadSession ( { expiry } ) ;
379-
380- // We know this is set, because it is always set in _loadSession
381- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
382- if ( ! this . session ! . sampled ) {
383- this . stop ( ) ;
378+ if ( ! this . _loadAndCheckSession ( expiry ) ) {
384379 return ;
385380 }
386381
@@ -407,10 +402,10 @@ export class ReplayContainer implements ReplayContainerInterface {
407402 }
408403
409404 /**
410- * Loads a session from storage, or creates a new one if it does not exist or
411- * is expired .
405+ * Loads ( or refreshes) the current session.
406+ * Returns false if session is not recorded .
412407 */
413- private _loadSession ( { expiry } : { expiry : number } ) : void {
408+ private _loadAndCheckSession ( expiry = SESSION_IDLE_DURATION ) : boolean {
414409 const { type, session } = getSession ( {
415410 expiry,
416411 stickySession : Boolean ( this . _options . stickySession ) ,
@@ -431,6 +426,13 @@ export class ReplayContainer implements ReplayContainerInterface {
431426 }
432427
433428 this . session = session ;
429+
430+ if ( ! this . session . sampled ) {
431+ this . stop ( ) ;
432+ return false ;
433+ }
434+
435+ return true ;
434436 }
435437
436438 /**
@@ -637,9 +639,7 @@ export class ReplayContainer implements ReplayContainerInterface {
637639 return ;
638640 }
639641
640- const isSessionActive = this . checkAndHandleExpiredSession ( {
641- expiry : VISIBILITY_CHANGE_TIMEOUT ,
642- } ) ;
642+ const isSessionActive = this . checkAndHandleExpiredSession ( VISIBILITY_CHANGE_TIMEOUT ) ;
643643
644644 if ( ! isSessionActive ) {
645645 // If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
0 commit comments