MCPcopy
hub / github.com/ampproject/amphtml / getTimingDataAsync

Function getTimingDataAsync

src/service/variable-source.js:74–106  ·  view source on GitHub ↗
(win, startEvent, endEvent)

Source from the content-addressed store, hash-verified

72 * @return {!Promise<ResolverReturnDef>}
73 */
74export function getTimingDataAsync(win, startEvent, endEvent) {
75 // Fallback to load event if we don't know what to wait for
76 const startWaitForEvent =
77 NAV_TIMING_WAITFOR_EVENTS[startEvent] || WAITFOR_EVENTS_ENUM.LOAD;
78 const endWaitForEvent = endEvent
79 ? NAV_TIMING_WAITFOR_EVENTS[endEvent] || WAITFOR_EVENTS_ENUM.LOAD
80 : startWaitForEvent;
81
82 const waitForEvent = Math.max(startWaitForEvent, endWaitForEvent);
83
84 // set wait for onload to be default
85 let readyPromise;
86 if (waitForEvent === WAITFOR_EVENTS_ENUM.VIEWER_FIRST_VISIBLE) {
87 readyPromise = Promise.resolve();
88 } else if (waitForEvent === WAITFOR_EVENTS_ENUM.DOCUMENT_COMPLETE) {
89 readyPromise = whenDocumentComplete(win.document);
90 } else if (waitForEvent === WAITFOR_EVENTS_ENUM.LOAD) {
91 readyPromise = loadPromise(win);
92 } else if (waitForEvent === WAITFOR_EVENTS_ENUM.LOAD_END) {
93 // performance.timing.loadEventEnd returns 0 before the load event handler
94 // has terminated, that's when the load event is completed.
95 // To wait for the event handler to terminate, wait 1ms and defer to the
96 // event loop.
97 const timer = Services.timerFor(win);
98 readyPromise = loadPromise(win).then(() => timer.promise(1));
99 }
100
101 devAssert(readyPromise, 'waitForEvent not supported ' + waitForEvent);
102
103 return readyPromise.then(() => {
104 return getTimingDataSync(win, startEvent, endEvent);
105 });
106}
107
108/**
109 * Returns navigation timing information based on the start and end events.

Callers 4

setTimingResolver_Method · 0.90
initializeMethod · 0.90
initializeMethod · 0.90

Calls 7

whenDocumentCompleteFunction · 0.90
loadPromiseFunction · 0.90
devAssertFunction · 0.90
getTimingDataSyncFunction · 0.85
resolveMethod · 0.80
promiseMethod · 0.80
thenMethod · 0.45

Tested by

no test coverage detected