Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/wmr/src/plugins/wmr/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const strip = url => url.replace(/[?&]t=\d+/g, '');
const addTimestamp = (url, time) => url + (/\?/.test(url) ? '&' : '?') + 't=' + time;

const resolve = url => new URL(url, location.origin).href;
let ws, connectTimer, connectDelay = 0;
let ws, visibilityCallback;

/**
* @param {boolean} [needsReload] Force page to reload once it's connected
Expand All @@ -23,6 +23,10 @@ function connect(needsReload) {
ws.addEventListener('open', () => {
connectDelay = 0;
log(`Connected to server.`);
if (visibilityCallback) {
window.removeEventListener('visibilitychange', visibilityCallback);
visibilityCallback = undefined;
}
if (needsReload) {
window.location.reload();
} else {
Expand All @@ -37,11 +41,13 @@ function connect(needsReload) {
}

function reconnect() {
connectDelay = Math.min(connectDelay * 2, 30000) || 500;
connectTimer = setTimeout(() => {
if (ws) ws.close();
connect(false);
}, connectDelay);
if (!visibilityCallback) {
visibilityCallback = () => {
if (document.visibilityState === 'visible')
connect(false);
};
}
window.addEventListener('visibilitychange', visibilityCallback)
}

connect();
Expand Down