|
| 1 | +import type { |
| 2 | + Configuration, |
| 3 | + DeflateEncoderStreamId, |
| 4 | + Encoder, |
| 5 | + EndpointBuilder, |
| 6 | + TrackingConsentState, |
| 7 | +} from '@datadog/browser-core' |
| 8 | +import { |
| 9 | + abstractHooks, |
| 10 | + buildAccountContextManager, |
| 11 | + buildGlobalContextManager, |
| 12 | + buildUserContextManager, |
| 13 | + createBatch, |
| 14 | + createFlushController, |
| 15 | + createHttpRequest, |
| 16 | + createIdentityEncoder, |
| 17 | + createPageMayExitObservable, |
| 18 | + createTrackingConsentState, |
| 19 | + startSessionManager, |
| 20 | + startTelemetry, |
| 21 | + TelemetryService, |
| 22 | + validateAndBuildConfiguration, |
| 23 | +} from '@datadog/browser-core' |
| 24 | +import type { CoreInitializeConfiguration, CoreSessionManager } from '@datadog/browser-internal-next' |
| 25 | +import { CoreContextType, getInternalApi, MessageType } from '@datadog/browser-internal-next' |
| 26 | +import { SessionReplayState } from '@datadog/browser-rum-core' |
| 27 | + |
| 28 | +export function initialize( |
| 29 | + initializeConfiguration: CoreInitializeConfiguration, |
| 30 | + createEncoder: (streamId: DeflateEncoderStreamId) => Encoder = createIdentityEncoder |
| 31 | +) { |
| 32 | + const configuration = validateAndBuildConfiguration(initializeConfiguration) |
| 33 | + if (!configuration) { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + const internalApi = getInternalApi() |
| 38 | + const hooks = abstractHooks() // TODO: specialized hook |
| 39 | + const contexts = startContexts() |
| 40 | + |
| 41 | + const reportError = () => { |
| 42 | + // TODO |
| 43 | + } |
| 44 | + const pageMayExitObservable = createPageMayExitObservable(configuration) |
| 45 | + const telemetry = startTelemetry( |
| 46 | + TelemetryService.RUM, |
| 47 | + configuration, |
| 48 | + hooks, |
| 49 | + reportError, |
| 50 | + pageMayExitObservable, |
| 51 | + createIdentityEncoder // Keep using the identity encoder here, so we can sent telemetry even if deflate isn't working |
| 52 | + ) |
| 53 | + |
| 54 | + const trackingConsentState = createTrackingConsentState() |
| 55 | + |
| 56 | + // TODO: handle bridge |
| 57 | + |
| 58 | + const sessionManager = startCoreSessionManager(configuration, trackingConsentState) |
| 59 | + |
| 60 | + return { |
| 61 | + coreInitializeConfiguration: initializeConfiguration, |
| 62 | + createEncoder, |
| 63 | + internalApi, |
| 64 | + hooks, |
| 65 | + contexts, |
| 66 | + telemetry, |
| 67 | + sessionManager, |
| 68 | + createBatch: (endpoints: EndpointBuilder[]) => |
| 69 | + createBatch({ |
| 70 | + encoder: createEncoder( |
| 71 | + Math.random() as any // TODO: remove named stream id |
| 72 | + ), |
| 73 | + request: createHttpRequest(endpoints, reportError), |
| 74 | + flushController: createFlushController({ |
| 75 | + pageMayExitObservable, |
| 76 | + sessionExpireObservable: sessionManager.expireObservable, |
| 77 | + }), |
| 78 | + }), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function startContexts() { |
| 83 | + const global = buildGlobalContextManager() |
| 84 | + const user = buildUserContextManager() |
| 85 | + const account = buildAccountContextManager() |
| 86 | + |
| 87 | + const contextsByType = { |
| 88 | + [CoreContextType.GLOBAL]: global, |
| 89 | + [CoreContextType.USER]: user, |
| 90 | + [CoreContextType.ACCOUNT]: account, |
| 91 | + } |
| 92 | + |
| 93 | + getInternalApi().bus.subscribe(({ message }) => { |
| 94 | + switch (message.type) { |
| 95 | + case MessageType.CORE_SET_CONTEXT: |
| 96 | + contextsByType[message.context].setContext(message.value) |
| 97 | + break |
| 98 | + |
| 99 | + case MessageType.CORE_SET_CONTEXT_PROPERTY: |
| 100 | + contextsByType[message.context].setContextProperty(message.key, message.value) |
| 101 | + break |
| 102 | + |
| 103 | + case MessageType.CORE_CLEAR_CONTEXT: |
| 104 | + contextsByType[message.context].clearContext() |
| 105 | + break |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + return { |
| 110 | + global, |
| 111 | + user, |
| 112 | + account, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function startCoreSessionManager( |
| 117 | + configuration: Configuration, |
| 118 | + trackingConsentState: TrackingConsentState |
| 119 | +): CoreSessionManager { |
| 120 | + // TODO: we should use a fallback if: |
| 121 | + // * there is an event bridge |
| 122 | + // * configuration.sessionStoreStrategyType is undefined (ex: no cookie access) |
| 123 | + |
| 124 | + const sessionManager = startSessionManager<string>( |
| 125 | + configuration, |
| 126 | + // TODO: product type will be removed in the future session manager |
| 127 | + 'x', |
| 128 | + () => 'x', |
| 129 | + trackingConsentState |
| 130 | + ) |
| 131 | + |
| 132 | + sessionManager.sessionStateUpdateObservable.subscribe(({ previousState, newState }) => { |
| 133 | + if (!previousState.forcedReplay && newState.forcedReplay) { |
| 134 | + const sessionEntity = sessionManager.findSession() |
| 135 | + if (sessionEntity) { |
| 136 | + sessionEntity.isReplayForced = true |
| 137 | + } |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + return { |
| 142 | + ...sessionManager, |
| 143 | + findTrackedSession: (startTime) => { |
| 144 | + const session = sessionManager.findSession(startTime) |
| 145 | + if (!session) { |
| 146 | + return |
| 147 | + } |
| 148 | + return { |
| 149 | + id: session.id, |
| 150 | + sessionReplay: SessionReplayState.OFF, // TODO |
| 151 | + anonymousId: session.anonymousId, |
| 152 | + } |
| 153 | + }, |
| 154 | + setForcedReplay: () => sessionManager.updateSessionState({ forcedReplay: '1' }), |
| 155 | + } |
| 156 | +} |
0 commit comments