Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/DdSdkReactNative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class DdSdkReactNative {
await DdSdkReactNative.initializeNativeSDK(configuration, {
initializationModeForTelemetry: 'LEGACY'
});

DdSdkReactNative.enableFeatures(configuration);
};

Expand All @@ -77,6 +78,7 @@ export class DdSdkReactNative {
}
): Promise<void> => {
registerRumSessionIdListener();

if (GlobalState.instance.isInitialized) {
InternalLog.log(
"Can't initialize Datadog, SDK was already initialized",
Expand Down
29 changes: 21 additions & 8 deletions packages/core/src/rum/sessionId/sessionIdHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable global-require */
import { NativeModules } from 'react-native';

import { createNativeEventEmitterForModule } from '../../sdk/EventEmitter/EventEmitter';
import type { RumSessionStartedEvent } from '../../specs/NativeDdSdk';

const RUM_SESSION_STARTED_EVENT_KEY = 'RumSessionStarted';

Expand All @@ -17,16 +20,26 @@ let cachedRumSessionId: string | null;
* the cached Session ID.
*/
export const registerRumSessionIdListener = () => {
const eventEmitter = createNativeEventEmitterForModule(NativeModules.DdSdk);
if (eventEmitter.listenerCount(RUM_SESSION_STARTED_EVENT_KEY) > 0) {
return;
const DdSdkModule =
NativeModules.DdSdk || require('../../specs/NativeDdSdk').default;
const eventEmitter = createNativeEventEmitterForModule(DdSdkModule);
const listenerCount = eventEmitter.listenerCount(
RUM_SESSION_STARTED_EVENT_KEY
);

// Always remove old listeners to avoid potential issues with invalid bridge references
if (listenerCount > 0) {
Comment thread
sbarrio marked this conversation as resolved.
removeRumSessionIdListeners();
}

eventEmitter.addListener(RUM_SESSION_STARTED_EVENT_KEY, (event: any) => {
const field = 'sessionId';
const sessionId = event[field] as string | null;
cachedRumSessionId = sessionId;
});
eventEmitter.addListener(
RUM_SESSION_STARTED_EVENT_KEY,
(event: RumSessionStartedEvent) => {
const field: keyof RumSessionStartedEvent = 'sessionId';
const sessionId = event[field];
cachedRumSessionId = sessionId;
}
);
};

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/specs/NativeDdSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { TurboModuleRegistry } from 'react-native';
/**
* Do not import this Spec directly, use DdNativeSdkType instead.
*/

export type RumSessionStartedEvent = {
sessionId: string | null;
};

export interface Spec extends TurboModule {
readonly getConstants: () => {};

Expand Down Expand Up @@ -81,6 +86,7 @@ export interface Spec extends TurboModule {
* https://github.com/react-native-community/RNNewArchitectureLibraries/tree/feat/swift-event-emitter?tab=readme-ov-file#codegen-update-codegen-specs)
*/
addListener: (eventType: string) => void;

removeListeners: (count: number) => void;
}

Expand Down
Loading