diff --git a/packages/rum/src/boot/postStartStrategy.ts b/packages/rum/src/boot/postStartStrategy.ts index f1555b9e14..a8948f7f30 100644 --- a/packages/rum/src/boot/postStartStrategy.ts +++ b/packages/rum/src/boot/postStartStrategy.ts @@ -1,12 +1,12 @@ -import { - LifeCycleEventType, - SessionReplayState, - type LifeCycle, - type RumConfiguration, - type RumSessionManager, - type StartRecordingOptions, - type ViewHistory, +import type { + LifeCycle, + RumConfiguration, + RumSessionManager, + StartRecordingOptions, + ViewHistory, + RumSession, } from '@datadog/browser-rum-core' +import { LifeCycleEventType, SessionReplayState } from '@datadog/browser-rum-core' import { PageExitReason, runOnReadyState, type DeflateEncoder } from '@datadog/browser-core' import { getSessionReplayLink } from '../domain/getSessionReplayLink' import type { startRecording } from './startRecording' @@ -64,12 +64,12 @@ export function createPostStartStrategy( function start(options?: StartRecordingOptions) { const session = sessionManager.findTrackedSession() - if (!session || (session.sessionReplay === SessionReplayState.OFF && (!options || !options.force))) { + if (canStartRecording(session, options)) { status = RecorderStatus.IntentToStart return } - if (status === RecorderStatus.Starting || status === RecorderStatus.Started) { + if (isRecordingInProgress(status)) { return } @@ -97,18 +97,16 @@ export function createPostStartStrategy( status = RecorderStatus.Started }) - if (options && options.force && session.sessionReplay === SessionReplayState.OFF) { + if (shouldForceReplay(session!, options)) { sessionManager.setForcedReplay() } } function stop() { - if (status === RecorderStatus.Stopped) { - return + if (status !== RecorderStatus.Stopped && status === RecorderStatus.Started) { + stopRecording?.() } - stopRecording?.() - status = RecorderStatus.Stopped } @@ -122,3 +120,15 @@ export function createPostStartStrategy( isRecording: () => status === RecorderStatus.Started, } } + +function canStartRecording(session: RumSession | undefined, options?: StartRecordingOptions) { + return !session || (session.sessionReplay === SessionReplayState.OFF && (!options || !options.force)) +} + +function isRecordingInProgress(status: RecorderStatus) { + return status === RecorderStatus.Starting || status === RecorderStatus.Started +} + +function shouldForceReplay(session: RumSession, options?: StartRecordingOptions) { + return options && options.force && session.sessionReplay === SessionReplayState.OFF +} diff --git a/packages/rum/src/boot/preStartStrategy.ts b/packages/rum/src/boot/preStartStrategy.ts new file mode 100644 index 0000000000..bc728523bf --- /dev/null +++ b/packages/rum/src/boot/preStartStrategy.ts @@ -0,0 +1,34 @@ +import { noop } from '@datadog/browser-core' +import type { RumConfiguration } from '@datadog/browser-rum-core' +import type { Strategy } from './postStartStrategy' + +const enum PreStartRecorderStatus { + None, + HadManualStart, + HadManualStop, +} + +export function createPreStartStrategy(): { + strategy: Strategy + shouldStartImmediately: (configuration: RumConfiguration) => boolean +} { + let status = PreStartRecorderStatus.None + return { + strategy: { + start() { + status = PreStartRecorderStatus.HadManualStart + }, + stop() { + status = PreStartRecorderStatus.HadManualStop + }, + isRecording: () => false, + getSessionReplayLink: noop as () => string | undefined, + }, + shouldStartImmediately(configuration) { + return ( + status === PreStartRecorderStatus.HadManualStart || + (status === PreStartRecorderStatus.None && !configuration.startSessionReplayRecordingManually) + ) + }, + } +} diff --git a/packages/rum/src/boot/recorderApi.ts b/packages/rum/src/boot/recorderApi.ts index 84c798e745..ca0d7133b9 100644 --- a/packages/rum/src/boot/recorderApi.ts +++ b/packages/rum/src/boot/recorderApi.ts @@ -23,8 +23,9 @@ import { startDeflateWorker, } from '../domain/deflate' import { isBrowserSupported } from './isBrowserSupported' -import type { StartRecording, Strategy } from './postStartStrategy' +import type { StartRecording } from './postStartStrategy' import { createPostStartStrategy } from './postStartStrategy' +import { createPreStartStrategy } from './preStartStrategy' export function makeRecorderApi( startRecordingImpl: StartRecording, @@ -42,7 +43,7 @@ export function makeRecorderApi( } // eslint-disable-next-line prefer-const - let { strategy, getStatus: getPreStartStatus } = createPreStartStrategy() + let { strategy, shouldStartImmediately } = createPreStartStrategy() return { start: (options?: StartRecordingOptions) => strategy.start(options), @@ -114,35 +115,8 @@ export function makeRecorderApi( getOrCreateDeflateEncoder ) - const preStartStatus = getPreStartStatus() - if ( - preStartStatus === PreStartRecorderStatus.HadManualStart || - (preStartStatus === PreStartRecorderStatus.None && !configuration.startSessionReplayRecordingManually) - ) { + if (shouldStartImmediately(configuration)) { strategy.start() } } } - -const enum PreStartRecorderStatus { - None, - HadManualStart, - HadManualStop, -} - -export function createPreStartStrategy(): { strategy: Strategy; getStatus: () => PreStartRecorderStatus } { - let status = PreStartRecorderStatus.None - return { - strategy: { - start() { - status = PreStartRecorderStatus.HadManualStart - }, - stop() { - status = PreStartRecorderStatus.HadManualStop - }, - getSessionReplayLink: noop as () => string | undefined, - isRecording: () => false, - }, - getStatus: () => status, - } -}