Repository navigation
Expand file tree
/
Copy pathdocumentActivity.ts
More file actions
69 lines (62 loc) · 2.54 KB
/
Copy pathdocumentActivity.ts
File metadata and controls
69 lines (62 loc) · 2.54 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
export interface DocumentActivity {
readonly visible: boolean;
readonly focused: boolean;
}
const ALWAYS_ACTIVE: DocumentActivity = { visible: true, focused: true };
const listeners = new Set<() => void>();
let snapshot: DocumentActivity = ALWAYS_ACTIVE;
function readActivity(): DocumentActivity {
if (typeof document === "undefined") return ALWAYS_ACTIVE;
const visible = document.visibilityState === "visible";
const focused = document.hasFocus();
return snapshot.visible === visible && snapshot.focused === focused
? snapshot
: { visible, focused };
}
function handleActivityEvent() {
const next = readActivity();
if (next === snapshot) return;
snapshot = next;
for (const listener of listeners) listener();
}
/** Whether the user is looking at this window: the tab is visible and the window has focus. */
export function getDocumentActivity(): DocumentActivity {
// Without listeners the cached snapshot is not kept current.
if (listeners.size === 0) snapshot = readActivity();
return snapshot;
}
/** One visibilitychange/focus/blur listener set shared by every subscriber. */
export function subscribeDocumentActivity(listener: () => void): () => void {
if (typeof document === "undefined") return () => {};
if (listeners.size === 0) {
snapshot = readActivity();
document.addEventListener("visibilitychange", handleActivityEvent);
window.addEventListener("focus", handleActivityEvent);
window.addEventListener("blur", handleActivityEvent);
}
listeners.add(listener);
return () => {
if (!listeners.delete(listener) || listeners.size > 0) return;
document.removeEventListener("visibilitychange", handleActivityEvent);
window.removeEventListener("focus", handleActivityEvent);
window.removeEventListener("blur", handleActivityEvent);
};
}
/**
* Fires on every window focus and every switch to a visible tab. Unlike
* `subscribeDocumentActivity` it does not wait for the cached activity to flip:
* focus coming back from an iframe, or an alt-tab whose blur was missed, still
* counts as a return.
*/
export function subscribeDocumentReturn(listener: () => void): () => void {
if (typeof document === "undefined") return () => {};
const handleVisibilityChange = () => {
if (document.visibilityState === "visible") listener();
};
window.addEventListener("focus", listener);
document.addEventListener("visibilitychange", handleVisibilityChange);
return () => {
window.removeEventListener("focus", listener);
document.removeEventListener("visibilitychange", handleVisibilityChange);
};
}