@datadog/mobile-react-navigation 1.0.0-rc8 → 1.1.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 (32) hide show
  1. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js +11 -4
  2. package/lib/commonjs/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  3. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js +11 -3
  4. package/lib/module/rum/instrumentation/DdRumReactNavigationTracking.js.map +1 -1
  5. package/lib/typescript/core/src/DdSdkReactNative.d.ts +2 -0
  6. package/lib/typescript/core/src/DdSdkReactNativeConfiguration.d.ts +20 -0
  7. package/lib/typescript/core/src/foundation.d.ts +3 -2
  8. package/lib/typescript/core/src/nativeModulesTypes.d.ts +168 -0
  9. package/lib/typescript/core/src/rum/instrumentation/DdEventsInterceptor.d.ts +0 -15
  10. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +15 -0
  11. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +18 -0
  12. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +6 -0
  13. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +8 -0
  14. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +20 -0
  15. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.d.ts +8 -0
  16. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/internalDevResourceBlocklist.d.ts +6 -0
  17. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/resourceTiming.d.ts +17 -0
  18. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/URLHostParser.d.ts +6 -0
  19. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +25 -0
  20. package/lib/typescript/core/src/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +3 -0
  21. package/lib/typescript/core/src/types.d.ts +42 -7
  22. package/lib/typescript/core/src/version.d.ts +1 -1
  23. package/lib/typescript/react-navigation/src/rum/instrumentation/DdRumReactNavigationTracking.d.ts +1 -0
  24. package/package.json +4 -4
  25. package/src/__tests__/rum/instrumentation/DdRumReactNavigationTracking.test.tsx +135 -122
  26. package/src/__tests__/rum/instrumentation/__utils__/AppStateMock.ts +35 -16
  27. package/src/__tests__/rum/instrumentation/__utils__/AppStateMockLegacy.ts +61 -0
  28. package/src/rum/instrumentation/DdRumReactNavigationTracking.tsx +14 -5
  29. package/lib/typescript/core/src/rum/instrumentation/DdRumResourceTracking.d.ts +0 -40
  30. package/lib/typescript/core/src/rum/instrumentation/DdRumXhr.d.ts +0 -23
  31. package/lib/typescript/core/src/rum/instrumentation/TraceIdentifier.d.ts +0 -1
  32. package/lib/typescript/core/src/rum/instrumentation/URLHostParser.d.ts +0 -6
@@ -106,26 +106,38 @@ export declare type DdRumType = {
106
106
  stopView(key: string, context?: object, timestampMs?: number): Promise<void>;
107
107
  /**
108
108
  * Start tracking a RUM Action.
109
- * @param type: The action type (tap, scroll, swipe, click, custom).
109
+ * @param type: The action type (tap, scroll, swipe, back, custom).
110
110
  * @param name: The action name.
111
111
  * @param context: The additional context to send.
112
112
  * @param timestampMs: The timestamp when the action started (in milliseconds). If not provided, current timestamp will be used.
113
113
  */
114
- startAction(type: string, name: string, context?: object, timestampMs?: number): Promise<void>;
114
+ startAction(type: RumActionType, name: string, context?: object, timestampMs?: number): Promise<void>;
115
115
  /**
116
116
  * Stop tracking the ongoing RUM Action.
117
+ * @param type: The action type (tap, scroll, swipe, back, custom).
118
+ * @param name: The action name.
119
+ * @param context: The additional context to send.
120
+ * @param timestampMs: The timestamp when the action stopped (in milliseconds). If not provided, current timestamp will be used.
121
+ */
122
+ stopAction(type: RumActionType, name: string, context?: object, timestampMs?: number): Promise<void>;
123
+ /**
124
+ * Stop tracking the ongoing RUM Action.
125
+ *
126
+ * Warning: using this function signature can lead to inconsistent behaviors on iOS and Android when multiple actions are started in parallel.
127
+ *
128
+ * @deprecated add the `type` and `name` of the action as first two arguments.
117
129
  * @param context: The additional context to send.
118
130
  * @param timestampMs: The timestamp when the action stopped (in milliseconds). If not provided, current timestamp will be used.
119
131
  */
120
132
  stopAction(context?: object, timestampMs?: number): Promise<void>;
121
133
  /**
122
134
  * Add a RUM Action.
123
- * @param type: The action type (tap, scroll, swipe, click, custom).
135
+ * @param type: The action type (tap, scroll, swipe, back, custom).
124
136
  * @param name: The action name.
125
137
  * @param context: The additional context to send.
126
138
  * @param timestampMs: The timestamp when the action occurred (in milliseconds). If not provided, current timestamp will be used.
127
139
  */
128
- addAction(type: string, name: string, context?: object, timestampMs?: number): Promise<void>;
140
+ addAction(type: RumActionType, name: string, context?: object, timestampMs?: number): Promise<void>;
129
141
  /**
130
142
  * Start tracking a RUM Resource.
131
143
  * @param key: The resource unique key identifier.
@@ -144,19 +156,42 @@ export declare type DdRumType = {
144
156
  * @param context: The additional context to send.
145
157
  * @param timestampMs: The timestamp when the resource stopped (in milliseconds). If not provided, current timestamp will be used.
146
158
  */
147
- stopResource(key: string, statusCode: number, kind: string, size?: number, context?: object, timestampMs?: number): Promise<void>;
159
+ stopResource(key: string, statusCode: number, kind: ResourceKind, size?: number, context?: object, timestampMs?: number): Promise<void>;
148
160
  /**
149
161
  * Add a RUM Error.
150
162
  * @param message: The error message.
151
- * @param source: The error source (network, source, console, logger, …).
163
+ * @param source: The error source (network, source, console, webview, custom).
152
164
  * @param stacktrace: The error stacktrace.
153
165
  * @param context: The additional context to send.
154
166
  * @param timestampMs: The timestamp when the error occurred (in milliseconds). If not provided, current timestamp will be used.
155
167
  */
156
- addError(message: string, source: string, stacktrace: string, context?: object, timestampMs?: number): Promise<void>;
168
+ addError(message: string, source: ErrorSource, stacktrace: string, context?: object, timestampMs?: number): Promise<void>;
157
169
  /**
158
170
  * Adds a specific timing in the active View. The timing duration will be computed as the difference between the time the View was started and the time this function was called.
159
171
  * @param name: The name of the new custom timing attribute. Timings can be nested up to 8 levels deep. Names using more than 8 levels will be sanitized by SDK.
160
172
  */
161
173
  addTiming(name: string): Promise<void>;
162
174
  };
175
+ /**
176
+ * Describe the type of a RUM Action.
177
+ */
178
+ export declare enum RumActionType {
179
+ /** User tapped on a widget. */
180
+ TAP = "TAP",
181
+ /** User scrolled a view. */
182
+ SCROLL = "SCROLL",
183
+ /** User swiped on a view. */
184
+ SWIPE = "SWIPE",
185
+ /** User pressed hardware back button (Android only). */
186
+ BACK = "BACK",
187
+ /** A custom action. */
188
+ CUSTOM = "CUSTOM"
189
+ }
190
+ export declare type ResourceKind = 'image' | 'xhr' | 'beacon' | 'css' | 'document' | 'fetch' | 'font' | 'js' | 'media' | 'other' | 'native';
191
+ export declare enum ErrorSource {
192
+ NETWORK = "NETWORK",
193
+ SOURCE = "SOURCE",
194
+ CONSOLE = "CONSOLE",
195
+ WEBVIEW = "WEBVIEW",
196
+ CUSTOM = "CUSTOM"
197
+ }
@@ -1 +1 @@
1
- export declare const version = "1.0.0-rc8";
1
+ export declare const version = "1.1.0";
@@ -8,6 +8,7 @@ export declare class DdRumReactNavigationTracking {
8
8
  private static navigationStateChangeListener;
9
9
  private static viewNamePredicate;
10
10
  private static backHandler;
11
+ private static appStateSubscription?;
11
12
  static ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE: string;
12
13
  static NULL_NAVIGATION_REF_ERROR_MESSAGE: string;
13
14
  static NAVIGATION_REF_IN_USE_ERROR_MESSAGE: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datadog/mobile-react-navigation",
3
- "version": "1.0.0-rc8",
3
+ "version": "1.1.0",
4
4
  "description": "A client-side React Native module to interact with Datadog",
5
5
  "keywords": [
6
6
  "datadog",
@@ -33,10 +33,10 @@
33
33
  "scripts": {
34
34
  "test": "jest",
35
35
  "lint": "eslint .",
36
- "prepare": "bob build"
36
+ "prepare": "rm -rf lib && yarn bob build"
37
37
  },
38
38
  "devDependencies": {
39
- "@datadog/mobile-react-native": "^1.0.0-rc8",
39
+ "@datadog/mobile-react-native": "^1.1.0",
40
40
  "@react-navigation/native-v5": "npm:@react-navigation/native@5.9.2",
41
41
  "@react-navigation/native-v6": "npm:@react-navigation/native@6.0.9",
42
42
  "@react-navigation/stack-v5": "npm:@react-navigation/stack@5.14.2",
@@ -77,5 +77,5 @@
77
77
  ]
78
78
  ]
79
79
  },
80
- "gitHead": "67456f90ac95007f8f99beb065ccbf6ec9a6f478"
80
+ "gitHead": "ca7c8d5d4969a8e8324bd0a7882eba11e6ef41ff"
81
81
  }
@@ -15,6 +15,7 @@ import React, { createRef } from 'react';
15
15
  import type { ViewNamePredicate } from '../../../rum/instrumentation/DdRumReactNavigationTracking';
16
16
  import { DdRumReactNavigationTracking } from '../../../rum/instrumentation/DdRumReactNavigationTracking';
17
17
 
18
+ import { AppStateMockLegacy } from './__utils__/AppStateMockLegacy';
18
19
  import { AppStateMock } from './__utils__/AppStateMock';
19
20
  import {
20
21
  FakeNavigator1 as FakeNavigator1v5,
@@ -56,16 +57,8 @@ jest.mock('@datadog/mobile-react-native', () => {
56
57
  };
57
58
  });
58
59
 
59
- const appStateMock = new AppStateMock();
60
- mocked(AppState.addEventListener).mockImplementation(
61
- appStateMock.addEventListener
62
- );
63
- mocked(AppState.removeEventListener).mockImplementation(
64
- appStateMock.removeEventListener
65
- );
66
-
67
60
  // Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
68
- jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
61
+ jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
69
62
  jest.useFakeTimers();
70
63
 
71
64
  beforeEach(() => {
@@ -74,8 +67,6 @@ beforeEach(() => {
74
67
  mocked(DdRum.startView).mockClear();
75
68
  mocked(DdRum.stopView).mockClear();
76
69
  mocked(AppState.addEventListener).mockClear();
77
- mocked(AppState.removeEventListener).mockClear();
78
- appStateMock.removeAllListeners();
79
70
  mocked(BackHandler.exitApp).mockClear();
80
71
 
81
72
  // @ts-ignore
@@ -339,117 +330,139 @@ describe.each([
339
330
  });
340
331
  });
341
332
 
342
- describe('AppState listener', () => {
343
- it('registers and unregisters AppState', async () => {
344
- // GIVEN
345
- const navigationRef1 = createRef<any>();
346
- render(<FakeNavigator1 navigationRef={navigationRef1} />);
347
- const navigationRef2 = createRef<any>();
348
- render(<FakeNavigator2 navigationRef={navigationRef2} />);
349
-
350
- // WHEN
351
- DdRumReactNavigationTracking.startTrackingViews(
352
- navigationRef1.current
353
- );
354
- DdRumReactNavigationTracking.stopTrackingViews(
355
- navigationRef1.current
356
- );
357
- DdRumReactNavigationTracking.startTrackingViews(
358
- navigationRef2.current
359
- );
360
-
361
- // THEN
362
- expect(AppState.addEventListener).toHaveBeenCalledTimes(2);
363
- expect(AppState.removeEventListener).toHaveBeenCalledTimes(1);
364
-
365
- // WHEN we go in background mode
366
- appStateMock.changeValue('background');
367
-
368
- // THEN the listener is only called once
369
- expect(DdRum.stopView).toHaveBeenCalledTimes(1);
370
- });
371
-
372
- it('does not log AppState changes when tracking is stopped', async () => {
373
- // GIVEN
374
- const navigationRef = createRef<any>();
375
- render(<FakeNavigator1 navigationRef={navigationRef} />);
376
-
377
- // WHEN
378
- DdRumReactNavigationTracking.startTrackingViews(
379
- navigationRef.current
380
- );
381
- DdRumReactNavigationTracking.stopTrackingViews(
382
- navigationRef.current
383
- );
384
- appStateMock.changeValue('background');
385
-
386
- // THEN
387
- expect(DdRum.stopView).not.toHaveBeenCalled();
388
- expect(InternalLog.log).not.toHaveBeenCalledWith(
389
- 'We could not determine the route when changing the application state to: background. No RUM View event will be sent in this case.',
390
- 'error'
391
- );
392
- });
393
-
394
- it('stops active view when app goes into background', async () => {
395
- // GIVEN
396
- const navigationRef = createRef<any>();
397
- render(<FakeNavigator1 navigationRef={navigationRef} />);
398
-
399
- DdRumReactNavigationTracking.startTrackingViews(
400
- navigationRef.current
401
- );
402
-
403
- // WHEN
404
- appStateMock.changeValue('background');
405
-
406
- // THEN
407
- expect(DdRum.stopView).toHaveBeenCalledTimes(1);
408
- expect(DdRum.stopView).toHaveBeenCalledWith(
409
- navigationRef.current?.getCurrentRoute()?.key
410
- );
411
- expect(
412
- typeof navigationRef.current?.getCurrentRoute()?.key
413
- ).toBe('string');
414
- });
415
-
416
- it('starts last view when app goes into foreground', async () => {
417
- // GIVEN
418
- const navigationRef = createRef<any>();
419
- render(<FakeNavigator1 navigationRef={navigationRef} />);
420
-
421
- DdRumReactNavigationTracking.startTrackingViews(
422
- navigationRef.current
423
- );
424
-
425
- // WHEN
426
- appStateMock.changeValue('background');
427
- appStateMock.changeValue('active');
428
-
429
- // THEN
430
- expect(DdRum.stopView).toHaveBeenCalledTimes(1);
431
- expect(DdRum.startView).toHaveBeenCalledTimes(2);
432
- });
433
-
434
- it('does not stop view when no navigator attached', async () => {
435
- // GIVEN
436
- const navigationRef = createRef<any>();
437
- render(<FakeNavigator1 navigationRef={navigationRef} />);
438
-
439
- DdRumReactNavigationTracking.startTrackingViews(
440
- navigationRef.current
441
- );
442
- DdRumReactNavigationTracking.stopTrackingViews(
443
- navigationRef.current
444
- );
445
-
446
- // WHEN
447
- appStateMock.changeValue('background');
448
-
449
- // THEN
450
- expect(DdRum.stopView).not.toHaveBeenCalled();
451
- });
452
- });
333
+ describe.each([
334
+ ['react-native 0.63-0.64', AppStateMockLegacy],
335
+ ['react-native 0.65+', AppStateMock]
336
+ ])(
337
+ 'AppState listener on %s',
338
+ (reactNativeVersion, AppStateMockVersion) => {
339
+ let appStateMock;
340
+ beforeEach(() => {
341
+ appStateMock = new AppStateMockVersion();
342
+ mocked(AppState.addEventListener).mockImplementation(
343
+ // @ts-ignore
344
+ appStateMock.addEventListener
345
+ );
346
+ // @ts-ignore
347
+ if (appStateMock.removeEventListener) {
348
+ AppState.removeEventListener = jest.fn(
349
+ appStateMock.removeEventListener
350
+ );
351
+ }
352
+ });
353
+ it('registers and unregisters AppState', async () => {
354
+ // GIVEN
355
+ const navigationRef1 = createRef<any>();
356
+ render(<FakeNavigator1 navigationRef={navigationRef1} />);
357
+ const navigationRef2 = createRef<any>();
358
+ render(<FakeNavigator2 navigationRef={navigationRef2} />);
359
+
360
+ // WHEN
361
+ DdRumReactNavigationTracking.startTrackingViews(
362
+ navigationRef1.current
363
+ );
364
+ DdRumReactNavigationTracking.stopTrackingViews(
365
+ navigationRef1.current
366
+ );
367
+ expect(appStateMock.listeners.change).toHaveLength(0);
368
+
369
+ DdRumReactNavigationTracking.startTrackingViews(
370
+ navigationRef2.current
371
+ );
372
+
373
+ // THEN
374
+ expect(AppState.addEventListener).toHaveBeenCalledTimes(2);
375
+ expect(appStateMock.listeners.change).toHaveLength(1);
376
+
377
+ // WHEN we go in background mode
378
+ appStateMock.changeValue('background');
379
+
380
+ // THEN the listener is only called once
381
+ expect(DdRum.stopView).toHaveBeenCalledTimes(1);
382
+ });
383
+
384
+ it('does not log AppState changes when tracking is stopped', async () => {
385
+ // GIVEN
386
+ const navigationRef = createRef<any>();
387
+ render(<FakeNavigator1 navigationRef={navigationRef} />);
388
+
389
+ // WHEN
390
+ DdRumReactNavigationTracking.startTrackingViews(
391
+ navigationRef.current
392
+ );
393
+ DdRumReactNavigationTracking.stopTrackingViews(
394
+ navigationRef.current
395
+ );
396
+ appStateMock.changeValue('background');
397
+
398
+ // THEN
399
+ expect(DdRum.stopView).not.toHaveBeenCalled();
400
+ expect(InternalLog.log).not.toHaveBeenCalledWith(
401
+ 'We could not determine the route when changing the application state to: background. No RUM View event will be sent in this case.',
402
+ 'error'
403
+ );
404
+ });
405
+
406
+ it('stops active view when app goes into background', async () => {
407
+ // GIVEN
408
+ const navigationRef = createRef<any>();
409
+ render(<FakeNavigator1 navigationRef={navigationRef} />);
410
+
411
+ DdRumReactNavigationTracking.startTrackingViews(
412
+ navigationRef.current
413
+ );
414
+
415
+ // WHEN
416
+ appStateMock.changeValue('background');
417
+
418
+ // THEN
419
+ expect(DdRum.stopView).toHaveBeenCalledTimes(1);
420
+ expect(DdRum.stopView).toHaveBeenCalledWith(
421
+ navigationRef.current?.getCurrentRoute()?.key
422
+ );
423
+ expect(
424
+ typeof navigationRef.current?.getCurrentRoute()?.key
425
+ ).toBe('string');
426
+ });
427
+
428
+ it('starts last view when app goes into foreground', async () => {
429
+ // GIVEN
430
+ const navigationRef = createRef<any>();
431
+ render(<FakeNavigator1 navigationRef={navigationRef} />);
432
+
433
+ DdRumReactNavigationTracking.startTrackingViews(
434
+ navigationRef.current
435
+ );
436
+
437
+ // WHEN
438
+ appStateMock.changeValue('background');
439
+ appStateMock.changeValue('active');
440
+
441
+ // THEN
442
+ expect(DdRum.stopView).toHaveBeenCalledTimes(1);
443
+ expect(DdRum.startView).toHaveBeenCalledTimes(2);
444
+ });
445
+
446
+ it('does not stop view when no navigator attached', async () => {
447
+ // GIVEN
448
+ const navigationRef = createRef<any>();
449
+ render(<FakeNavigator1 navigationRef={navigationRef} />);
450
+
451
+ DdRumReactNavigationTracking.startTrackingViews(
452
+ navigationRef.current
453
+ );
454
+ DdRumReactNavigationTracking.stopTrackingViews(
455
+ navigationRef.current
456
+ );
457
+
458
+ // WHEN
459
+ appStateMock.changeValue('background');
460
+
461
+ // THEN
462
+ expect(DdRum.stopView).not.toHaveBeenCalled();
463
+ });
464
+ }
465
+ );
453
466
 
454
467
  describe('Android back handler', () => {
455
468
  it('does not send an error when the app closes with Android back button', async () => {
@@ -4,44 +4,63 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import type { AppStateEvent, AppStateStatus } from 'react-native';
7
+ import type {
8
+ AppStateEvent,
9
+ AppStateStatus,
10
+ NativeEventSubscription
11
+ } from 'react-native';
8
12
 
9
13
  type handler = (type: AppStateStatus) => void;
10
14
 
11
15
  export class AppStateMock {
12
- private handlers: { [eventType: string]: handler[] } = {};
16
+ private listeners: {
17
+ [eventType: string]: {
18
+ callback: handler;
19
+ subscription: NativeEventSubscription;
20
+ }[];
21
+ } = {};
13
22
 
14
23
  addEventListener = (type: AppStateEvent, callback: handler) => {
15
- if (!this.handlers[type]) {
16
- this.handlers[type] = [];
24
+ if (!this.listeners[type]) {
25
+ this.listeners[type] = [];
17
26
  }
18
- this.handlers[type].push(callback);
27
+ const subscription = {
28
+ remove: () => this.removeListeners(type, callback)
29
+ };
30
+ this.listeners[type].push({
31
+ callback,
32
+ subscription
33
+ });
34
+
35
+ return subscription;
19
36
  };
20
37
 
21
- removeEventListener = (type: string, callback: handler) => {
22
- if (!this.handlers[type]) {
38
+ private removeListeners = (type: string, callback: handler) => {
39
+ if (!this.listeners[type]) {
23
40
  return;
24
41
  }
25
- const callbackIndex = this.handlers[type].indexOf(callback);
42
+ const callbackIndex = this.listeners[type].findIndex(
43
+ handler => handler.callback === callback
44
+ );
26
45
  if (callbackIndex === -1) {
27
46
  return;
28
47
  }
29
- this.handlers[type] = [
30
- ...this.handlers[type].slice(0, callbackIndex),
31
- ...this.handlers[type].slice(
48
+ this.listeners[type] = [
49
+ ...this.listeners[type].slice(0, callbackIndex),
50
+ ...this.listeners[type].slice(
32
51
  callbackIndex + 1,
33
- this.handlers[type].length
52
+ this.listeners[type].length
34
53
  )
35
54
  ];
36
55
  };
37
56
 
38
57
  changeValue = (value: AppStateStatus) => {
39
- if (!this.handlers.change) {
58
+ if (!this.listeners.change) {
40
59
  return;
41
60
  }
42
- this.handlers.change.forEach(callback => {
61
+ this.listeners.change.forEach(handler => {
43
62
  try {
44
- callback(value);
63
+ handler.callback(value);
45
64
  } catch (e) {
46
65
  console.warn(
47
66
  `Failure while executing callback for value ${value}`
@@ -51,6 +70,6 @@ export class AppStateMock {
51
70
  };
52
71
 
53
72
  removeAllListeners = () => {
54
- this.handlers = {};
73
+ this.listeners = {};
55
74
  };
56
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
+ }
@@ -39,6 +39,8 @@ export class DdRumReactNavigationTracking {
39
39
 
40
40
  private static backHandler: NativeEventSubscription | null;
41
41
 
42
+ private static appStateSubscription?: NativeEventSubscription;
43
+
42
44
  static ROUTE_UNDEFINED_NAVIGATION_WARNING_MESSAGE =
43
45
  'A navigation change was detected but the RUM ViewEvent was dropped as the route was undefined.';
44
46
  static NULL_NAVIGATION_REF_ERROR_MESSAGE =
@@ -108,7 +110,7 @@ export class DdRumReactNavigationTracking {
108
110
  'hardwareBackPress',
109
111
  DdRumReactNavigationTracking.onBackPress
110
112
  );
111
- AppState.addEventListener(
113
+ this.appStateSubscription = AppState.addEventListener(
112
114
  'change',
113
115
  DdRumReactNavigationTracking.appStateListener
114
116
  );
@@ -137,10 +139,17 @@ export class DdRumReactNavigationTracking {
137
139
  return trackedName;
138
140
  };
139
141
  }
140
- AppState.removeEventListener(
141
- 'change',
142
- DdRumReactNavigationTracking.appStateListener
143
- );
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
+ );
152
+ }
144
153
  }
145
154
 
146
155
  private static handleRouteNavigation(
@@ -1,40 +0,0 @@
1
- /// <reference types="react-native" />
2
- import type { DdRumResourceTracingAttributes } from './DdRumXhr';
3
- export declare const TRACE_ID_HEADER_KEY = "x-datadog-trace-id";
4
- export declare const PARENT_ID_HEADER_KEY = "x-datadog-parent-id";
5
- export declare const ORIGIN_HEADER_KEY = "x-datadog-origin";
6
- export declare const SAMPLING_PRIORITY_HEADER_KEY = "x-datadog-sampling-priority";
7
- export declare const ORIGIN_RUM = "rum";
8
- export declare const RESOURCE_SIZE_ERROR_MESSAGE = "Couldn't get resource size, because an error occured: ";
9
- export declare function calculateResponseSize(xhr: XMLHttpRequest): number;
10
- /**
11
- * Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
12
- */
13
- export declare class DdRumResourceTracking {
14
- private static isTracking;
15
- private static tracingSamplingRate;
16
- private static firstPartyHostsRegex;
17
- private static originalXhrOpen;
18
- private static originalXhrSend;
19
- /**
20
- * Starts tracking resources and sends a RUM Resource event every time a network request is detected.
21
- */
22
- static startTracking({ tracingSamplingRate, firstPartyHosts }: {
23
- tracingSamplingRate: number;
24
- firstPartyHosts: string[];
25
- }): void;
26
- /**
27
- * Starts tracking resources and sends a RUM Resource event every time a fetch or XHR call is detected.
28
- */
29
- static startTrackingInternal(xhrType: typeof XMLHttpRequest, { tracingSamplingRate, firstPartyHosts }: {
30
- tracingSamplingRate: number;
31
- firstPartyHosts: string[];
32
- }): void;
33
- static stopTracking(): void;
34
- static getTracingAttributes(url: string): DdRumResourceTracingAttributes;
35
- static proxyXhr(xhrType: any): void;
36
- private static proxyOpen;
37
- private static proxySend;
38
- private static proxyOnReadyStateChange;
39
- private static reportXhr;
40
- }