@datadog/mobile-react-native 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/DatadogSDKReactNative.podspec +7 -7
  2. package/android/src/main/kotlin/com/datadog/reactnative/DdRumImplementation.kt +4 -0
  3. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt +9 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkNativeInitialization.kt +13 -6
  5. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkSessionStartedListener.kt +93 -22
  6. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  7. package/android/src/newarch/kotlin/com/datadog/reactnative/DdRum.kt +5 -1
  8. package/android/src/oldarch/kotlin/com/datadog/reactnative/DdRum.kt +5 -1
  9. package/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt +9 -1
  10. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkNativeInitializationTest.kt +68 -0
  11. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkSessionStartedListenerTest.kt +38 -1
  12. package/ios/Sources/DdRum.mm +6 -4
  13. package/ios/Sources/DdRumImplementation.swift +82 -7
  14. package/ios/Sources/DdSdkNativeInitialization.swift +24 -17
  15. package/ios/Sources/DdSdkSessionStartedListener.swift +28 -1
  16. package/ios/Sources/RNViewResolution.swift +88 -0
  17. package/ios/Sources/RUMMonitorInternalProtocol.swift +8 -0
  18. package/ios/Sources/SdkVersion.swift +1 -1
  19. package/lib/commonjs/DdSdkReactNative.js +16 -3
  20. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  21. package/lib/commonjs/config/DatadogProviderConfiguration.js +3 -1
  22. package/lib/commonjs/config/DatadogProviderConfiguration.js.map +1 -1
  23. package/lib/commonjs/metro/debugIdResolver.js +5 -3
  24. package/lib/commonjs/metro/debugIdResolver.js.map +1 -1
  25. package/lib/commonjs/metro/plugin/metroSerializer.js +4 -3
  26. package/lib/commonjs/metro/plugin/metroSerializer.js.map +1 -1
  27. package/lib/commonjs/rum/DdRum.js +14 -1
  28. package/lib/commonjs/rum/DdRum.js.map +1 -1
  29. package/lib/commonjs/rum/eventMappers/actionEventMapper.js.map +1 -1
  30. package/lib/commonjs/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js +1 -1
  31. package/lib/commonjs/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js.map +1 -1
  32. package/lib/commonjs/sdk/AttributesEncoding/defaultEncoders.js +6 -1
  33. package/lib/commonjs/sdk/AttributesEncoding/defaultEncoders.js.map +1 -1
  34. package/lib/commonjs/specs/NativeDdRum.js.map +1 -1
  35. package/lib/commonjs/version.js +1 -1
  36. package/lib/module/DdSdkReactNative.js +16 -3
  37. package/lib/module/DdSdkReactNative.js.map +1 -1
  38. package/lib/module/config/DatadogProviderConfiguration.js +3 -1
  39. package/lib/module/config/DatadogProviderConfiguration.js.map +1 -1
  40. package/lib/module/metro/debugIdResolver.js +5 -3
  41. package/lib/module/metro/debugIdResolver.js.map +1 -1
  42. package/lib/module/metro/plugin/metroSerializer.js +4 -3
  43. package/lib/module/metro/plugin/metroSerializer.js.map +1 -1
  44. package/lib/module/rum/DdRum.js +14 -1
  45. package/lib/module/rum/DdRum.js.map +1 -1
  46. package/lib/module/rum/eventMappers/actionEventMapper.js.map +1 -1
  47. package/lib/module/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js +1 -1
  48. package/lib/module/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js.map +1 -1
  49. package/lib/module/sdk/AttributesEncoding/defaultEncoders.js +6 -1
  50. package/lib/module/sdk/AttributesEncoding/defaultEncoders.js.map +1 -1
  51. package/lib/module/specs/NativeDdRum.js.map +1 -1
  52. package/lib/module/version.js +1 -1
  53. package/lib/typescript/DdSdkReactNative.d.ts.map +1 -1
  54. package/lib/typescript/config/DatadogProviderConfiguration.d.ts +3 -1
  55. package/lib/typescript/config/DatadogProviderConfiguration.d.ts.map +1 -1
  56. package/lib/typescript/metro/debugIdResolver.d.ts.map +1 -1
  57. package/lib/typescript/metro/plugin/metroSerializer.d.ts +1 -1
  58. package/lib/typescript/metro/plugin/metroSerializer.d.ts.map +1 -1
  59. package/lib/typescript/nativeModulesTypes.d.ts +2 -1
  60. package/lib/typescript/nativeModulesTypes.d.ts.map +1 -1
  61. package/lib/typescript/rum/DdRum.d.ts.map +1 -1
  62. package/lib/typescript/rum/eventMappers/actionEventMapper.d.ts +1 -0
  63. package/lib/typescript/rum/eventMappers/actionEventMapper.d.ts.map +1 -1
  64. package/lib/typescript/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.d.ts.map +1 -1
  65. package/lib/typescript/sdk/AttributesEncoding/defaultEncoders.d.ts.map +1 -1
  66. package/lib/typescript/specs/NativeDdRum.d.ts +2 -1
  67. package/lib/typescript/specs/NativeDdRum.d.ts.map +1 -1
  68. package/lib/typescript/version.d.ts +1 -1
  69. package/package.json +2 -2
  70. package/src/DdSdkReactNative.tsx +18 -2
  71. package/src/__tests__/DdSdkReactNative.test.tsx +1 -0
  72. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +8 -0
  73. package/src/config/DatadogProviderConfiguration.ts +3 -1
  74. package/src/metro/__tests__/metro.test.ts +13 -0
  75. package/src/metro/debugIdResolver.ts +7 -3
  76. package/src/metro/plugin/metroSerializer.ts +11 -3
  77. package/src/nativeModulesTypes.ts +2 -0
  78. package/src/rum/DdRum.ts +25 -0
  79. package/src/rum/__tests__/DdRum.test.ts +33 -0
  80. package/src/rum/eventMappers/actionEventMapper.ts +1 -0
  81. package/src/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.ts +2 -1
  82. package/src/rum/instrumentation/interactionTracking/__tests__/DdBabelInteractionTracking.test.ts +28 -0
  83. package/src/sdk/AttributesEncoding/__tests__/defaultEncoders.test.ts +25 -1
  84. package/src/sdk/AttributesEncoding/defaultEncoders.ts +11 -1
  85. package/src/sdk/DatadogProvider/__tests__/__utils__/renderWithProvider.tsx +24 -7
  86. package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +19 -15
  87. package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +85 -54
  88. package/src/specs/NativeDdRum.ts +2 -0
  89. package/src/version.ts +1 -1
@@ -19,15 +19,15 @@ Pod::Spec.new do |s|
19
19
  s.dependency "React-Core"
20
20
 
21
21
  # /!\ Remember to keep the versions in sync with DatadogSDKReactNativeSessionReplay.podspec
22
- s.dependency 'DatadogCore', '3.8.2'
23
- s.dependency 'DatadogLogs', '3.8.2'
24
- s.dependency 'DatadogTrace', '3.8.2'
25
- s.dependency 'DatadogRUM', '3.8.2'
26
- s.dependency 'DatadogCrashReporting', '3.8.2'
27
- s.dependency 'DatadogFlags', '3.8.2'
22
+ s.dependency 'DatadogCore', '3.10.0'
23
+ s.dependency 'DatadogLogs', '3.10.0'
24
+ s.dependency 'DatadogTrace', '3.10.0'
25
+ s.dependency 'DatadogRUM', '3.10.0'
26
+ s.dependency 'DatadogCrashReporting', '3.10.0'
27
+ s.dependency 'DatadogFlags', '3.10.0'
28
28
 
29
29
  # DatadogWebViewTracking is not available for tvOS
30
- s.ios.dependency 'DatadogWebViewTracking', '3.8.2'
30
+ s.ios.dependency 'DatadogWebViewTracking', '3.10.0'
31
31
 
32
32
  s.test_spec 'Tests' do |test_spec|
33
33
  test_spec.source_files = 'ios/Tests/**/*.{swift,json}'
@@ -119,12 +119,16 @@ class DdRumImplementation(private val datadog: DatadogWrapper = DatadogSDKWrappe
119
119
  * Add a RUM Action.
120
120
  * @param type The action type (tap, scroll, swipe, click, custom).
121
121
  * @param name The action name.
122
+ * @param touch The native touch data for tap actions, or null for other action types.
123
+ * Currently unused on Android; reserved for heatmap support.
122
124
  * @param context The additional context to send.
123
125
  * @param timestampMs The timestamp when the action occurred (in milliseconds). If not provided, current timestamp will be used.
124
126
  */
127
+ @Suppress("LongParameterList", "UnusedParameter")
125
128
  fun addAction(
126
129
  type: String,
127
130
  name: String,
131
+ touch: ReadableMap?,
128
132
  context: ReadableMap,
129
133
  timestampMs: Double,
130
134
  promise: Promise
@@ -45,6 +45,15 @@ class DdSdkImplementation(
45
45
  fun initialize(configuration: ReadableMap, promise: Promise) {
46
46
  val ddSdkConfiguration = configuration.asDdSdkConfiguration()
47
47
 
48
+ // On new arch DdSdk is a lazy TurboModule — it's instantiated on the first JS-side
49
+ // method call (typically this initialize()), which usually lands AFTER the activity's
50
+ // first onHostResume. That means registerLifecycleEvents's listener missed the first
51
+ // resume, reactContext is still null on the session listener, and the replay inside
52
+ // nativeInitialization.initialize → onRnSdkInitialized has no emitter target. Setting
53
+ // the reactContext here — we are provably being dispatched through an active one —
54
+ // closes that race so the cached session ID is delivered on the first JS init.
55
+ DdSdkSessionStartedListener.getInstance().setReactContext(reactContext)
56
+
48
57
  val nativeInitialization = DdSdkNativeInitialization(appContext, datadog, ddTelemetry)
49
58
  nativeInitialization.initialize(ddSdkConfiguration)
50
59
 
@@ -47,7 +47,7 @@ class DdSdkNativeInitialization internal constructor(
47
47
  private val jsonFileReader: JSONFileReader = JSONFileReader()
48
48
  ) {
49
49
  @Suppress("CyclomaticComplexMethod")
50
- internal fun initialize(ddSdkConfiguration: DdSdkConfiguration) {
50
+ internal fun initialize(ddSdkConfiguration: DdSdkConfiguration, isCalledFromJs: Boolean = true) {
51
51
  val sdkConfiguration = buildSdkConfiguration(ddSdkConfiguration)
52
52
  val trackingConsent = buildTrackingConsent(ddSdkConfiguration.trackingConsent)
53
53
  var rumConfiguration: RumConfiguration? = null
@@ -71,10 +71,14 @@ class DdSdkNativeInitialization internal constructor(
71
71
 
72
72
  configureRumAndTracesForLogs(ddSdkConfiguration)
73
73
 
74
- if (datadog.isInitialized()) {
75
- datadog.getRumMonitor().getCurrentSessionId {
76
- it?.let { sessionId ->
77
- DdSdkSessionStartedListener.getInstance().onSessionStarted(sessionId, false)
74
+ if (isCalledFromJs) {
75
+ DdSdkSessionStartedListener.getInstance().onRnSdkInitialized()
76
+ // Handles the case in which the SDK was already initialized with initFromNative.
77
+ if (datadog.isInitialized()) {
78
+ datadog.getRumMonitor().getCurrentSessionId {
79
+ it?.let { sessionId ->
80
+ DdSdkSessionStartedListener.getInstance().onSessionStarted(sessionId, false)
81
+ }
78
82
  }
79
83
  }
80
84
  }
@@ -413,7 +417,10 @@ class DdSdkNativeInitialization internal constructor(
413
417
  fun initFromNative(appContext: Context) {
414
418
  val nativeInitialization = DdSdkNativeInitialization(appContext.applicationContext)
415
419
  try {
416
- nativeInitialization.initialize(nativeInitialization.getConfigurationFromJSONFile())
420
+ nativeInitialization.initialize(
421
+ ddSdkConfiguration = nativeInitialization.getConfigurationFromJSONFile(),
422
+ isCalledFromJs = false
423
+ )
417
424
  } catch (@Suppress("TooGenericExceptionCaught") error: Exception) {
418
425
  Log.w(
419
426
  DdSdkNativeInitialization::class.java.canonicalName,
@@ -14,13 +14,27 @@ import com.facebook.react.modules.core.DeviceEventManagerModule
14
14
  import org.jetbrains.annotations.TestOnly
15
15
 
16
16
 
17
- internal class DdSdkSessionStartedListener private constructor(): RumSessionListener {
17
+ internal class DdSdkSessionStartedListener private constructor() : RumSessionListener {
18
18
  companion object {
19
+ // JS-side callable module registered via BatchedBridge.registerCallableModule.
19
20
  private const val BRIDGE_MODULE_NAME = "DatadogInternalReactBridge"
20
21
  private const val BRIDGE_MODULE_METHOD = "__datadogOnMessageReceived"
21
22
 
22
23
  private var instance: DdSdkSessionStartedListener? = null
23
24
 
25
+ // Process-level state — tracks whether JS initialize() has ever run since the
26
+ // app process started. Kept outside the instance on purpose: the listener
27
+ // singleton is recreated on every onHostPause/onHostDestroy via invalidate(),
28
+ // but the JS bridge module registration survives activity lifecycle events, so
29
+ // this flag must survive them too.
30
+ private var isRnSdkInitialized: Boolean = false
31
+
32
+ // Routes UI-thread dispatch through an injectable abstraction so JVM unit tests
33
+ // can use TestUiThreadExecutor (synchronous, no main looper required) instead of
34
+ // the real UiThreadUtil.
35
+ private var uiThreadExecutor: UiThreadExecutor = ReactUiThreadExecutor()
36
+
37
+ // Returns the shared listener instance, creating it on first call.
24
38
  fun getInstance(): DdSdkSessionStartedListener {
25
39
  if (instance == null) {
26
40
  instance = DdSdkSessionStartedListener()
@@ -28,26 +42,67 @@ internal class DdSdkSessionStartedListener private constructor(): RumSessionList
28
42
  return instance!!
29
43
  }
30
44
 
45
+ // Resets the singleton — used in tests and on host pause/destroy to release the
46
+ // React context reference. Does NOT reset isRnSdkInitialized, which tracks
47
+ // process-level JS-bridge registration state.
31
48
  fun invalidate() {
32
49
  instance = null
33
50
  }
51
+
52
+ @TestOnly
53
+ fun resetIsRnSdkInitialized() {
54
+ isRnSdkInitialized = false
55
+ }
56
+
57
+ @TestOnly
58
+ fun isRnSdkInitializedForTests(): Boolean = isRnSdkInitialized
34
59
  }
35
60
 
36
- private var reactContext: ReactContext? = null
61
+ // Cached so it can be delivered once the bridge becomes available.
37
62
  private var lastSessionId: String? = null
63
+
64
+ // Set from onHostResume; null until the React activity is first resumed.
65
+ private var reactContext: ReactContext? = null
66
+ // Overridable in tests — NativeArray cannot be instantiated without the native SO.
38
67
  private var convertToNativeArray: ((array: Array<String>) -> NativeArray?)? = null
39
- private var exceptionHandler: ((error:Exception)->Unit)? = null
68
+ // Overridable in tests to assert on bridge exceptions without crashing.
69
+ private var exceptionHandler: ((error: Exception) -> Unit)? = null
70
+ // Lazily resolved from BuildConfig; overridable in tests.
40
71
  private var isNewArchitecture: Boolean? = null
41
72
 
73
+ // Stores the session ID and attempts immediate delivery.
42
74
  override fun onSessionStarted(sessionId: String, isDiscarded: Boolean) {
43
- sendSessionStartedToJS(sessionId)
75
+ this.lastSessionId = sessionId
76
+ trySendSessionStartedToJS(sessionId)
44
77
  }
45
78
 
79
+ // Stores the React context and schedules a catch-up delivery for any cached session
80
+ // ID on the UI thread. Called from:
81
+ // - DdSdk#onHostResume (both architectures) via the lifecycle listener
82
+ // - DdSdkImplementation#initialize (new-arch race fix — the TurboModule is lazy-
83
+ // instantiated, so the first onHostResume may have fired before its lifecycle
84
+ // listener was registered; setting the context here guarantees a non-null
85
+ // target for onRnSdkInitialized's replay)
86
+ //
87
+ // Delivery is dispatched to the UI thread because sendSessionIdWithBridge and
88
+ // sendSessionIdWithEventEmitter are @MainThread. The field assignment stays
89
+ // synchronous so callers that rely on reactContext being set immediately after
90
+ // this call (e.g. a same-stack onRnSdkInitialized) see the new value.
46
91
  fun setReactContext(reactContext: ReactContext) {
47
92
  this.reactContext = reactContext
48
- if (hasValidBridge()) {
49
- this.lastSessionId?.let { sendSessionStartedToJS(it) }
50
- }
93
+ val cached = this.lastSessionId ?: return
94
+ uiThreadExecutor.runOnUiThread { trySendSessionStartedToJS(cached) }
95
+ }
96
+
97
+ // Called when the RN SDK is initialized from JS for the first time while the native
98
+ // SDK was already running. At this point DatadogInternalReactBridge is guaranteed to
99
+ // be registered, so it is safe to deliver any session ID that was stored before the
100
+ // React bridge was available. Delivery is posted to the UI thread for the same
101
+ // reason as setReactContext.
102
+ fun onRnSdkInitialized() {
103
+ isRnSdkInitialized = true
104
+ val cached = this.lastSessionId ?: return
105
+ uiThreadExecutor.runOnUiThread { trySendSessionStartedToJS(cached) }
51
106
  }
52
107
 
53
108
  @TestOnly
@@ -65,14 +120,28 @@ internal class DdSdkSessionStartedListener private constructor(): RumSessionList
65
120
  this.isNewArchitecture = isNewArch
66
121
  }
67
122
 
123
+ @TestOnly
124
+ fun setIsRnSdkInitialized(value: Boolean) {
125
+ isRnSdkInitialized = value
126
+ }
127
+
128
+ @TestOnly
129
+ fun setUiThreadExecutor(executor: UiThreadExecutor) {
130
+ uiThreadExecutor = executor
131
+ }
132
+
133
+ // Returns true only when it is safe to call callFunction on the JS thread.
68
134
  private fun hasValidBridge(): Boolean {
69
135
  val context = reactContext ?: return false
70
136
  val instance = context.catalystInstance ?: return false
71
137
  return !isNewArchitecture() &&
72
138
  !instance.isDestroyed &&
73
- context.hasActiveReactInstance()
139
+ context.hasActiveReactInstance() &&
140
+ isRnSdkInitialized
74
141
  }
75
142
 
143
+ // Lazily cached — BuildConfig is a compile-time constant but reading it through the
144
+ // nullable override lets tests inject the value without reflection.
76
145
  private fun isNewArchitecture(): Boolean {
77
146
  isNewArchitecture?.let { return it }
78
147
  BuildConfig.IS_NEW_ARCHITECTURE_ENABLED.let {
@@ -81,8 +150,8 @@ internal class DdSdkSessionStartedListener private constructor(): RumSessionList
81
150
  }
82
151
  }
83
152
 
84
- private fun sendSessionStartedToJS(sessionId: String) {
85
- this.lastSessionId = sessionId
153
+ // Routes delivery to the appropriate path based on arch and bridge readiness.
154
+ private fun trySendSessionStartedToJS(sessionId: String) {
86
155
  if (hasValidBridge()) {
87
156
  sendSessionIdWithBridge(sessionId)
88
157
  } else {
@@ -90,30 +159,32 @@ internal class DdSdkSessionStartedListener private constructor(): RumSessionList
90
159
  }
91
160
  }
92
161
 
162
+ // Old-arch delivery path via BatchedBridge.
93
163
  @MainThread
94
164
  private fun sendSessionIdWithBridge(sessionId: String) {
95
165
  @Suppress("TooGenericExceptionCaught")
96
166
  try {
97
- val args = arrayOf(sessionId)
98
- val nativeArray = if (convertToNativeArray != null) {
99
- convertToNativeArray?.invoke(args)
100
- } else {
101
- WritableNativeArray().apply {
102
- pushString("RUMSessionStarted")
103
- pushString(sessionId)
104
- }
105
- }
106
-
167
+ val args = arrayOf("RUMSessionStarted", sessionId)
107
168
  reactContext?.catalystInstance?.callFunction(
108
169
  BRIDGE_MODULE_NAME,
109
170
  BRIDGE_MODULE_METHOD,
110
- nativeArray
171
+ buildBridgeArgs(args)
111
172
  )
112
- } catch(err: Exception) {
173
+ } catch (err: Exception) {
113
174
  exceptionHandler?.invoke(err)
114
175
  }
115
176
  }
116
177
 
178
+ private fun buildBridgeArgs(args: Array<String>): NativeArray? {
179
+ if (convertToNativeArray != null) {
180
+ return convertToNativeArray?.invoke(args)
181
+ }
182
+ return WritableNativeArray().apply {
183
+ args.forEach { pushString(it) }
184
+ }
185
+ }
186
+
187
+ // New-arch delivery path and fallback when the bridge is not active.
117
188
  @MainThread
118
189
  private fun sendSessionIdWithEventEmitter(sessionId: String) {
119
190
  val context = reactContext ?: return
@@ -7,4 +7,4 @@
7
7
  package com.datadog.reactnative
8
8
 
9
9
  // This is automatically updated by the update-version.sh script
10
- internal const val SDK_VERSION = "3.3.0"
10
+ internal const val SDK_VERSION = "3.4.0"
@@ -104,19 +104,23 @@ class DdRum(
104
104
  * Add a RUM Action.
105
105
  * @param type The action type (tap, scroll, swipe, click, custom).
106
106
  * @param name The action name.
107
+ * @param touch The native touch data for tap actions, or null for other action types.
108
+ * Currently unused on Android; reserved for heatmap support.
107
109
  * @param context The additional context to send.
108
110
  * @param timestampMs The timestamp when the action occurred (in milliseconds).
109
111
  * If not provided, current timestamp will be used.
110
112
  */
113
+ @Suppress("LongParameterList")
111
114
  @ReactMethod
112
115
  override fun addAction(
113
116
  type: String,
114
117
  name: String,
118
+ touch: ReadableMap?,
115
119
  context: ReadableMap,
116
120
  timestampMs: Double,
117
121
  promise: Promise
118
122
  ) {
119
- implementation.addAction(type, name, context, timestampMs, promise)
123
+ implementation.addAction(type, name, touch, context, timestampMs, promise)
120
124
  }
121
125
 
122
126
  /**
@@ -97,18 +97,22 @@ class DdRum(
97
97
  * Add a RUM Action.
98
98
  * @param type The action type (tap, scroll, swipe, click, custom).
99
99
  * @param name The action name.
100
+ * @param touch The native touch data for tap actions, or null for other action types.
101
+ * Currently unused on Android; reserved for heatmap support.
100
102
  * @param context The additional context to send.
101
103
  * @param timestampMs The timestamp when the action occurred (in milliseconds). If not provided, current timestamp will be used.
102
104
  */
105
+ @Suppress("LongParameterList")
103
106
  @ReactMethod
104
107
  fun addAction(
105
108
  type: String,
106
109
  name: String,
110
+ touch: ReadableMap?,
107
111
  context: ReadableMap,
108
112
  timestampMs: Double,
109
113
  promise: Promise
110
114
  ) {
111
- implementation.addAction(type, name, context, timestampMs, promise)
115
+ implementation.addAction(type, name, touch, context, timestampMs, promise)
112
116
  }
113
117
 
114
118
  /**
@@ -137,6 +137,7 @@ internal class DdRumTest {
137
137
  testedDdRum.addAction(
138
138
  type.name,
139
139
  name,
140
+ null,
140
141
  fakeContext.toReadableMap(),
141
142
  fakeTimestamp,
142
143
  mockPromise
@@ -157,7 +158,14 @@ internal class DdRumTest {
157
158
  }
158
159
 
159
160
  // When
160
- testedDdRum.addAction(type, name, fakeContext.toReadableMap(), fakeTimestamp, mockPromise)
161
+ testedDdRum.addAction(
162
+ type,
163
+ name,
164
+ null,
165
+ fakeContext.toReadableMap(),
166
+ fakeTimestamp,
167
+ mockPromise
168
+ )
161
169
 
162
170
  // Then
163
171
  verify(mockRumMonitor).addAction(RumActionType.CUSTOM, name, updatedContext)
@@ -7,6 +7,7 @@
7
7
  package com.datadog.reactnative
8
8
 
9
9
  import android.content.pm.PackageInfo
10
+ import com.datadog.android.rum.RumMonitor
10
11
  import com.datadog.tools.unit.GenericAssert.Companion.assertThat
11
12
  import com.datadog.tools.unit.forge.BaseConfigurator
12
13
  import com.facebook.react.bridge.ReactApplicationContext
@@ -24,8 +25,12 @@ import org.mockito.Answers
24
25
  import org.mockito.Mock
25
26
  import org.mockito.junit.jupiter.MockitoExtension
26
27
  import org.mockito.junit.jupiter.MockitoSettings
28
+ import org.mockito.kotlin.any
27
29
  import org.mockito.kotlin.doReturn
28
30
  import org.mockito.kotlin.isNull
31
+ import org.mockito.kotlin.mock
32
+ import org.mockito.kotlin.never
33
+ import org.mockito.kotlin.verify
29
34
  import org.mockito.kotlin.whenever
30
35
  import org.mockito.quality.Strictness
31
36
 
@@ -70,6 +75,9 @@ internal class DdSdkNativeInitializationTest {
70
75
  mockDdTelemetry,
71
76
  mockJSONFileReader
72
77
  )
78
+
79
+ DdSdkSessionStartedListener.invalidate()
80
+ DdSdkSessionStartedListener.resetIsRnSdkInitialized()
73
81
  }
74
82
 
75
83
  // region getConfigurationFromJSONFile
@@ -202,4 +210,64 @@ internal class DdSdkNativeInitializationTest {
202
210
  }
203
211
 
204
212
  // endregion
213
+
214
+ // region initialize()
215
+
216
+ @Test
217
+ fun `𝕄 mark RN SDK initialized and catch up session 𝕎 initialize() { isCalledFromJs=true, datadog already initialized }`() { // ktlint-disable-line max-line-length
218
+ // Given
219
+ val mockRumMonitor: RumMonitor = mock()
220
+ whenever(mockDatadog.isInitialized()) doReturn true
221
+ whenever(mockDatadog.getRumMonitor()) doReturn mockRumMonitor
222
+
223
+ // When
224
+ testedNativeInitialization.initialize(
225
+ ddSdkConfiguration = minimalConfiguration(),
226
+ isCalledFromJs = true
227
+ )
228
+
229
+ // Then
230
+ assertThat(DdSdkSessionStartedListener.isRnSdkInitializedForTests()).isTrue()
231
+ verify(mockRumMonitor).getCurrentSessionId(any())
232
+ }
233
+
234
+ @Test
235
+ fun `𝕄 mark RN SDK initialized without catch up 𝕎 initialize() { isCalledFromJs=true, datadog not initialized }`() { // ktlint-disable-line max-line-length
236
+ // Given
237
+ whenever(mockDatadog.isInitialized()) doReturn false
238
+
239
+ // When
240
+ testedNativeInitialization.initialize(
241
+ ddSdkConfiguration = minimalConfiguration(),
242
+ isCalledFromJs = true
243
+ )
244
+
245
+ // Then
246
+ assertThat(DdSdkSessionStartedListener.isRnSdkInitializedForTests()).isTrue()
247
+ verify(mockDatadog, never()).getRumMonitor()
248
+ }
249
+
250
+ @Test
251
+ fun `𝕄 not mark RN SDK initialized or catch up 𝕎 initialize() { isCalledFromJs=false }`() {
252
+ // Given
253
+ whenever(mockDatadog.isInitialized()) doReturn true
254
+
255
+ // When
256
+ testedNativeInitialization.initialize(
257
+ ddSdkConfiguration = minimalConfiguration(),
258
+ isCalledFromJs = false
259
+ )
260
+
261
+ // Then
262
+ assertThat(DdSdkSessionStartedListener.isRnSdkInitializedForTests()).isFalse()
263
+ verify(mockDatadog, never()).getRumMonitor()
264
+ }
265
+
266
+ // endregion
267
+
268
+ private fun minimalConfiguration(): DdSdkConfiguration = DdSdkConfiguration(
269
+ clientToken = "fake-client-token",
270
+ env = "fake-env",
271
+ additionalConfiguration = emptyMap()
272
+ )
205
273
  }
@@ -8,6 +8,7 @@
8
8
  package com.datadog.reactnative
9
9
 
10
10
  import com.datadog.tools.unit.GenericAssert.Companion.assertThat
11
+ import com.datadog.tools.unit.TestUiThreadExecutor
11
12
  import com.datadog.tools.unit.forge.BaseConfigurator
12
13
  import com.facebook.react.bridge.CatalystInstance
13
14
  import com.facebook.react.bridge.NativeArray
@@ -55,6 +56,8 @@ internal class DdSdkSessionStartedListenerTest {
55
56
  @BeforeEach
56
57
  fun `set up`() {
57
58
  DdSdkSessionStartedListener.invalidate()
59
+ DdSdkSessionStartedListener.getInstance().setUiThreadExecutor(TestUiThreadExecutor())
60
+ DdSdkSessionStartedListener.resetIsRnSdkInitialized()
58
61
  }
59
62
 
60
63
  @Test
@@ -91,6 +94,7 @@ internal class DdSdkSessionStartedListenerTest {
91
94
  instance.setReactContext(mockReactContext)
92
95
  instance.setExceptionHandler(mockExceptionHandler)
93
96
  instance.setIsNewArchitecture(false)
97
+ instance.setIsRnSdkInitialized(true)
94
98
 
95
99
  val passedArgs = mutableListOf<String>()
96
100
  instance.setConvertToNativeArray {
@@ -111,6 +115,36 @@ internal class DdSdkSessionStartedListenerTest {
111
115
  )
112
116
  }
113
117
 
118
+ @Test
119
+ fun `𝕄 session ID is NOT sent via bridge W { setReactContext fires before onRnSdkInitialized }`() { // ktlint-disable-line max-line-length
120
+ // GIVEN — bridge looks valid but RN SDK has not called initialize() from JS yet
121
+ whenever(mockReactContext.hasActiveReactInstance()).thenReturn(true)
122
+ whenever(mockReactContext.catalystInstance).thenReturn(mockCatalystInstance)
123
+ whenever(mockCatalystInstance.isDestroyed).thenReturn(false)
124
+ whenever(mockReactContext.fabricUIManager).thenReturn(null)
125
+
126
+ val instance = DdSdkSessionStartedListener.getInstance()
127
+
128
+ val mockConvertToNativeArray = mock<(array: Array<String>) -> NativeArray?>()
129
+ instance.setConvertToNativeArray(mockConvertToNativeArray)
130
+ instance.setIsNewArchitecture(false)
131
+
132
+ // WHEN — native session starts and context becomes available before JS initialize()
133
+ instance.onSessionStarted("TEST-SESSION-ID", false)
134
+ instance.setReactContext(mockReactContext)
135
+
136
+ // THEN — bridge must NOT be called (DatadogInternalReactBridge not yet registered)
137
+ verifyNoInteractions(mockConvertToNativeArray)
138
+
139
+ // WHEN — JS initialize() runs, guaranteeing the callable module is registered
140
+ instance.onRnSdkInitialized()
141
+
142
+ // THEN — catch-up delivery is now safe and matches __datadogOnMessageReceived(eventName, data)
143
+ verify(mockConvertToNativeArray).invoke(
144
+ argWhere { it.size == 2 && it[0] == "RUMSessionStarted" && it[1] == "TEST-SESSION-ID" }
145
+ )
146
+ }
147
+
114
148
  @Test
115
149
  fun `𝕄 session ID event is delayed until context is available W { bridge }`() {
116
150
  // GIVEN
@@ -134,9 +168,12 @@ internal class DdSdkSessionStartedListenerTest {
134
168
  verifyNoInteractions(mockConvertToNativeArray)
135
169
 
136
170
  // WHEN
171
+ instance.setIsRnSdkInitialized(true)
137
172
  instance.setReactContext(mockReactContext)
138
173
 
139
174
  // THEN
140
- verify(mockConvertToNativeArray).invoke(argWhere { it.first() == "TEST-SESSION-ID" })
175
+ verify(mockConvertToNativeArray).invoke(
176
+ argWhere { it.size == 2 && it[0] == "RUMSessionStarted" && it[1] == "TEST-SESSION-ID" }
177
+ )
141
178
  }
142
179
  }
@@ -14,6 +14,7 @@
14
14
 
15
15
  @implementation DdRum
16
16
 
17
+ @synthesize bridge = _bridge;
17
18
  RCT_EXPORT_MODULE()
18
19
 
19
20
  RCT_REMAP_METHOD(startView, withKey:(NSString*)key
@@ -57,12 +58,13 @@ RCT_REMAP_METHOD(stopAction, stopWithType:(NSString*)type
57
58
 
58
59
  RCT_REMAP_METHOD(addAction, addWithType:(NSString*)type
59
60
  withName:(NSString*)name
61
+ withTouch:(nullable NSDictionary*)touch
60
62
  withContext:(NSDictionary*)context
61
63
  withTimestampms:(double)timestampMs
62
64
  withResolver:(RCTPromiseResolveBlock)resolve
63
65
  withRejecter:(RCTPromiseRejectBlock)reject)
64
66
  {
65
- [self addAction:type name:name context:context timestampMs:timestampMs resolve:resolve reject:reject];
67
+ [self addAction:type name:name touch:touch context:context timestampMs:timestampMs resolve:resolve reject:reject];
66
68
  }
67
69
 
68
70
  RCT_REMAP_METHOD(startResource, withKey:(NSString*)key
@@ -207,7 +209,7 @@ RCT_REMAP_METHOD(failFeatureOperation,
207
209
  - (DdRumImplementation*)ddRumImplementation
208
210
  {
209
211
  if (_ddRumImplementation == nil) {
210
- _ddRumImplementation = [[DdRumImplementation alloc] init];
212
+ _ddRumImplementation = [[DdRumImplementation alloc] initWithBridge:_bridge];
211
213
  }
212
214
  return _ddRumImplementation;
213
215
  }
@@ -220,8 +222,8 @@ RCT_REMAP_METHOD(failFeatureOperation,
220
222
  return [RNQueue getSharedQueue];
221
223
  }
222
224
 
223
- - (void)addAction:(NSString *)type name:(NSString *)name context:(NSDictionary *)context timestampMs:(double)timestampMs resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
224
- [self.ddRumImplementation addActionWithType:type name:name context:context timestampMs:timestampMs resolve:resolve reject:reject];
225
+ - (void)addAction:(NSString *)type name:(NSString *)name touch:(nullable NSDictionary *)touch context:(NSDictionary *)context timestampMs:(double)timestampMs resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
226
+ [self.ddRumImplementation addActionWithType:type name:name touch:touch context:context timestampMs:timestampMs resolve:resolve reject:reject];
225
227
  }
226
228
 
227
229
  - (void)addError:(NSString *)message source:(NSString *)source stacktrace:(NSString *)stacktrace context:(NSDictionary *)context timestampMs:(double)timestampMs fingerprint:(NSString *)fingerprint resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {