Skip to content

Commit

Permalink
♻️ Simplify trackLoadingTime (#1336)
Browse files Browse the repository at this point in the history
* ♻️ Simplify trackLoadingTime

* 👌 namings
  • Loading branch information
amortemousque authored Feb 15, 2022
1 parent 48f5747 commit f4a4758
Showing 1 changed file with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ export function trackViewMetrics(
scheduleViewUpdate()
})

const { setActivityLoadingTime, setLoadEvent } = trackLoadingTime(loadingType, (newLoadingTime) => {
viewMetrics.loadingTime = newLoadingTime
scheduleViewUpdate()
})

const { stop: stopActivityLoadingTimeTracking } = trackActivityLoadingTime(
const { stop: stopLoadingTimeTracking, setLoadEvent } = trackLoadingTime(
lifeCycle,
domMutationObservable,
setActivityLoadingTime,
viewStart
loadingType,
viewStart,
(newLoadingTime) => {
viewMetrics.loadingTime = newLoadingTime
scheduleViewUpdate()
}
)

let stopCLSTracking: () => void
Expand All @@ -60,15 +59,21 @@ export function trackViewMetrics(
return {
stop: () => {
stopEventCountsTracking()
stopActivityLoadingTimeTracking()
stopLoadingTimeTracking()
stopCLSTracking()
},
setLoadEvent,
viewMetrics,
}
}

function trackLoadingTime(loadType: ViewLoadingType, callback: (loadingTime: Duration) => void) {
function trackLoadingTime(
lifeCycle: LifeCycle,
domMutationObservable: Observable<void>,
loadType: ViewLoadingType,
viewStart: ClocksState,
callback: (loadingTime: Duration) => void
) {
let isWaitingForLoadEvent = loadType === ViewLoadingType.INITIAL_LOAD
let isWaitingForActivityLoadingTime = true
const loadingTimeCandidates: Duration[] = []
Expand All @@ -79,41 +84,28 @@ function trackLoadingTime(loadType: ViewLoadingType, callback: (loadingTime: Dur
}
}

const { stop } = waitIdlePage(lifeCycle, domMutationObservable, (event) => {
if (isWaitingForActivityLoadingTime) {
isWaitingForActivityLoadingTime = false
if (event.hadActivity) {
loadingTimeCandidates.push(elapsed(viewStart.timeStamp, event.end))
}
invokeCallbackIfAllCandidatesAreReceived()
}
})

return {
stop,
setLoadEvent: (loadEvent: Duration) => {
if (isWaitingForLoadEvent) {
isWaitingForLoadEvent = false
loadingTimeCandidates.push(loadEvent)
invokeCallbackIfAllCandidatesAreReceived()
}
},
setActivityLoadingTime: (activityLoadingTime: Duration | undefined) => {
if (isWaitingForActivityLoadingTime) {
isWaitingForActivityLoadingTime = false
if (activityLoadingTime !== undefined) {
loadingTimeCandidates.push(activityLoadingTime)
}
invokeCallbackIfAllCandidatesAreReceived()
}
},
}
}

function trackActivityLoadingTime(
lifeCycle: LifeCycle,
domMutationObservable: Observable<void>,
callback: (loadingTimeValue: Duration | undefined) => void,
viewStart: ClocksState
) {
return waitIdlePage(lifeCycle, domMutationObservable, (event) => {
if (event.hadActivity) {
callback(elapsed(viewStart.timeStamp, event.end))
} else {
callback(undefined)
}
})
}

/**
* Track the cumulative layout shifts (CLS).
* Layout shifts are grouped into session windows.
Expand Down

0 comments on commit f4a4758

Please sign in to comment.