@@ -149,7 +149,9 @@ export class ReplayContainer implements ReplayContainerInterface {
149149 public start ( ) : void {
150150 this . _setInitialState ( ) ;
151151
152- this . _loadSession ( { expiry : SESSION_IDLE_DURATION } ) ;
152+ if ( ! this . _loadAndCheckSession ( ) ) {
153+ return ;
154+ }
153155
154156 // If there is no session, then something bad has happened - can't continue
155157 if ( ! this . session ) {
@@ -228,6 +230,10 @@ export class ReplayContainer implements ReplayContainerInterface {
228230 * does not support a teardown
229231 */
230232 public stop ( ) : void {
233+ if ( ! this . _isEnabled ) {
234+ return ;
235+ }
236+
231237 try {
232238 __DEBUG_BUILD__ && logger . log ( '[Replay] Stopping Replays' ) ;
233239 this . _isEnabled = false ;
@@ -258,8 +264,7 @@ export class ReplayContainer implements ReplayContainerInterface {
258264 * new DOM checkout.`
259265 */
260266 public resume ( ) : void {
261- if ( ! this . session || ! this . session . sampled ) {
262- this . stop ( ) ;
267+ if ( ! this . _loadAndCheckSession ( ) ) {
263268 return ;
264269 }
265270
@@ -309,12 +314,7 @@ export class ReplayContainer implements ReplayContainerInterface {
309314 if ( ! this . _stopRecording ) {
310315 // Create a new session, otherwise when the user action is flushed, it
311316 // will get rejected due to an expired session.
312- this . _loadSession ( { expiry : SESSION_IDLE_DURATION } ) ;
313-
314- // We know this is set, because it is always set in _loadSession
315- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
316- if ( ! this . session ! . sampled ) {
317- this . stop ( ) ;
317+ if ( ! this . _loadAndCheckSession ( ) ) {
318318 return ;
319319 }
320320
@@ -354,7 +354,7 @@ export class ReplayContainer implements ReplayContainerInterface {
354354 * Returns true if session is not expired, false otherwise.
355355 * @hidden
356356 */
357- public checkAndHandleExpiredSession ( { expiry = SESSION_IDLE_DURATION } : { expiry ?: number } = { } ) : boolean | void {
357+ public checkAndHandleExpiredSession ( expiry ?: number ) : boolean | void {
358358 const oldSessionId = this . getSessionId ( ) ;
359359
360360 // Prevent starting a new session if the last user activity is older than
@@ -369,12 +369,7 @@ export class ReplayContainer implements ReplayContainerInterface {
369369
370370 // --- There is recent user activity --- //
371371 // This will create a new session if expired, based on expiry length
372- this . _loadSession ( { expiry } ) ;
373-
374- // We know this is set, because it is always set in _loadSession
375- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
376- if ( ! this . session ! . sampled ) {
377- this . stop ( ) ;
372+ if ( ! this . _loadAndCheckSession ( expiry ) ) {
378373 return ;
379374 }
380375
@@ -401,10 +396,10 @@ export class ReplayContainer implements ReplayContainerInterface {
401396 }
402397
403398 /**
404- * Loads a session from storage, or creates a new one if it does not exist or
405- * is expired .
399+ * Loads ( or refreshes) the current session.
400+ * Returns false if session is not recorded .
406401 */
407- private _loadSession ( { expiry } : { expiry : number } ) : void {
402+ private _loadAndCheckSession ( expiry = SESSION_IDLE_DURATION ) : boolean {
408403 const { type, session } = getSession ( {
409404 expiry,
410405 stickySession : Boolean ( this . _options . stickySession ) ,
@@ -425,6 +420,13 @@ export class ReplayContainer implements ReplayContainerInterface {
425420 }
426421
427422 this . session = session ;
423+
424+ if ( ! this . session . sampled ) {
425+ this . stop ( ) ;
426+ return false ;
427+ }
428+
429+ return true ;
428430 }
429431
430432 /**
@@ -630,9 +632,7 @@ export class ReplayContainer implements ReplayContainerInterface {
630632 return ;
631633 }
632634
633- const isSessionActive = this . checkAndHandleExpiredSession ( {
634- expiry : VISIBILITY_CHANGE_TIMEOUT ,
635- } ) ;
635+ const isSessionActive = this . checkAndHandleExpiredSession ( VISIBILITY_CHANGE_TIMEOUT ) ;
636636
637637 if ( ! isSessionActive ) {
638638 // If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
0 commit comments