Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 528x 1567x 1567x 19x 1567x 1x 1x 1x 1x 1567x 11x 10x 1x 1567x 1567x 1567x 1567x 1567x 8x 8x 8x | // Mobile browsers freeze the page while the screen is off without firing
// offline/online events, and WebKit restores pages from the back/forward
// cache without firing visibilitychange, so detecting "the page resumed"
// needs both the visibility and the pagehide/pageshow signals
export const onPageResume = (
callback: (hiddenDurationMs: number) => void,
): (() => void) => {
// A page can load already hidden (e.g. opened in a background tab), and
// its first foregrounding is a resume like any other
let hiddenAt: number | undefined = document.hidden ? Date.now() : undefined;
const markHidden = (): void => {
hiddenAt ??= Date.now();
};
const resume = (): void => {
// Undefined means the resume was already handled by the other event type
Iif (hiddenAt === undefined) {
return;
}
const hiddenDurationMs = Date.now() - hiddenAt;
hiddenAt = undefined;
callback(hiddenDurationMs);
};
const handleVisibilityChange = (): void => {
if (document.hidden) {
markHidden();
} else {
resume();
}
};
const handlePageShow = (event: PageTransitionEvent): void => {
if (event.persisted) {
resume();
}
};
document.addEventListener("visibilitychange", handleVisibilityChange);
addEventListener("pagehide", markHidden);
addEventListener("pageshow", handlePageShow);
return () => {
document.removeEventListener("visibilitychange", handleVisibilityChange);
removeEventListener("pagehide", markHidden);
removeEventListener("pageshow", handlePageShow);
};
};
|