-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathloadOrCreateSession.ts
38 lines (34 loc) · 1.23 KB
/
loadOrCreateSession.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { DEBUG_BUILD } from '../debug-build';
import type { Session, SessionOptions } from '../types';
import { logger } from '../util/logger';
import { createSession } from './createSession';
import { fetchSession } from './fetchSession';
import { shouldRefreshSession } from './shouldRefreshSession';
/**
* Get or create a session, when initializing the replay.
* Returns a session that may be unsampled.
*/
export function loadOrCreateSession(
{
sessionIdleExpire,
maxReplayDuration,
previousSessionId,
}: {
sessionIdleExpire: number;
maxReplayDuration: number;
previousSessionId?: string;
},
sessionOptions: SessionOptions,
): Session {
const existingSession = sessionOptions.stickySession && fetchSession();
// No session exists yet, just create a new one
if (!existingSession) {
DEBUG_BUILD && logger.infoTick('Creating new session');
return createSession(sessionOptions, { previousSessionId });
}
if (!shouldRefreshSession(existingSession, { sessionIdleExpire, maxReplayDuration })) {
return existingSession;
}
DEBUG_BUILD && logger.infoTick('Session in sessionStorage is expired, creating new one...');
return createSession(sessionOptions, { previousSessionId: existingSession.id });
}