-
Notifications
You must be signed in to change notification settings - Fork 16
/
disconnect-when-backgrounded.js
43 lines (33 loc) · 1.24 KB
/
disconnect-when-backgrounded.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
var disconnectTimer = null;
// 60 seconds by default
var disconnectTime = (Meteor.settings && Meteor.settings.public && Meteor.settings.public.disconnectTimeSec || 60) * 1000;
var disconnectVoids = (Meteor.settings && Meteor.settings.public && Meteor.settings.public.disconnectVoids || []);
Meteor.startup(disconnectIfHidden);
document.addEventListener('visibilitychange', disconnectIfHidden);
if (Meteor.isCordova) {
document.addEventListener('resume', function () { Meteor.reconnect(); });
document.addEventListener('pause', function () { createDisconnectTimeout(); });
}
function currentPageIsNotExempt() {
const pathName = Package["iron:router"] ? Router.current().route.getName() : window.location.pathname;
return !disconnectVoids.includes(pathName);
};
function disconnectIfHidden() {
removeDisconnectTimeout();
if (document.hidden && currentPageIsNotExempt()) {
createDisconnectTimeout();
} else {
Meteor.reconnect();
}
}
function createDisconnectTimeout() {
removeDisconnectTimeout();
disconnectTimer = setTimeout(function () {
Meteor.disconnect();
}, disconnectTime);
}
function removeDisconnectTimeout() {
if (disconnectTimer) {
clearTimeout(disconnectTimer);
}
}