@@ -149,7 +149,7 @@ export class ReplayContainer implements ReplayContainerInterface {
149149 public start ( ) : void {
150150 this . _setInitialState ( ) ;
151151
152- this . _loadSession ( { expiry : SESSION_IDLE_DURATION } ) ;
152+ this . _loadSession ( ) ;
153153
154154 // If there is no session, then something bad has happened - can't continue
155155 if ( ! this . session ) {
@@ -258,8 +258,7 @@ export class ReplayContainer implements ReplayContainerInterface {
258258 * new DOM checkout.`
259259 */
260260 public resume ( ) : void {
261- if ( ! this . session || ! this . session . sampled ) {
262- this . stop ( ) ;
261+ if ( ! this . _loadAndCheckSession ( ) ) {
263262 return ;
264263 }
265264
@@ -309,12 +308,7 @@ export class ReplayContainer implements ReplayContainerInterface {
309308 if ( ! this . _stopRecording ) {
310309 // Create a new session, otherwise when the user action is flushed, it
311310 // 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 ( ) ;
311+ if ( ! this . _loadAndCheckSession ( ) ) {
318312 return ;
319313 }
320314
@@ -354,7 +348,7 @@ export class ReplayContainer implements ReplayContainerInterface {
354348 * Returns true if session is not expired, false otherwise.
355349 * @hidden
356350 */
357- public checkAndHandleExpiredSession ( { expiry = SESSION_IDLE_DURATION } : { expiry ?: number } = { } ) : boolean | void {
351+ public checkAndHandleExpiredSession ( expiry ?: number ) : boolean | void {
358352 const oldSessionId = this . getSessionId ( ) ;
359353
360354 // Prevent starting a new session if the last user activity is older than
@@ -369,12 +363,7 @@ export class ReplayContainer implements ReplayContainerInterface {
369363
370364 // --- There is recent user activity --- //
371365 // 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 ( ) ;
366+ if ( ! this . _loadAndCheckSession ( expiry ) ) {
378367 return ;
379368 }
380369
@@ -404,7 +393,7 @@ export class ReplayContainer implements ReplayContainerInterface {
404393 * Loads a session from storage, or creates a new one if it does not exist or
405394 * is expired.
406395 */
407- private _loadSession ( { expiry } : { expiry : number } ) : void {
396+ private _loadSession ( expiry = SESSION_IDLE_DURATION ) : void {
408397 const { type, session } = getSession ( {
409398 expiry,
410399 stickySession : Boolean ( this . _options . stickySession ) ,
@@ -630,9 +619,7 @@ export class ReplayContainer implements ReplayContainerInterface {
630619 return ;
631620 }
632621
633- const isSessionActive = this . checkAndHandleExpiredSession ( {
634- expiry : VISIBILITY_CHANGE_TIMEOUT ,
635- } ) ;
622+ const isSessionActive = this . checkAndHandleExpiredSession ( VISIBILITY_CHANGE_TIMEOUT ) ;
636623
637624 if ( ! isSessionActive ) {
638625 // If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
@@ -891,4 +878,18 @@ export class ReplayContainer implements ReplayContainerInterface {
891878 } , rateLimitDuration ) ;
892879 }
893880 }
881+
882+ /** Loads (or refreshes) the current session. Returns false if session is not recorded. */
883+ private _loadAndCheckSession ( expiry ?: number ) : boolean {
884+ this . _loadSession ( expiry ) ;
885+
886+ // We know this is set, because it is always set in _loadSession
887+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
888+ if ( ! this . session ! . sampled ) {
889+ this . stop ( ) ;
890+ return false ;
891+ }
892+
893+ return true ;
894+ }
894895}
0 commit comments