@datadog/mobile-react-native 2.3.3 → 2.3.5

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 (82) hide show
  1. package/android/src/main/kotlin/com/datadog/reactnative/DdRumImplementation.kt +9 -0
  2. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  3. package/android/src/newarch/kotlin/com/datadog/reactnative/DdRum.kt +8 -0
  4. package/android/src/oldarch/kotlin/com/datadog/reactnative/DdRum.kt +8 -0
  5. package/datadog-sourcemaps.gradle +13 -2
  6. package/ios/Sources/DdRum.mm +11 -0
  7. package/ios/Sources/DdRumImplementation.swift +7 -0
  8. package/ios/Sources/SdkVersion.swift +1 -1
  9. package/jest/mock.js +9 -1
  10. package/lib/commonjs/DdSdkReactNative.js +11 -4
  11. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  12. package/lib/commonjs/index.js +16 -0
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/rum/DdRum.js +26 -12
  15. package/lib/commonjs/rum/DdRum.js.map +1 -1
  16. package/lib/commonjs/rum/types.js.map +1 -1
  17. package/lib/commonjs/sdk/DatadogProvider/Buffer/BoundedBuffer.js +14 -6
  18. package/lib/commonjs/sdk/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -1
  19. package/lib/commonjs/specs/NativeDdRum.js.map +1 -1
  20. package/lib/commonjs/trace/DdTrace.js +2 -2
  21. package/lib/commonjs/trace/DdTrace.js.map +1 -1
  22. package/lib/commonjs/utils/Timer.js +4 -4
  23. package/lib/commonjs/utils/Timer.js.map +1 -1
  24. package/lib/commonjs/utils/time-provider/DefaultTimeProvider.js +41 -0
  25. package/lib/commonjs/utils/time-provider/DefaultTimeProvider.js.map +1 -0
  26. package/lib/commonjs/utils/{TimeProvider.js → time-provider/TimeProvider.js} +8 -25
  27. package/lib/commonjs/utils/time-provider/TimeProvider.js.map +1 -0
  28. package/lib/commonjs/version.js +1 -1
  29. package/lib/commonjs/version.js.map +1 -1
  30. package/lib/module/DdSdkReactNative.js +11 -4
  31. package/lib/module/DdSdkReactNative.js.map +1 -1
  32. package/lib/module/index.js +3 -1
  33. package/lib/module/index.js.map +1 -1
  34. package/lib/module/rum/DdRum.js +25 -11
  35. package/lib/module/rum/DdRum.js.map +1 -1
  36. package/lib/module/rum/types.js.map +1 -1
  37. package/lib/module/sdk/DatadogProvider/Buffer/BoundedBuffer.js +14 -6
  38. package/lib/module/sdk/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -1
  39. package/lib/module/specs/NativeDdRum.js.map +1 -1
  40. package/lib/module/trace/DdTrace.js +2 -2
  41. package/lib/module/trace/DdTrace.js.map +1 -1
  42. package/lib/module/utils/Timer.js +4 -4
  43. package/lib/module/utils/Timer.js.map +1 -1
  44. package/lib/module/utils/time-provider/DefaultTimeProvider.js +36 -0
  45. package/lib/module/utils/time-provider/DefaultTimeProvider.js.map +1 -0
  46. package/lib/module/utils/{TimeProvider.js → time-provider/TimeProvider.js} +8 -25
  47. package/lib/module/utils/time-provider/TimeProvider.js.map +1 -0
  48. package/lib/module/version.js +1 -1
  49. package/lib/module/version.js.map +1 -1
  50. package/lib/typescript/DdSdkReactNative.d.ts +5 -1
  51. package/lib/typescript/index.d.ts +5 -1
  52. package/lib/typescript/rum/DdRum.d.ts +4 -0
  53. package/lib/typescript/rum/types.d.ts +4 -0
  54. package/lib/typescript/sdk/DatadogProvider/Buffer/BoundedBuffer.d.ts +2 -0
  55. package/lib/typescript/specs/NativeDdRum.d.ts +4 -0
  56. package/lib/typescript/utils/Timer.d.ts +1 -1
  57. package/lib/typescript/utils/time-provider/DefaultTimeProvider.d.ts +7 -0
  58. package/lib/typescript/utils/{TimeProvider.d.ts → time-provider/TimeProvider.d.ts} +4 -6
  59. package/lib/typescript/version.d.ts +1 -1
  60. package/package.json +2 -2
  61. package/src/DdSdkReactNative.tsx +10 -4
  62. package/src/__tests__/DdSdkReactNative.test.tsx +2 -2
  63. package/src/__tests__/mock.test.ts +1 -1
  64. package/src/index.tsx +7 -0
  65. package/src/rum/DdRum.ts +24 -12
  66. package/src/rum/__mocks__/MockTimeProvider.ts +26 -0
  67. package/src/rum/__tests__/DdRum.test.ts +39 -13
  68. package/src/rum/__tests__/DdRumTimeProvider.test.ts +23 -0
  69. package/src/rum/types.ts +5 -0
  70. package/src/sdk/DatadogProvider/Buffer/BoundedBuffer.ts +22 -14
  71. package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +6 -5
  72. package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +5 -5
  73. package/src/specs/NativeDdRum.ts +5 -0
  74. package/src/trace/DdTrace.ts +2 -2
  75. package/src/utils/Timer.ts +5 -5
  76. package/src/utils/__tests__/TimeProvider.test.tsx +24 -7
  77. package/src/utils/__tests__/Timer.test.ts +38 -22
  78. package/src/utils/time-provider/DefaultTimeProvider.tsx +31 -0
  79. package/src/utils/{TimeProvider.tsx → time-provider/TimeProvider.tsx} +13 -27
  80. package/src/version.ts +1 -1
  81. package/lib/commonjs/utils/TimeProvider.js.map +0 -1
  82. package/lib/module/utils/TimeProvider.js.map +0 -1
@@ -11,7 +11,7 @@ import { DdSdkReactNative } from '../../../DdSdkReactNative';
11
11
  import { DdRum } from '../../../rum/DdRum';
12
12
  import { RumActionType } from '../../../rum/types';
13
13
  import { DdTrace } from '../../../trace/DdTrace';
14
- import { TimeProvider } from '../../../utils/TimeProvider';
14
+ import { DefaultTimeProvider } from '../../../utils/time-provider/DefaultTimeProvider';
15
15
  import { BufferSingleton } from '../Buffer/BufferSingleton';
16
16
  import {
17
17
  DatadogProvider,
@@ -24,15 +24,16 @@ import {
24
24
  renderWithProvider
25
25
  } from './__utils__/renderWithProvider';
26
26
 
27
- jest.mock('../../../utils/TimeProvider', () => {
27
+ jest.mock('../../../utils/time-provider/DefaultTimeProvider', () => {
28
28
  const now = jest.fn();
29
29
  return {
30
- TimeProvider: jest.fn().mockImplementation(() => {
30
+ DefaultTimeProvider: jest.fn().mockImplementation(() => {
31
31
  return { now };
32
32
  })
33
33
  };
34
34
  });
35
- const nowMock = new TimeProvider().now;
35
+
36
+ const nowMock = new DefaultTimeProvider().now;
36
37
 
37
38
  const flushPromises = () =>
38
39
  new Promise<void>(jest.requireActual('timers').setImmediate);
@@ -40,7 +41,7 @@ const flushPromises = () =>
40
41
  describe('DatadogProvider', () => {
41
42
  afterEach(() => {
42
43
  jest.clearAllMocks();
43
- DdSdkReactNative['wasInitialized'] = false;
44
+ DdSdkReactNative['_isInitialized'] = false;
44
45
  __internalResetIsInitializedForTesting();
45
46
  BufferSingleton.reset();
46
47
  (nowMock as any).mockReturnValue('timestamp_not_specified');
@@ -11,7 +11,7 @@ import { InitializationMode } from '../../../DdSdkReactNativeConfiguration';
11
11
  import { DdSdkReactNative } from '../../../DdSdkReactNative';
12
12
  import { DdRumUserInteractionTracking } from '../../../rum/instrumentation/interactionTracking/DdRumUserInteractionTracking';
13
13
  import { XMLHttpRequestMock } from '../../../rum/instrumentation/resourceTracking/__tests__/__utils__/XMLHttpRequestMock';
14
- import { TimeProvider } from '../../../utils/TimeProvider';
14
+ import { DefaultTimeProvider } from '../../../utils/time-provider/DefaultTimeProvider';
15
15
  import { BufferSingleton } from '../Buffer/BufferSingleton';
16
16
  import {
17
17
  DatadogProvider,
@@ -25,15 +25,15 @@ import {
25
25
  renderWithProviderAndAnimation
26
26
  } from './__utils__/renderWithProvider';
27
27
 
28
- jest.mock('../../../utils/TimeProvider', () => {
28
+ jest.mock('../../../utils/time-provider/DefaultTimeProvider', () => {
29
29
  const now = jest.fn();
30
30
  return {
31
- TimeProvider: jest.fn().mockImplementation(() => {
31
+ DefaultTimeProvider: jest.fn().mockImplementation(() => {
32
32
  return { now };
33
33
  })
34
34
  };
35
35
  });
36
- const nowMock = new TimeProvider().now;
36
+ const nowMock = new DefaultTimeProvider().now;
37
37
 
38
38
  const flushPromises = () =>
39
39
  new Promise<void>(jest.requireActual('timers').setImmediate);
@@ -41,7 +41,7 @@ const flushPromises = () =>
41
41
  describe('DatadogProvider', () => {
42
42
  beforeEach(() => {
43
43
  jest.clearAllMocks();
44
- DdSdkReactNative['wasInitialized'] = false;
44
+ DdSdkReactNative['_isInitialized'] = false;
45
45
  DdSdkReactNative['wasAutoInstrumented'] = false;
46
46
  __internalResetIsInitializedForTesting();
47
47
  BufferSingleton.reset();
@@ -146,6 +146,11 @@ export interface Spec extends TurboModule {
146
146
  * @param value: The value of the feature flag, encapsulated in an Object to accept all types
147
147
  */
148
148
  addFeatureFlagEvaluation(name: string, value: Object): Promise<void>;
149
+
150
+ /**
151
+ * Get current Session ID, or `undefined` if not available.
152
+ */
153
+ getCurrentSessionId(): Promise<string | undefined>;
149
154
  }
150
155
 
151
156
  export default TurboModuleRegistry.get<Spec>('DdRum');
@@ -12,9 +12,9 @@ import {
12
12
  bufferNativeCallWithId
13
13
  } from '../sdk/DatadogProvider/Buffer/bufferNativeCall';
14
14
  import type { DdTraceType } from '../types';
15
- import { TimeProvider } from '../utils/TimeProvider';
15
+ import { DefaultTimeProvider } from '../utils/time-provider/DefaultTimeProvider';
16
16
 
17
- const timeProvider = new TimeProvider();
17
+ const timeProvider = new DefaultTimeProvider();
18
18
 
19
19
  class DdTraceWrapper implements DdTraceType {
20
20
  // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
@@ -4,8 +4,8 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import type { Timestamp } from './TimeProvider';
8
- import { TimeProvider } from './TimeProvider';
7
+ import { DefaultTimeProvider } from './time-provider/DefaultTimeProvider';
8
+ import type { TimeProvider, Timestamp } from './time-provider/TimeProvider';
9
9
 
10
10
  const START_LABEL = '__start';
11
11
  const STOP_LABEL = '__stop';
@@ -18,7 +18,7 @@ export default class Timer {
18
18
  private timeProvider: TimeProvider;
19
19
  private times: Record<string, Timestamp> = {};
20
20
 
21
- constructor(timeProvider: TimeProvider = new TimeProvider()) {
21
+ constructor(timeProvider: TimeProvider = new DefaultTimeProvider()) {
22
22
  this.timeProvider = timeProvider;
23
23
  }
24
24
 
@@ -67,8 +67,8 @@ export default class Timer {
67
67
  }
68
68
 
69
69
  private durationBetweenTicks(start: Timestamp, end: Timestamp): number {
70
- if (start.react_native != null && end.react_native != null) {
71
- return end.react_native - start.react_native;
70
+ if (start.reactNative != null && end.reactNative != null) {
71
+ return end.reactNative - start.reactNative;
72
72
  }
73
73
  return end.unix - start.unix;
74
74
  }
@@ -4,7 +4,8 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import { TimeProvider } from '../TimeProvider';
7
+ import MockTimeProvider from '../../rum/__mocks__/MockTimeProvider';
8
+ import { DefaultTimeProvider } from '../time-provider/DefaultTimeProvider';
8
9
 
9
10
  function mockDateNow(value: number) {
10
11
  Date.now = (): number => {
@@ -38,38 +39,54 @@ beforeEach(() => {
38
39
 
39
40
  it('M use performance W available', () => {
40
41
  // GIVEN
41
- const timeProvider = new TimeProvider();
42
+ const timeProvider = new DefaultTimeProvider();
42
43
 
43
44
  // WHEN
44
45
  const result = timeProvider.getTimestamp();
45
46
 
46
47
  // THEN
47
48
  expect(result.unix).toBe(dateTime);
48
- expect(result.react_native).toBe(perfTime);
49
+ expect(result.reactNative).toBe(perfTime);
50
+ });
51
+
52
+ it('M use unix time W reactNative time unavailable', () => {
53
+ // GIVEN
54
+ const timeProvider = new MockTimeProvider(1000, null);
55
+
56
+ // WHEN
57
+ const now = timeProvider.now();
58
+ const timestamp = timeProvider.getTimestamp();
59
+
60
+ // THEN
61
+ expect(timestamp.unix).toBe(1000);
62
+ expect(timestamp.reactNative).toBe(null);
63
+ expect(now).toBe(1000);
49
64
  });
50
65
 
51
66
  it('M ignore performance W global.performance unavailable', () => {
52
67
  // GIVEN
68
+ // @ts-expect-error performance is not supposed to be null, but we treat it as such for testing purposes
53
69
  delete global.performance;
54
- const timeProvider = new TimeProvider();
70
+ const timeProvider = new DefaultTimeProvider();
55
71
 
56
72
  // WHEN
57
73
  const result = timeProvider.getTimestamp();
58
74
 
59
75
  // THEN
60
76
  expect(result.unix).toBe(dateTime);
61
- expect(result.react_native).toBe(null);
77
+ expect(result.reactNative).toBe(null);
62
78
  });
63
79
 
64
80
  it('M ignore performance W unavailable', () => {
65
81
  // GIVEN
82
+ // @ts-expect-error performance.now is not supposed to be null, but we treat it as such for testing purposes
66
83
  delete global.performance.now;
67
- const timeProvider = new TimeProvider();
84
+ const timeProvider = new DefaultTimeProvider();
68
85
 
69
86
  // WHEN
70
87
  const result = timeProvider.getTimestamp();
71
88
 
72
89
  // THEN
73
90
  expect(result.unix).toBe(dateTime);
74
- expect(result.react_native).toBe(null);
91
+ expect(result.reactNative).toBe(null);
75
92
  });
@@ -21,11 +21,13 @@ it('M use performance W available', () => {
21
21
  // GIVEN
22
22
  const expectedDuration = randomInt();
23
23
  mockTimeProvider.getTimestamp
24
- .mockReturnValueOnce({ unix: 5.0, react_native: 7.0 })
24
+ .mockReturnValueOnce({ unix: 5.0, reactNative: 7.0 })
25
25
  .mockReturnValueOnce({
26
26
  unix: 6.0,
27
- react_native: 7.0 + expectedDuration
27
+ reactNative: 7.0 + expectedDuration
28
28
  });
29
+
30
+ // @ts-expect-error mockTimeProvider does not mock all the TimeProvider public methods
29
31
  const timer = new Timer(mockTimeProvider);
30
32
 
31
33
  // WHEN
@@ -41,11 +43,13 @@ it('M use Date W performance data is never available', () => {
41
43
  // GIVEN
42
44
  const expectedDuration = randomInt();
43
45
  mockTimeProvider.getTimestamp
44
- .mockReturnValueOnce({ unix: 5.0, react_native: null })
46
+ .mockReturnValueOnce({ unix: 5.0, reactNative: null })
45
47
  .mockReturnValueOnce({
46
48
  unix: 5.0 + expectedDuration,
47
- react_native: null
49
+ reactNative: null
48
50
  });
51
+
52
+ // @ts-expect-error mockTimeProvider does not mock all the TimeProvider public methods
49
53
  const timer = new Timer(mockTimeProvider);
50
54
 
51
55
  // WHEN
@@ -61,11 +65,13 @@ it('M use Date W performance data is not available on start', () => {
61
65
  // GIVEN
62
66
  const expectedDuration = randomInt();
63
67
  mockTimeProvider.getTimestamp
64
- .mockReturnValueOnce({ unix: 5.0, react_native: null })
68
+ .mockReturnValueOnce({ unix: 5.0, reactNative: null })
65
69
  .mockReturnValueOnce({
66
70
  unix: 5.0 + expectedDuration,
67
- react_native: 13.0
71
+ reactNative: 13.0
68
72
  });
73
+
74
+ // @ts-expect-error mockTimeProvider does not mock all the TimeProvider public methods
69
75
  const timer = new Timer(mockTimeProvider);
70
76
 
71
77
  // WHEN
@@ -81,11 +87,13 @@ it('M use Date W performance data is not available on stop', () => {
81
87
  // GIVEN
82
88
  const expectedDuration = randomInt();
83
89
  mockTimeProvider.getTimestamp
84
- .mockReturnValueOnce({ unix: 5.0, react_native: 13.0 })
90
+ .mockReturnValueOnce({ unix: 5.0, reactNative: 13.0 })
85
91
  .mockReturnValueOnce({
86
92
  unix: 5.0 + expectedDuration,
87
- react_native: null
93
+ reactNative: null
88
94
  });
95
+
96
+ // @ts-expect-error mockTimeProvider does not mock all the TimeProvider public methods
89
97
  const timer = new Timer(mockTimeProvider);
90
98
 
91
99
  // WHEN
@@ -103,20 +111,22 @@ it('M return duration between two ticks W performance is available', () => {
103
111
  const expectedDuration2 = randomInt();
104
112
  const expectedDuration3 = randomInt();
105
113
  mockTimeProvider.getTimestamp
106
- .mockReturnValueOnce({ unix: 5.0, react_native: 13.0 })
114
+ .mockReturnValueOnce({ unix: 5.0, reactNative: 13.0 })
107
115
  .mockReturnValueOnce({
108
116
  unix: 6.0,
109
- react_native: 13.0 + expectedDuration1
117
+ reactNative: 13.0 + expectedDuration1
110
118
  })
111
119
  .mockReturnValueOnce({
112
120
  unix: 6.0,
113
- react_native: 13.0 + expectedDuration1 + expectedDuration2
121
+ reactNative: 13.0 + expectedDuration1 + expectedDuration2
114
122
  })
115
123
  .mockReturnValueOnce({
116
124
  unix: 6.0,
117
- react_native:
125
+ reactNative:
118
126
  13.0 + expectedDuration1 + expectedDuration2 + expectedDuration3
119
127
  });
128
+
129
+ // @ts-expect-error mockTimeProvider does not mock all the TimeProvider public methods
120
130
  const timer = new Timer(mockTimeProvider);
121
131
 
122
132
  // WHEN
@@ -139,20 +149,22 @@ it('M return duration between two ticks W performance is not available', () => {
139
149
  const expectedDuration2 = randomInt();
140
150
  const expectedDuration3 = randomInt();
141
151
  mockTimeProvider.getTimestamp
142
- .mockReturnValueOnce({ unix: 5.0, react_native: null })
152
+ .mockReturnValueOnce({ unix: 5.0, reactNative: null })
143
153
  .mockReturnValueOnce({
144
154
  unix: 5.0 + expectedDuration1,
145
- react_native: null
155
+ reactNative: null
146
156
  })
147
157
  .mockReturnValueOnce({
148
158
  unix: 5.0 + expectedDuration1 + expectedDuration2,
149
- react_native: null
159
+ reactNative: null
150
160
  })
151
161
  .mockReturnValueOnce({
152
162
  unix:
153
163
  5.0 + expectedDuration1 + expectedDuration2 + expectedDuration3,
154
- react_native: null
164
+ reactNative: null
155
165
  });
166
+
167
+ // @ts-expect-error mockTimeProvider does not mock all the TimeProvider public methods
156
168
  const timer = new Timer(mockTimeProvider);
157
169
 
158
170
  // WHEN
@@ -189,15 +201,17 @@ it('M return time for tick W performance is available', () => {
189
201
  const expectedDuration2 = randomInt();
190
202
 
191
203
  mockTimeProvider.getTimestamp
192
- .mockReturnValueOnce({ unix: 5.0, react_native: 13.0 })
204
+ .mockReturnValueOnce({ unix: 5.0, reactNative: 13.0 })
193
205
  .mockReturnValueOnce({
194
206
  unix: 6.0,
195
- react_native: 13.0 + expectedDuration1
207
+ reactNative: 13.0 + expectedDuration1
196
208
  })
197
209
  .mockReturnValueOnce({
198
210
  unix: 6.0,
199
- react_native: 13.0 + expectedDuration1 + expectedDuration2
211
+ reactNative: 13.0 + expectedDuration1 + expectedDuration2
200
212
  });
213
+
214
+ // @ts-expect-error mockTimeProvider does not mock all the TimeProvider public methods
201
215
  const timer = new Timer(mockTimeProvider);
202
216
 
203
217
  // WHEN
@@ -215,15 +229,17 @@ it('M return time for tick W performance is not available', () => {
215
229
  const expectedDuration2 = randomInt();
216
230
 
217
231
  mockTimeProvider.getTimestamp
218
- .mockReturnValueOnce({ unix: 5.0, react_native: null })
232
+ .mockReturnValueOnce({ unix: 5.0, reactNative: null })
219
233
  .mockReturnValueOnce({
220
234
  unix: 5.0 + expectedDuration1,
221
- react_native: null
235
+ reactNative: null
222
236
  })
223
237
  .mockReturnValueOnce({
224
238
  unix: 5.0 + expectedDuration1 + expectedDuration2,
225
- react_native: null
239
+ reactNative: null
226
240
  });
241
+
242
+ // @ts-expect-error mockTimeProvider does not mock all the TimeProvider public methods
227
243
  const timer = new Timer(mockTimeProvider);
228
244
 
229
245
  // WHEN
@@ -0,0 +1,31 @@
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 { TimeProvider } from './TimeProvider';
8
+ import type { Timestamp } from './TimeProvider';
9
+
10
+ export class DefaultTimeProvider extends TimeProvider {
11
+ private canUsePerformanceNow =
12
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
13
+ // @ts-ignore
14
+ global.performance && typeof performance.now === 'function';
15
+
16
+ getTimestamp(): Timestamp {
17
+ return {
18
+ unix: Date.now(),
19
+ reactNative: this.performanceNow()
20
+ };
21
+ }
22
+
23
+ private performanceNow(): number | null {
24
+ if (this.canUsePerformanceNow) {
25
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
26
+ // @ts-ignore
27
+ return performance.now();
28
+ }
29
+ return null;
30
+ }
31
+ }
@@ -12,53 +12,39 @@ export type Timestamp = {
12
12
  unix: number;
13
13
  // Result of performance.now API. Timestamp in ms (with microsecond precision)
14
14
  // since JS context start.
15
- react_native: number | null;
15
+ reactNative: number | null;
16
16
  };
17
17
 
18
18
  /**
19
19
  * Simple class providing timestamps in milliseconds.
20
20
  * If available, it will use the `performance.now()` method, and will fallback on `Date.now()` otherwise.
21
21
  */
22
- export class TimeProvider {
23
- private canUsePerformanceNow =
24
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
25
- // @ts-ignore
26
- global.performance && typeof performance.now === 'function';
27
-
22
+ export abstract class TimeProvider {
28
23
  /** Keeps an average offset between the unix time and the provided timestamp. */
29
- private baseOffset: number;
24
+ protected baseOffset: number;
30
25
 
31
26
  constructor() {
32
27
  const timestamp = this.getTimestamp();
33
- if (timestamp.react_native == null) {
28
+ if (timestamp.reactNative == null) {
34
29
  this.baseOffset = 0;
35
30
  } else {
36
- this.baseOffset = timestamp.unix - timestamp.react_native;
31
+ this.baseOffset = timestamp.unix - timestamp.reactNative;
37
32
  }
38
33
  }
39
34
 
40
- getTimestamp(): Timestamp {
41
- return {
42
- unix: Date.now(),
43
- react_native: this.performanceNow()
44
- };
45
- }
35
+ abstract getTimestamp(): Timestamp;
46
36
 
47
37
  now(): number {
48
38
  const timestamp = this.getTimestamp();
49
- if (timestamp.react_native == null) {
50
- return timestamp.unix;
51
- } else {
52
- return this.baseOffset + timestamp.react_native;
39
+
40
+ if (timestamp.reactNative != null && this.baseOffset === 0) {
41
+ this.baseOffset = timestamp.unix - timestamp.reactNative;
53
42
  }
54
- }
55
43
 
56
- private performanceNow(): number | null {
57
- if (this.canUsePerformanceNow) {
58
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
59
- // @ts-ignore
60
- return performance.now();
44
+ if (timestamp.reactNative == null || this.baseOffset === 0) {
45
+ return timestamp.unix;
46
+ } else {
47
+ return this.baseOffset + timestamp.reactNative;
61
48
  }
62
- return null;
63
49
  }
64
50
  }
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // generated by genversion
2
- export const version = '2.3.3';
2
+ export const version = '2.3.5';
@@ -1 +0,0 @@
1
- {"version":3,"sources":["TimeProvider.tsx"],"names":["TimeProvider","constructor","global","performance","now","timestamp","getTimestamp","react_native","baseOffset","unix","Date","performanceNow","canUsePerformanceNow"],"mappings":";;;;;;;;;AAAA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AASA;AACA;AACA;AACA;AACO,MAAMA,YAAN,CAAmB;AAMtB;AAGAC,EAAAA,WAAW,GAAG;AAAA,kDAPV;AACA;AACAC,IAAAA,MAAM,CAACC,WAAP,IAAsB,OAAOA,WAAW,CAACC,GAAnB,KAA2B,UAKvC;;AAAA;;AACV,UAAMC,SAAS,GAAG,KAAKC,YAAL,EAAlB;;AACA,QAAID,SAAS,CAACE,YAAV,IAA0B,IAA9B,EAAoC;AAChC,WAAKC,UAAL,GAAkB,CAAlB;AACH,KAFD,MAEO;AACH,WAAKA,UAAL,GAAkBH,SAAS,CAACI,IAAV,GAAiBJ,SAAS,CAACE,YAA7C;AACH;AACJ;;AAEDD,EAAAA,YAAY,GAAc;AACtB,WAAO;AACHG,MAAAA,IAAI,EAAEC,IAAI,CAACN,GAAL,EADH;AAEHG,MAAAA,YAAY,EAAE,KAAKI,cAAL;AAFX,KAAP;AAIH;;AAEDP,EAAAA,GAAG,GAAW;AACV,UAAMC,SAAS,GAAG,KAAKC,YAAL,EAAlB;;AACA,QAAID,SAAS,CAACE,YAAV,IAA0B,IAA9B,EAAoC;AAChC,aAAOF,SAAS,CAACI,IAAjB;AACH,KAFD,MAEO;AACH,aAAO,KAAKD,UAAL,GAAkBH,SAAS,CAACE,YAAnC;AACH;AACJ;;AAEOI,EAAAA,cAAc,GAAkB;AACpC,QAAI,KAAKC,oBAAT,EAA+B;AAC3B;AACA;AACA,aAAOT,WAAW,CAACC,GAAZ,EAAP;AACH;;AACD,WAAO,IAAP;AACH;;AAzCqB","sourcesContent":["/*\n * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.\n * This product includes software developed at Datadog (https://www.datadoghq.com/).\n * Copyright 2016-Present Datadog, Inc.\n */\n\n/**\n * A Timestamp structure holding the\n */\nexport type Timestamp = {\n // Result of Date API. Unix timestamp in ms.\n unix: number;\n // Result of performance.now API. Timestamp in ms (with microsecond precision)\n // since JS context start.\n react_native: number | null;\n};\n\n/**\n * Simple class providing timestamps in milliseconds.\n * If available, it will use the `performance.now()` method, and will fallback on `Date.now()` otherwise.\n */\nexport class TimeProvider {\n private canUsePerformanceNow =\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n global.performance && typeof performance.now === 'function';\n\n /** Keeps an average offset between the unix time and the provided timestamp. */\n private baseOffset: number;\n\n constructor() {\n const timestamp = this.getTimestamp();\n if (timestamp.react_native == null) {\n this.baseOffset = 0;\n } else {\n this.baseOffset = timestamp.unix - timestamp.react_native;\n }\n }\n\n getTimestamp(): Timestamp {\n return {\n unix: Date.now(),\n react_native: this.performanceNow()\n };\n }\n\n now(): number {\n const timestamp = this.getTimestamp();\n if (timestamp.react_native == null) {\n return timestamp.unix;\n } else {\n return this.baseOffset + timestamp.react_native;\n }\n }\n\n private performanceNow(): number | null {\n if (this.canUsePerformanceNow) {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n return performance.now();\n }\n return null;\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["TimeProvider.tsx"],"names":["TimeProvider","constructor","global","performance","now","timestamp","getTimestamp","react_native","baseOffset","unix","Date","performanceNow","canUsePerformanceNow"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AASA;AACA;AACA;AACA;AACA,OAAO,MAAMA,YAAN,CAAmB;AAMtB;AAGAC,EAAAA,WAAW,GAAG;AAAA,kDAPV;AACA;AACAC,IAAAA,MAAM,CAACC,WAAP,IAAsB,OAAOA,WAAW,CAACC,GAAnB,KAA2B,UAKvC;;AAAA;;AACV,UAAMC,SAAS,GAAG,KAAKC,YAAL,EAAlB;;AACA,QAAID,SAAS,CAACE,YAAV,IAA0B,IAA9B,EAAoC;AAChC,WAAKC,UAAL,GAAkB,CAAlB;AACH,KAFD,MAEO;AACH,WAAKA,UAAL,GAAkBH,SAAS,CAACI,IAAV,GAAiBJ,SAAS,CAACE,YAA7C;AACH;AACJ;;AAEDD,EAAAA,YAAY,GAAc;AACtB,WAAO;AACHG,MAAAA,IAAI,EAAEC,IAAI,CAACN,GAAL,EADH;AAEHG,MAAAA,YAAY,EAAE,KAAKI,cAAL;AAFX,KAAP;AAIH;;AAEDP,EAAAA,GAAG,GAAW;AACV,UAAMC,SAAS,GAAG,KAAKC,YAAL,EAAlB;;AACA,QAAID,SAAS,CAACE,YAAV,IAA0B,IAA9B,EAAoC;AAChC,aAAOF,SAAS,CAACI,IAAjB;AACH,KAFD,MAEO;AACH,aAAO,KAAKD,UAAL,GAAkBH,SAAS,CAACE,YAAnC;AACH;AACJ;;AAEOI,EAAAA,cAAc,GAAkB;AACpC,QAAI,KAAKC,oBAAT,EAA+B;AAC3B;AACA;AACA,aAAOT,WAAW,CAACC,GAAZ,EAAP;AACH;;AACD,WAAO,IAAP;AACH;;AAzCqB","sourcesContent":["/*\n * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.\n * This product includes software developed at Datadog (https://www.datadoghq.com/).\n * Copyright 2016-Present Datadog, Inc.\n */\n\n/**\n * A Timestamp structure holding the\n */\nexport type Timestamp = {\n // Result of Date API. Unix timestamp in ms.\n unix: number;\n // Result of performance.now API. Timestamp in ms (with microsecond precision)\n // since JS context start.\n react_native: number | null;\n};\n\n/**\n * Simple class providing timestamps in milliseconds.\n * If available, it will use the `performance.now()` method, and will fallback on `Date.now()` otherwise.\n */\nexport class TimeProvider {\n private canUsePerformanceNow =\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n global.performance && typeof performance.now === 'function';\n\n /** Keeps an average offset between the unix time and the provided timestamp. */\n private baseOffset: number;\n\n constructor() {\n const timestamp = this.getTimestamp();\n if (timestamp.react_native == null) {\n this.baseOffset = 0;\n } else {\n this.baseOffset = timestamp.unix - timestamp.react_native;\n }\n }\n\n getTimestamp(): Timestamp {\n return {\n unix: Date.now(),\n react_native: this.performanceNow()\n };\n }\n\n now(): number {\n const timestamp = this.getTimestamp();\n if (timestamp.react_native == null) {\n return timestamp.unix;\n } else {\n return this.baseOffset + timestamp.react_native;\n }\n }\n\n private performanceNow(): number | null {\n if (this.canUsePerformanceNow) {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n return performance.now();\n }\n return null;\n }\n}\n"]}