-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUiPeriodicUpdater.js
63 lines (55 loc) · 1.8 KB
/
UiPeriodicUpdater.js
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
export class UiPeriodicUpdater {
constructor(cb, intervalMs) {
this.bgTask = null;
this.callback = null;
this.install_visibility_callback();
}
installCallback(cb, intervalMs) {
this.callback = cb;
this.intervalMs = intervalMs;
this.reinstallTicker();
}
app_became_hidden() {
if (this.bgTask != null) {
clearInterval(this.bgTask);
}
}
reinstallTicker() {
if (this.bgTask == null && this.callback != null) {
this.bgTask = setInterval(this.callback, this.intervalMs);
}
}
app_became_visible() {
this.callback();
this.reinstallTicker();
}
static warn_if_visibility_not_supported(visChangeAction) {
if (this.visibility_checked !== undefined) return;
this.visibility_checked = true;
if (visChangeAction === undefined) {
console.log("Visibility changes not supported: UI elements won't auto-refresh");
}
}
install_visibility_callback() {
if (this.vis_cb_installed !== undefined) return;
this.vis_cb_installed = true;
var hidden, visChangeAction;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visChangeAction = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visChangeAction = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visChangeAction = "webkitvisibilitychange";
}
UiPeriodicUpdater.warn_if_visibility_not_supported(visChangeAction);
if (visChangeAction !== undefined) {
document.addEventListener(visChangeAction, () => {
const app_hidden = document[hidden];
app_hidden? this.app_became_hidden() : this.app_became_visible();
});
}
}
};