Commit df6c44d2 authored by eddie's avatar eddie Committed by GitHub

fix: use Date as a performance tracker fallback (#7349)

parent 59e7a286
import { SwapEventTimestampTracker, SwapEventType } from './SwapEventTimestampTracker' import { SwapEventTimestampTracker, SwapEventType } from './SwapEventTimestampTracker'
jest.mock('./utils', () => ({ jest.mock('./utils', () => ({
calculateElapsedTimeWithPerformanceMark: (mark: string) => { calculateElapsedTimeWithPerformanceMarkMs: (mark: string) => {
switch (mark) { switch (mark) {
case SwapEventType.FIRST_SWAP_ACTION: case SwapEventType.FIRST_SWAP_ACTION:
return 100 return 100
......
import { calculateElapsedTimeWithPerformanceMark } from './utils' import { calculateElapsedTimeWithPerformanceMarkMs } from './utils'
// These events should happen in this order. // These events should happen in this order.
export enum SwapEventType { export enum SwapEventType {
...@@ -18,6 +18,7 @@ export enum SwapEventType { ...@@ -18,6 +18,7 @@ export enum SwapEventType {
export class SwapEventTimestampTracker { export class SwapEventTimestampTracker {
private static _instance: SwapEventTimestampTracker private static _instance: SwapEventTimestampTracker
private createdAt = Date.now()
private constructor() { private constructor() {
// Private constructor to prevent direct construction calls with the `new` operator. // Private constructor to prevent direct construction calls with the `new` operator.
} }
...@@ -36,7 +37,7 @@ export class SwapEventTimestampTracker { ...@@ -36,7 +37,7 @@ export class SwapEventTimestampTracker {
public setElapsedTime(eventType: SwapEventType): number | undefined { public setElapsedTime(eventType: SwapEventType): number | undefined {
if (this.timestamps.has(eventType)) return undefined if (this.timestamps.has(eventType)) return undefined
const elapsedTime = calculateElapsedTimeWithPerformanceMark(eventType) const elapsedTime = calculateElapsedTimeWithPerformanceMarkMs(eventType, this.createdAt)
if (elapsedTime) { if (elapsedTime) {
this.timestamps.set(eventType, elapsedTime) this.timestamps.set(eventType, elapsedTime)
} }
......
/** /**
* Returns the time elapsed between page load and now. * Returns the time elapsed between page load and now in milliseconds.
* @param markName the identifier for the performance mark to be created and measured. * @param markName the identifier for the performance mark to be created and measured.
*/ */
export function calculateElapsedTimeWithPerformanceMark(markName: string): number | undefined { export function calculateElapsedTimeWithPerformanceMarkMs(
markName: string,
fallbackStartTime?: number
): number | undefined {
const elapsedTime = performance.mark(markName) const elapsedTime = performance.mark(markName)
if (!elapsedTime) return undefined if (elapsedTime) {
return elapsedTime.startTime return elapsedTime.startTime
}
if (fallbackStartTime) {
// On some browsers like iOS WebViews, performance.mark is not supported.
return Date.now() - fallbackStartTime
}
return undefined
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment