Skip to content

Commit

Permalink
Move createPreStartStrategy in its own module
Browse files Browse the repository at this point in the history
Extracted helper functions to check recorder status
  • Loading branch information
amortemousque committed Nov 29, 2024
1 parent efed90d commit e103093
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
40 changes: 25 additions & 15 deletions packages/rum/src/boot/postStartStrategy.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
34 changes: 34 additions & 0 deletions packages/rum/src/boot/preStartStrategy.ts
Original file line number Diff line number Diff line change
@@ -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)
)
},
}
}
34 changes: 4 additions & 30 deletions packages/rum/src/boot/recorderApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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,
}
}

0 comments on commit e103093

Please sign in to comment.