(win, startEvent, endEvent)
| 72 | * @return {!Promise<ResolverReturnDef>} |
| 73 | */ |
| 74 | export 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. |
no test coverage detected