Skip to content

Commit f28856c

Browse files
dschomvbudhram
authored andcommitted
bug(content): Fix flush time metric
Because: - We can see that some browsers on some platforms don't produce a valid flush time value. This Commit: - Uses a more robust check to determine if the window.performance api should be used, or if the fallback api should be used.
1 parent ca54317 commit f28856c

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

packages/fxa-shared/speed-trap/performance-factory.js

+24-20
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { NAVIGATION_TIMING_FIELDS } from './timing-fields'
5+
import { NAVIGATION_TIMING_FIELDS } from './timing-fields';
66

77
/**
88
* Small util for determining if a browser went to sleep.
99
*/
1010
class SleepDetection {
11-
1211
constructor() {
1312
this.sleepDetected = false;
14-
this.lastTime =Date.now();
13+
this.lastTime = Date.now();
1514
this.iid = '';
1615
}
1716

1817
startSleepDetection() {
1918
this.iid = setInterval(() => {
20-
if (this.sleepDetected) {
21-
clearInterval(this.iid);
22-
return;
23-
}
19+
if (this.sleepDetected) {
20+
clearInterval(this.iid);
21+
return;
22+
}
2423

25-
const currentTime = Date.now();
26-
if (currentTime > (this.lastTime + 2000*2)) { // ignore small delays
27-
this.sleepDetected = true;
28-
}
29-
this.lastTime = currentTime;
30-
}, 2000);
31-
}
24+
const currentTime = Date.now();
25+
if (currentTime > this.lastTime + 2000 * 2) {
26+
// ignore small delays
27+
this.sleepDetected = true;
28+
}
29+
this.lastTime = currentTime;
30+
}, 2000);
31+
}
3232
}
3333

3434
/**
@@ -39,7 +39,6 @@ class SleepDetection {
3939
* and the metrics collected will be wildly off.
4040
*/
4141
class PerformanceFallback {
42-
4342
constructor() {
4443
this.unreliable = true;
4544
this.timeOrigin = Date.now();
@@ -69,7 +68,7 @@ export function getFallbackPerformanceApi() {
6968
/**
7069
* Provides the browser's performance api.
7170
*/
72-
export function getRealPerformanceApi () {
71+
export function getRealPerformanceApi() {
7372
// eslint-disable-next-line no-undef
7473
return window.performance;
7574
}
@@ -79,10 +78,15 @@ export function getRealPerformanceApi () {
7978
* of it to support minimal functionality required by speed trap.
8079
*/
8180
export function getPerformanceApi() {
82-
// eslint-disable-next-line no-undef
83-
if (!!window.performance && typeof window.performance.now === 'function') {
84-
return getRealPerformanceApi();
85-
}
81+
const api = getRealPerformanceApi();
82+
// If the api can produce a time origin and a valid now, let's use it.
83+
try {
84+
const check = api.timeOrigin + api.now();
85+
if (typeof check === 'number' && check > 0) {
86+
return api;
87+
}
88+
} catch (err) {}
8689

90+
// Otherwise return the fallback api
8791
return getFallbackPerformanceApi();
8892
}

0 commit comments

Comments
 (0)