@datadog/mobile-react-native 2.3.3 → 2.3.4
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.
- package/android/src/main/kotlin/com/datadog/reactnative/DdRumImplementation.kt +9 -0
- package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
- package/android/src/newarch/kotlin/com/datadog/reactnative/DdRum.kt +8 -0
- package/android/src/oldarch/kotlin/com/datadog/reactnative/DdRum.kt +8 -0
- package/datadog-sourcemaps.gradle +13 -2
- package/ios/Sources/DdRum.mm +11 -0
- package/ios/Sources/DdRumImplementation.swift +7 -0
- package/ios/Sources/SdkVersion.swift +1 -1
- package/jest/mock.js +9 -1
- package/lib/commonjs/DdSdkReactNative.js +11 -4
- package/lib/commonjs/DdSdkReactNative.js.map +1 -1
- package/lib/commonjs/index.js +16 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/rum/DdRum.js +26 -12
- package/lib/commonjs/rum/DdRum.js.map +1 -1
- package/lib/commonjs/rum/types.js.map +1 -1
- package/lib/commonjs/sdk/DatadogProvider/Buffer/BoundedBuffer.js +14 -6
- package/lib/commonjs/sdk/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -1
- package/lib/commonjs/specs/NativeDdRum.js.map +1 -1
- package/lib/commonjs/trace/DdTrace.js +2 -2
- package/lib/commonjs/trace/DdTrace.js.map +1 -1
- package/lib/commonjs/utils/Timer.js +4 -4
- package/lib/commonjs/utils/Timer.js.map +1 -1
- package/lib/commonjs/utils/time-provider/DefaultTimeProvider.js +41 -0
- package/lib/commonjs/utils/time-provider/DefaultTimeProvider.js.map +1 -0
- package/lib/commonjs/utils/{TimeProvider.js → time-provider/TimeProvider.js} +4 -25
- package/lib/commonjs/utils/time-provider/TimeProvider.js.map +1 -0
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/DdSdkReactNative.js +11 -4
- package/lib/module/DdSdkReactNative.js.map +1 -1
- package/lib/module/index.js +3 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/rum/DdRum.js +25 -11
- package/lib/module/rum/DdRum.js.map +1 -1
- package/lib/module/rum/types.js.map +1 -1
- package/lib/module/sdk/DatadogProvider/Buffer/BoundedBuffer.js +14 -6
- package/lib/module/sdk/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -1
- package/lib/module/specs/NativeDdRum.js.map +1 -1
- package/lib/module/trace/DdTrace.js +2 -2
- package/lib/module/trace/DdTrace.js.map +1 -1
- package/lib/module/utils/Timer.js +4 -4
- package/lib/module/utils/Timer.js.map +1 -1
- package/lib/module/utils/time-provider/DefaultTimeProvider.js +36 -0
- package/lib/module/utils/time-provider/DefaultTimeProvider.js.map +1 -0
- package/lib/module/utils/{TimeProvider.js → time-provider/TimeProvider.js} +4 -25
- package/lib/module/utils/time-provider/TimeProvider.js.map +1 -0
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/DdSdkReactNative.d.ts +5 -1
- package/lib/typescript/index.d.ts +5 -1
- package/lib/typescript/rum/DdRum.d.ts +4 -0
- package/lib/typescript/rum/types.d.ts +4 -0
- package/lib/typescript/sdk/DatadogProvider/Buffer/BoundedBuffer.d.ts +2 -0
- package/lib/typescript/specs/NativeDdRum.d.ts +4 -0
- package/lib/typescript/utils/Timer.d.ts +1 -1
- package/lib/typescript/utils/time-provider/DefaultTimeProvider.d.ts +7 -0
- package/lib/typescript/utils/{TimeProvider.d.ts → time-provider/TimeProvider.d.ts} +4 -6
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/DdSdkReactNative.tsx +10 -4
- package/src/__tests__/DdSdkReactNative.test.tsx +2 -2
- package/src/__tests__/mock.test.ts +1 -1
- package/src/index.tsx +7 -0
- package/src/rum/DdRum.ts +24 -12
- package/src/rum/__mocks__/MockTimeProvider.ts +26 -0
- package/src/rum/__tests__/DdRum.test.ts +39 -13
- package/src/rum/__tests__/DdRumTimeProvider.test.ts +23 -0
- package/src/rum/types.ts +5 -0
- package/src/sdk/DatadogProvider/Buffer/BoundedBuffer.ts +22 -14
- package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +6 -5
- package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +5 -5
- package/src/specs/NativeDdRum.ts +5 -0
- package/src/trace/DdTrace.ts +2 -2
- package/src/utils/Timer.ts +5 -5
- package/src/utils/__tests__/TimeProvider.test.tsx +24 -7
- package/src/utils/__tests__/Timer.test.ts +38 -22
- package/src/utils/time-provider/DefaultTimeProvider.tsx +31 -0
- package/src/utils/{TimeProvider.tsx → time-provider/TimeProvider.tsx} +8 -27
- package/src/version.ts +1 -1
- package/lib/commonjs/utils/TimeProvider.js.map +0 -1
- 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 {
|
|
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/
|
|
27
|
+
jest.mock('../../../utils/time-provider/DefaultTimeProvider', () => {
|
|
28
28
|
const now = jest.fn();
|
|
29
29
|
return {
|
|
30
|
-
|
|
30
|
+
DefaultTimeProvider: jest.fn().mockImplementation(() => {
|
|
31
31
|
return { now };
|
|
32
32
|
})
|
|
33
33
|
};
|
|
34
34
|
});
|
|
35
|
-
|
|
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['
|
|
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 {
|
|
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/
|
|
28
|
+
jest.mock('../../../utils/time-provider/DefaultTimeProvider', () => {
|
|
29
29
|
const now = jest.fn();
|
|
30
30
|
return {
|
|
31
|
-
|
|
31
|
+
DefaultTimeProvider: jest.fn().mockImplementation(() => {
|
|
32
32
|
return { now };
|
|
33
33
|
})
|
|
34
34
|
};
|
|
35
35
|
});
|
|
36
|
-
const nowMock = new
|
|
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['
|
|
44
|
+
DdSdkReactNative['_isInitialized'] = false;
|
|
45
45
|
DdSdkReactNative['wasAutoInstrumented'] = false;
|
|
46
46
|
__internalResetIsInitializedForTesting();
|
|
47
47
|
BufferSingleton.reset();
|
package/src/specs/NativeDdRum.ts
CHANGED
|
@@ -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');
|
package/src/trace/DdTrace.ts
CHANGED
|
@@ -12,9 +12,9 @@ import {
|
|
|
12
12
|
bufferNativeCallWithId
|
|
13
13
|
} from '../sdk/DatadogProvider/Buffer/bufferNativeCall';
|
|
14
14
|
import type { DdTraceType } from '../types';
|
|
15
|
-
import {
|
|
15
|
+
import { DefaultTimeProvider } from '../utils/time-provider/DefaultTimeProvider';
|
|
16
16
|
|
|
17
|
-
const timeProvider = new
|
|
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
|
package/src/utils/Timer.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* Copyright 2016-Present Datadog, Inc.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import
|
|
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
|
|
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.
|
|
71
|
-
return end.
|
|
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
|
|
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
|
|
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.
|
|
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
|
|
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.
|
|
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
|
|
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.
|
|
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,
|
|
24
|
+
.mockReturnValueOnce({ unix: 5.0, reactNative: 7.0 })
|
|
25
25
|
.mockReturnValueOnce({
|
|
26
26
|
unix: 6.0,
|
|
27
|
-
|
|
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,
|
|
46
|
+
.mockReturnValueOnce({ unix: 5.0, reactNative: null })
|
|
45
47
|
.mockReturnValueOnce({
|
|
46
48
|
unix: 5.0 + expectedDuration,
|
|
47
|
-
|
|
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,
|
|
68
|
+
.mockReturnValueOnce({ unix: 5.0, reactNative: null })
|
|
65
69
|
.mockReturnValueOnce({
|
|
66
70
|
unix: 5.0 + expectedDuration,
|
|
67
|
-
|
|
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,
|
|
90
|
+
.mockReturnValueOnce({ unix: 5.0, reactNative: 13.0 })
|
|
85
91
|
.mockReturnValueOnce({
|
|
86
92
|
unix: 5.0 + expectedDuration,
|
|
87
|
-
|
|
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,
|
|
114
|
+
.mockReturnValueOnce({ unix: 5.0, reactNative: 13.0 })
|
|
107
115
|
.mockReturnValueOnce({
|
|
108
116
|
unix: 6.0,
|
|
109
|
-
|
|
117
|
+
reactNative: 13.0 + expectedDuration1
|
|
110
118
|
})
|
|
111
119
|
.mockReturnValueOnce({
|
|
112
120
|
unix: 6.0,
|
|
113
|
-
|
|
121
|
+
reactNative: 13.0 + expectedDuration1 + expectedDuration2
|
|
114
122
|
})
|
|
115
123
|
.mockReturnValueOnce({
|
|
116
124
|
unix: 6.0,
|
|
117
|
-
|
|
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,
|
|
152
|
+
.mockReturnValueOnce({ unix: 5.0, reactNative: null })
|
|
143
153
|
.mockReturnValueOnce({
|
|
144
154
|
unix: 5.0 + expectedDuration1,
|
|
145
|
-
|
|
155
|
+
reactNative: null
|
|
146
156
|
})
|
|
147
157
|
.mockReturnValueOnce({
|
|
148
158
|
unix: 5.0 + expectedDuration1 + expectedDuration2,
|
|
149
|
-
|
|
159
|
+
reactNative: null
|
|
150
160
|
})
|
|
151
161
|
.mockReturnValueOnce({
|
|
152
162
|
unix:
|
|
153
163
|
5.0 + expectedDuration1 + expectedDuration2 + expectedDuration3,
|
|
154
|
-
|
|
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,
|
|
204
|
+
.mockReturnValueOnce({ unix: 5.0, reactNative: 13.0 })
|
|
193
205
|
.mockReturnValueOnce({
|
|
194
206
|
unix: 6.0,
|
|
195
|
-
|
|
207
|
+
reactNative: 13.0 + expectedDuration1
|
|
196
208
|
})
|
|
197
209
|
.mockReturnValueOnce({
|
|
198
210
|
unix: 6.0,
|
|
199
|
-
|
|
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,
|
|
232
|
+
.mockReturnValueOnce({ unix: 5.0, reactNative: null })
|
|
219
233
|
.mockReturnValueOnce({
|
|
220
234
|
unix: 5.0 + expectedDuration1,
|
|
221
|
-
|
|
235
|
+
reactNative: null
|
|
222
236
|
})
|
|
223
237
|
.mockReturnValueOnce({
|
|
224
238
|
unix: 5.0 + expectedDuration1 + expectedDuration2,
|
|
225
|
-
|
|
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,34 @@ 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
|
-
|
|
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
|
-
|
|
24
|
+
protected baseOffset: number;
|
|
30
25
|
|
|
31
26
|
constructor() {
|
|
32
27
|
const timestamp = this.getTimestamp();
|
|
33
|
-
if (timestamp.
|
|
28
|
+
if (timestamp.reactNative == null) {
|
|
34
29
|
this.baseOffset = 0;
|
|
35
30
|
} else {
|
|
36
|
-
this.baseOffset = timestamp.unix - timestamp.
|
|
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.
|
|
39
|
+
if (timestamp.reactNative == null) {
|
|
50
40
|
return timestamp.unix;
|
|
51
41
|
} else {
|
|
52
|
-
return this.baseOffset + timestamp.
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
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();
|
|
42
|
+
return this.baseOffset + timestamp.reactNative;
|
|
61
43
|
}
|
|
62
|
-
return null;
|
|
63
44
|
}
|
|
64
45
|
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// generated by genversion
|
|
2
|
-
export const version = '2.3.
|
|
2
|
+
export const version = '2.3.4';
|
|
@@ -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"]}
|