@datadog/mobile-react-navigation 1.0.0-rc5 → 1.0.0-rc9

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 (46) hide show
  1. package/lib/commonjs/index.js.map +1 -1
  2. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js +77 -51
  3. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  4. package/lib/commonjs/rum/instrumentation/react-navigation.js +2 -0
  5. package/lib/commonjs/rum/instrumentation/react-navigation.js.map +1 -0
  6. package/lib/module/index.js.map +1 -1
  7. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js +74 -52
  8. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  9. package/lib/module/rum/instrumentation/react-navigation.js +2 -0
  10. package/lib/module/rum/instrumentation/react-navigation.js.map +1 -0
  11. package/lib/typescript/core/src/DdSdkReactNative.d.ts +6 -6
  12. package/lib/typescript/core/src/DdSdkReactNativeConfiguration.d.ts +15 -3
  13. package/lib/typescript/core/src/InternalLog.d.ts +5 -0
  14. package/lib/typescript/core/src/TimeProvider.d.ts +20 -0
  15. package/lib/typescript/core/src/Timer.d.ts +3 -2
  16. package/lib/typescript/core/src/index.d.ts +4 -3
  17. package/lib/typescript/core/src/rum/instrumentation/DdEventsInterceptor.d.ts +1 -0
  18. package/lib/typescript/core/src/rum/instrumentation/DdRumErrorTracking.d.ts +2 -2
  19. package/lib/typescript/core/src/rum/instrumentation/DdRumUserInteractionTracking.d.ts +3 -3
  20. package/lib/typescript/core/src/rum/instrumentation/ShallowObjectEqualityChecker.d.ts +4 -0
  21. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +15 -0
  22. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +18 -0
  23. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +6 -0
  24. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +8 -0
  25. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +19 -0
  26. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.d.ts +8 -0
  27. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.d.ts +6 -0
  28. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.d.ts +17 -0
  29. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/URLHostParser.d.ts +6 -0
  30. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +25 -0
  31. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +3 -0
  32. package/lib/typescript/core/src/version.d.ts +1 -1
  33. package/lib/typescript/react-navigation/src/rum/instrumentation/DdRumReactNavigationTracking.d.ts +9 -3
  34. package/lib/typescript/react-navigation/src/rum/instrumentation/react-navigation.d.ts +116 -0
  35. package/package.json +11 -6
  36. package/src/__tests__/rum/instrumentation/DdRumReactNavigationTracking.test.tsx +481 -385
  37. package/src/__tests__/rum/instrumentation/__utils__/AppStateMock.ts +75 -0
  38. package/src/__tests__/rum/instrumentation/__utils__/AppStateMockLegacy.ts +61 -0
  39. package/src/__tests__/rum/instrumentation/__utils__/Navigators/NavigatorsV5.tsx +105 -0
  40. package/src/__tests__/rum/instrumentation/__utils__/Navigators/NavigatorsV6.tsx +105 -0
  41. package/src/index.tsx +4 -1
  42. package/src/rum/instrumentation/DdRumReactNavigationTracking.tsx +145 -64
  43. package/src/rum/instrumentation/react-navigation.ts +158 -0
  44. package/lib/typescript/core/src/rum/instrumentation/DdRumResourceTracking.d.ts +0 -30
  45. package/lib/typescript/core/src/rum/instrumentation/DdRumXhr.d.ts +0 -13
  46. package/lib/typescript/core/src/rum/instrumentation/TraceIdentifier.d.ts +0 -1
@@ -0,0 +1,75 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import type {
8
+ AppStateEvent,
9
+ AppStateStatus,
10
+ NativeEventSubscription
11
+ } from 'react-native';
12
+
13
+ type handler = (type: AppStateStatus) => void;
14
+
15
+ export class AppStateMock {
16
+ private listeners: {
17
+ [eventType: string]: {
18
+ callback: handler;
19
+ subscription: NativeEventSubscription;
20
+ }[];
21
+ } = {};
22
+
23
+ addEventListener = (type: AppStateEvent, callback: handler) => {
24
+ if (!this.listeners[type]) {
25
+ this.listeners[type] = [];
26
+ }
27
+ const subscription = {
28
+ remove: () => this.removeListeners(type, callback)
29
+ };
30
+ this.listeners[type].push({
31
+ callback,
32
+ subscription
33
+ });
34
+
35
+ return subscription;
36
+ };
37
+
38
+ private removeListeners = (type: string, callback: handler) => {
39
+ if (!this.listeners[type]) {
40
+ return;
41
+ }
42
+ const callbackIndex = this.listeners[type].findIndex(
43
+ handler => handler.callback === callback
44
+ );
45
+ if (callbackIndex === -1) {
46
+ return;
47
+ }
48
+ this.listeners[type] = [
49
+ ...this.listeners[type].slice(0, callbackIndex),
50
+ ...this.listeners[type].slice(
51
+ callbackIndex + 1,
52
+ this.listeners[type].length
53
+ )
54
+ ];
55
+ };
56
+
57
+ changeValue = (value: AppStateStatus) => {
58
+ if (!this.listeners.change) {
59
+ return;
60
+ }
61
+ this.listeners.change.forEach(handler => {
62
+ try {
63
+ handler.callback(value);
64
+ } catch (e) {
65
+ console.warn(
66
+ `Failure while executing callback for value ${value}`
67
+ );
68
+ }
69
+ });
70
+ };
71
+
72
+ removeAllListeners = () => {
73
+ this.listeners = {};
74
+ };
75
+ }
@@ -0,0 +1,61 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import type { AppStateEvent, AppStateStatus } from 'react-native';
8
+
9
+ type handler = (type: AppStateStatus) => void;
10
+
11
+ /**
12
+ * This is a mock of legacy implementation of AppState (up until RN 0.65).
13
+ * In the new version, removeEventListener has been deprecated and addEventListener
14
+ * now returns a subscription.
15
+ */
16
+ export class AppStateMockLegacy {
17
+ private listeners: { [eventType: string]: handler[] } = {};
18
+
19
+ addEventListener = (type: AppStateEvent, callback: handler) => {
20
+ if (!this.listeners[type]) {
21
+ this.listeners[type] = [];
22
+ }
23
+ this.listeners[type].push(callback);
24
+ };
25
+
26
+ removeEventListener = (type: string, callback: handler) => {
27
+ if (!this.listeners[type]) {
28
+ return;
29
+ }
30
+ const callbackIndex = this.listeners[type].indexOf(callback);
31
+ if (callbackIndex === -1) {
32
+ return;
33
+ }
34
+ this.listeners[type] = [
35
+ ...this.listeners[type].slice(0, callbackIndex),
36
+ ...this.listeners[type].slice(
37
+ callbackIndex + 1,
38
+ this.listeners[type].length
39
+ )
40
+ ];
41
+ };
42
+
43
+ changeValue = (value: AppStateStatus) => {
44
+ if (!this.listeners.change) {
45
+ return;
46
+ }
47
+ this.listeners.change.forEach(callback => {
48
+ try {
49
+ callback(value);
50
+ } catch (e) {
51
+ console.warn(
52
+ `Failure while executing callback for value ${value}`
53
+ );
54
+ }
55
+ });
56
+ };
57
+
58
+ removeAllListeners = () => {
59
+ this.listeners = {};
60
+ };
61
+ }
@@ -0,0 +1,105 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import type { NavigationContainerRef } from '@react-navigation/native-v5';
8
+ import { NavigationContainer } from '@react-navigation/native-v5';
9
+ import { createStackNavigator } from '@react-navigation/stack-v5';
10
+ import { View, Text, Button } from 'react-native';
11
+ import React from 'react';
12
+
13
+ const { Screen, Navigator } = createStackNavigator();
14
+
15
+ export function FakeAboutScreen({ navigation }) {
16
+ return (
17
+ <View>
18
+ <Text>Welcome to About</Text>
19
+ </View>
20
+ );
21
+ }
22
+
23
+ export function FakeHomeScreen({ navigation }) {
24
+ return (
25
+ <View>
26
+ <Text>Welcome to Home</Text>
27
+ <Button
28
+ title="Go to About"
29
+ onPress={() => {
30
+ navigation.navigate('About');
31
+ }}
32
+ />
33
+ </View>
34
+ );
35
+ }
36
+
37
+ export function FakeSettingsScreen({ navigation }) {
38
+ return (
39
+ <View>
40
+ <Text>Welcome to Settings</Text>
41
+ <Button
42
+ title="Go to Nested Home"
43
+ onPress={() => {
44
+ navigation.navigate('NestedStack');
45
+ }}
46
+ />
47
+ </View>
48
+ );
49
+ }
50
+
51
+ export function FakeProfileScreen({ navigation }) {
52
+ return (
53
+ <Navigator>
54
+ <Screen name="Home" component={FakeHomeScreen} />
55
+ <Screen name="About" component={FakeSettingsScreen} />
56
+ </Navigator>
57
+ );
58
+ }
59
+
60
+ export function FakeNestedStack({ navigation }) {
61
+ return (
62
+ <Navigator>
63
+ <Screen name="NestedHome" component={FakeHomeScreen} />
64
+ </Navigator>
65
+ );
66
+ }
67
+
68
+ export function FakeNavigator1(props: {
69
+ navigationRef: React.RefObject<NavigationContainerRef>;
70
+ }) {
71
+ return (
72
+ <NavigationContainer ref={props.navigationRef}>
73
+ <Navigator>
74
+ <Screen name="Home" component={FakeHomeScreen} />
75
+ <Screen name="About" component={FakeAboutScreen} />
76
+ </Navigator>
77
+ </NavigationContainer>
78
+ );
79
+ }
80
+
81
+ export function FakeNavigator2(props: {
82
+ navigationRef: React.RefObject<NavigationContainerRef>;
83
+ }) {
84
+ return (
85
+ <NavigationContainer ref={props.navigationRef}>
86
+ <Navigator>
87
+ <Screen name="Home" component={FakeHomeScreen} />
88
+ <Screen name="About" component={FakeAboutScreen} />
89
+ </Navigator>
90
+ </NavigationContainer>
91
+ );
92
+ }
93
+
94
+ export function FakeNestedNavigator(props: {
95
+ navigationRef: React.RefObject<NavigationContainerRef>;
96
+ }) {
97
+ return (
98
+ <NavigationContainer ref={props.navigationRef}>
99
+ <Navigator>
100
+ <Screen name="Profile" component={FakeProfileScreen} />
101
+ <Screen name="NestedStack" component={FakeNestedStack} />
102
+ </Navigator>
103
+ </NavigationContainer>
104
+ );
105
+ }
@@ -0,0 +1,105 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import type { NavigationContainerRef } from '@react-navigation/native-v6';
8
+ import { NavigationContainer } from '@react-navigation/native-v6';
9
+ import { createStackNavigator } from '@react-navigation/stack-v6';
10
+ import { View, Text, Button } from 'react-native';
11
+ import React from 'react';
12
+
13
+ const { Screen, Navigator } = createStackNavigator();
14
+
15
+ export function FakeAboutScreen({ navigation }) {
16
+ return (
17
+ <View>
18
+ <Text>Welcome to About</Text>
19
+ </View>
20
+ );
21
+ }
22
+
23
+ export function FakeHomeScreen({ navigation }) {
24
+ return (
25
+ <View>
26
+ <Text>Welcome to Home</Text>
27
+ <Button
28
+ title="Go to About"
29
+ onPress={() => {
30
+ navigation.navigate('About');
31
+ }}
32
+ />
33
+ </View>
34
+ );
35
+ }
36
+
37
+ export function FakeSettingsScreen({ navigation }) {
38
+ return (
39
+ <View>
40
+ <Text>Welcome to Settings</Text>
41
+ <Button
42
+ title="Go to Nested Home"
43
+ onPress={() => {
44
+ navigation.navigate('NestedStack');
45
+ }}
46
+ />
47
+ </View>
48
+ );
49
+ }
50
+
51
+ export function FakeProfileScreen({ navigation }) {
52
+ return (
53
+ <Navigator>
54
+ <Screen name="Home" component={FakeHomeScreen} />
55
+ <Screen name="About" component={FakeSettingsScreen} />
56
+ </Navigator>
57
+ );
58
+ }
59
+
60
+ export function FakeNestedStack({ navigation }) {
61
+ return (
62
+ <Navigator>
63
+ <Screen name="NestedHome" component={FakeHomeScreen} />
64
+ </Navigator>
65
+ );
66
+ }
67
+
68
+ export function FakeNavigator1(props: {
69
+ navigationRef: React.RefObject<NavigationContainerRef<unknown>>;
70
+ }) {
71
+ return (
72
+ <NavigationContainer ref={props.navigationRef}>
73
+ <Navigator>
74
+ <Screen name="Home" component={FakeHomeScreen} />
75
+ <Screen name="About" component={FakeAboutScreen} />
76
+ </Navigator>
77
+ </NavigationContainer>
78
+ );
79
+ }
80
+
81
+ export function FakeNavigator2(props: {
82
+ navigationRef: React.RefObject<NavigationContainerRef<unknown>>;
83
+ }) {
84
+ return (
85
+ <NavigationContainer ref={props.navigationRef}>
86
+ <Navigator>
87
+ <Screen name="Home" component={FakeHomeScreen} />
88
+ <Screen name="About" component={FakeAboutScreen} />
89
+ </Navigator>
90
+ </NavigationContainer>
91
+ );
92
+ }
93
+
94
+ export function FakeNestedNavigator(props: {
95
+ navigationRef: React.RefObject<NavigationContainerRef<unknown>>;
96
+ }) {
97
+ return (
98
+ <NavigationContainer ref={props.navigationRef}>
99
+ <Navigator>
100
+ <Screen name="Profile" component={FakeProfileScreen} />
101
+ <Screen name="NestedStack" component={FakeNestedStack} />
102
+ </Navigator>
103
+ </NavigationContainer>
104
+ );
105
+ }
package/src/index.tsx CHANGED
@@ -4,6 +4,9 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import { DdRumReactNavigationTracking, ViewNamePredicate } from './rum/instrumentation/DdRumReactNavigationTracking';
7
+ import {
8
+ DdRumReactNavigationTracking,
9
+ ViewNamePredicate
10
+ } from './rum/instrumentation/DdRumReactNavigationTracking';
8
11
 
9
12
  export { DdRumReactNavigationTracking, ViewNamePredicate };
@@ -4,82 +4,165 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import type { EventArg, NavigationContainerRef, Route } from "@react-navigation/native";
8
- import { DdRum } from '@datadog/mobile-react-native';
9
- import { AppState, AppStateStatus } from 'react-native';
7
+ import { DdRum, SdkVerbosity, InternalLog } from '@datadog/mobile-react-native';
8
+ import type { AppStateStatus, NativeEventSubscription } from 'react-native';
9
+ import { AppState, BackHandler } from 'react-native';
10
10
 
11
- declare type NavigationListener = (event: EventArg<string, boolean, any>) => void | null
11
+ import type {
12
+ NavigationContainerRef,
13
+ Route,
14
+ NavigationListener
15
+ } from './react-navigation';
12
16
 
13
- // AppStateStatus can have values:
17
+ // AppStateStatus can have values:
14
18
  // 'active' - The app is running in the foreground
15
19
  // 'background' - The app is running in the background. The user is either in another app or on the home screen
16
20
  // 'inactive' [iOS] - This is a transition state that currently never happens for typical React Native apps.
17
21
  // 'unknown' [iOS] - Initial value until the current app state is determined
18
22
  // 'extension' [iOS] - The app is running as an app extension
19
- declare type AppStateListener = (appStateStatus: AppStateStatus) => void | null
23
+ declare type AppStateListener = (appStateStatus: AppStateStatus) => void | null;
20
24
 
21
- export type ViewNamePredicate = (route: Route<string, any | undefined>, trackedName: string) => string
25
+ export type ViewNamePredicate = (
26
+ route: Route<string, any | undefined>,
27
+ trackedName: string
28
+ ) => string;
22
29
 
23
30
  /**
24
31
  * Provides RUM integration for the [ReactNavigation](https://reactnavigation.org/) API.
25
32
  */
26
33
  export class DdRumReactNavigationTracking {
27
-
28
34
  private static registeredContainer: NavigationContainerRef | null;
29
-
30
- private static navigationStateChangeListener: NavigationListener;
31
35
 
32
- private static appStateListener: AppStateListener;
36
+ private static navigationStateChangeListener: NavigationListener;
33
37
 
34
38
  private static viewNamePredicate: ViewNamePredicate;
35
39
 
40
+ private static backHandler: NativeEventSubscription | null;
41
+
42
+ private static appStateSubscription?: NativeEventSubscription;
43
+
44
+ static ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE =
45
+ 'A navigation change was detected but the RUM ViewEvent was dropped as the route was undefined.';
46
+ static NULL_NAVIGATION_REF_ERROR_MESSAGE =
47
+ 'Cannot track views with a null navigationRef.';
48
+ static NAVIGATION_REF_IN_USE_ERROR_MESSAGE =
49
+ 'Cannot track new navigation container while another one is still tracked. Please call `DdRumReactNavigationTracking.stopTrackingViews` on the previous container reference.';
50
+
51
+ static isAppExitingOnBackPress = (): boolean => {
52
+ if (DdRumReactNavigationTracking.registeredContainer === null) {
53
+ return false;
54
+ }
55
+ if (DdRumReactNavigationTracking.registeredContainer.canGoBack()) {
56
+ return false;
57
+ }
58
+ return true;
59
+ };
60
+
61
+ static onBackPress = () => {
62
+ if (DdRumReactNavigationTracking.isAppExitingOnBackPress()) {
63
+ DdRumReactNavigationTracking.stopTrackingViews(
64
+ DdRumReactNavigationTracking.registeredContainer
65
+ );
66
+ }
67
+ // We always return false so we make sure the react-navigation callback is called.
68
+ // See https://reactnative.dev/docs/backhandler
69
+ return false;
70
+ };
71
+
36
72
  /**
37
73
  * Starts tracking the NavigationContainer and sends a RUM View event every time the navigation route changed.
38
74
  * @param navigationRef the reference to the real NavigationContainer.
39
75
  */
40
76
  static startTrackingViews(
41
- navigationRef: NavigationContainerRef | null,
42
- viewNamePredicate: ViewNamePredicate = function (_route: Route<string, any | undefined>, trackedName: string) { return trackedName; }
43
- ): void {
44
-
77
+ navigationRef: NavigationContainerRef | null,
78
+ viewNamePredicate: ViewNamePredicate = function (
79
+ _route: Route<string, any | undefined>,
80
+ trackedName: string
81
+ ) {
82
+ return trackedName;
83
+ }
84
+ ): void {
45
85
  if (navigationRef == null) {
46
- console.log ("Cannot track views with a null navigationRef");
86
+ InternalLog.log(
87
+ DdRumReactNavigationTracking.NULL_NAVIGATION_REF_ERROR_MESSAGE,
88
+ SdkVerbosity.ERROR
89
+ );
47
90
  return;
48
91
  }
49
92
 
50
- if (DdRumReactNavigationTracking.registeredContainer != null && this.registeredContainer !== navigationRef) {
51
- console.error('Cannot track new navigation container while another one is still tracked');
93
+ if (
94
+ DdRumReactNavigationTracking.registeredContainer != null &&
95
+ this.registeredContainer !== navigationRef
96
+ ) {
97
+ InternalLog.log(
98
+ DdRumReactNavigationTracking.NAVIGATION_REF_IN_USE_ERROR_MESSAGE,
99
+ SdkVerbosity.ERROR
100
+ );
52
101
  } else if (DdRumReactNavigationTracking.registeredContainer == null) {
53
102
  DdRumReactNavigationTracking.viewNamePredicate = viewNamePredicate;
54
103
  const listener = DdRumReactNavigationTracking.resolveNavigationStateChangeListener();
55
- DdRumReactNavigationTracking.handleRouteNavigation(navigationRef.getCurrentRoute());
56
- navigationRef.addListener("state", listener);
104
+ DdRumReactNavigationTracking.handleRouteNavigation(
105
+ navigationRef.getCurrentRoute()
106
+ );
107
+ navigationRef.addListener('state', listener);
57
108
  DdRumReactNavigationTracking.registeredContainer = navigationRef;
109
+ DdRumReactNavigationTracking.backHandler = BackHandler.addEventListener(
110
+ 'hardwareBackPress',
111
+ DdRumReactNavigationTracking.onBackPress
112
+ );
113
+ this.appStateSubscription = AppState.addEventListener(
114
+ 'change',
115
+ DdRumReactNavigationTracking.appStateListener
116
+ );
58
117
  }
59
-
60
-
61
- DdRumReactNavigationTracking.registerAppStateListenerIfNeeded();
62
118
  }
63
119
 
64
120
  /**
65
121
  * Stops tracking the NavigationContainer.
66
122
  * @param navigationRef the reference to the real NavigationContainer.
67
123
  */
68
- static stopTrackingViews(navigationRef: NavigationContainerRef | null): void {
124
+ static stopTrackingViews(
125
+ navigationRef: NavigationContainerRef | null
126
+ ): void {
69
127
  if (navigationRef != null) {
70
- navigationRef.removeListener("state", DdRumReactNavigationTracking.navigationStateChangeListener);
128
+ navigationRef.removeListener(
129
+ 'state',
130
+ DdRumReactNavigationTracking.navigationStateChangeListener
131
+ );
132
+ DdRumReactNavigationTracking.backHandler?.remove();
133
+ DdRumReactNavigationTracking.backHandler = null;
71
134
  DdRumReactNavigationTracking.registeredContainer = null;
72
- DdRumReactNavigationTracking.viewNamePredicate = function (_route: Route<string, any | undefined>, trackedName: string) { return trackedName; }
135
+ DdRumReactNavigationTracking.viewNamePredicate = function (
136
+ _route: Route<string, any | undefined>,
137
+ trackedName: string
138
+ ) {
139
+ return trackedName;
140
+ };
141
+ }
142
+
143
+ // For versions of React Native below 0.65, addEventListener does not return a subscription.
144
+ // We have to call AppState.removeEventListener instead.
145
+ if (this.appStateSubscription) {
146
+ this.appStateSubscription.remove();
147
+ } else {
148
+ AppState.removeEventListener(
149
+ 'change',
150
+ DdRumReactNavigationTracking.appStateListener
151
+ );
73
152
  }
74
153
  }
75
154
 
76
155
  private static handleRouteNavigation(
77
- route: Route<string, any | undefined> | undefined,
156
+ route: Route<string, any | undefined> | undefined,
78
157
  appStateStatus: AppStateStatus | undefined = undefined
79
- ) {
80
- if (route == undefined || route == null) {
158
+ ) {
159
+ if (route === undefined || route === null) {
160
+ InternalLog.log(
161
+ DdRumReactNavigationTracking.ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE,
162
+ SdkVerbosity.WARN
163
+ );
81
164
  // RUMM-1400 in some cases the route seem to be undefined
82
- return
165
+ return;
83
166
  }
84
167
  const key = route.key;
85
168
 
@@ -89,8 +172,11 @@ export class DdRumReactNavigationTracking {
89
172
  if (key != null && screenName != null) {
90
173
  if (appStateStatus === 'background') {
91
174
  DdRum.stopView(key);
92
- } else if (appStateStatus === 'active' || appStateStatus == undefined) {
93
- // case when app goes into foreground,
175
+ } else if (
176
+ appStateStatus === 'active' ||
177
+ appStateStatus === undefined
178
+ ) {
179
+ // case when app goes into foreground,
94
180
  // in that case navigation listener won't be called
95
181
  DdRum.startView(key, screenName);
96
182
  }
@@ -98,22 +184,18 @@ export class DdRumReactNavigationTracking {
98
184
  }
99
185
 
100
186
  private static resolveNavigationStateChangeListener(): NavigationListener {
101
- if (DdRumReactNavigationTracking.navigationStateChangeListener == null) {
102
- DdRumReactNavigationTracking.navigationStateChangeListener = (event: EventArg<string, boolean, any>) => {
103
- let route = event.data?.state?.routes[event.data?.state?.index];
104
-
105
- if (route == undefined) {
106
- // RUMM-1400 in some cases the route seem to be undefined
107
- return
108
- }
109
-
110
- while (route.state != undefined) {
111
- const nestedRoute = route.state.routes[route.state.index];
112
- if (nestedRoute == undefined) {
113
- // RUMM-1400 in some cases the route seem to be undefined
114
- break;
115
- }
116
- route = nestedRoute
187
+ if (
188
+ DdRumReactNavigationTracking.navigationStateChangeListener == null
189
+ ) {
190
+ DdRumReactNavigationTracking.navigationStateChangeListener = () => {
191
+ const route = DdRumReactNavigationTracking.registeredContainer?.getCurrentRoute();
192
+
193
+ if (route === undefined) {
194
+ InternalLog.log(
195
+ DdRumReactNavigationTracking.ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE,
196
+ SdkVerbosity.WARN
197
+ );
198
+ return;
117
199
  }
118
200
 
119
201
  DdRumReactNavigationTracking.handleRouteNavigation(route);
@@ -122,22 +204,21 @@ export class DdRumReactNavigationTracking {
122
204
  return DdRumReactNavigationTracking.navigationStateChangeListener;
123
205
  }
124
206
 
125
- private static registerAppStateListenerIfNeeded() {
126
- if (DdRumReactNavigationTracking.appStateListener == null) {
127
- DdRumReactNavigationTracking.appStateListener = (appStateStatus: AppStateStatus) => {
128
-
129
- const currentRoute = DdRumReactNavigationTracking.registeredContainer?.getCurrentRoute();
130
- if (currentRoute == undefined) {
131
- return;
132
- }
133
-
134
- DdRumReactNavigationTracking.handleRouteNavigation(currentRoute, appStateStatus);
135
- };
136
-
137
- // AppState is singleton, so we should add a listener only once in the app lifetime
138
- AppState.addEventListener("change", DdRumReactNavigationTracking.appStateListener);
207
+ private static appStateListener: AppStateListener = (
208
+ appStateStatus: AppStateStatus
209
+ ) => {
210
+ const currentRoute = DdRumReactNavigationTracking.registeredContainer?.getCurrentRoute();
211
+ if (currentRoute === undefined || currentRoute === null) {
212
+ InternalLog.log(
213
+ `We could not determine the route when changing the application state to: ${appStateStatus}. No RUM View event will be sent in this case.`,
214
+ SdkVerbosity.ERROR
215
+ );
216
+ return;
139
217
  }
140
- }
141
218
 
219
+ DdRumReactNavigationTracking.handleRouteNavigation(
220
+ currentRoute,
221
+ appStateStatus
222
+ );
223
+ };
142
224
  }
143
-