-
-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] Automatically reconnect to HMR with exponential backoff #785
Open
developit
wants to merge
3
commits into
main
Choose a base branch
from
hmr-reconnection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wmr": patch | ||
--- | ||
|
||
when the hmr client disconnects attempt to reconnect with exponential backoff and a visibilityChange event-listener |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,11 @@ function log(...args) { | |
|
||
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; | ||
|
||
let ws, | ||
connectTimer, | ||
connectDelay = 0; | ||
|
||
/** | ||
* @param {boolean} [needsReload] Force page to reload once it's connected | ||
|
@@ -19,10 +21,15 @@ function connect(needsReload) { | |
ws.send(JSON.stringify(msg)); | ||
} | ||
|
||
clearTimeout(connectTimer); | ||
ws.addEventListener('open', () => { | ||
log(`Connected to server.`); | ||
connectDelay = 0; | ||
clearTimeout(connectTimer); | ||
connectTimer = undefined; | ||
removeEventListener('visibilitychange', connectIfVisible); | ||
if (needsReload) { | ||
window.location.reload(); | ||
location.reload(); | ||
} else { | ||
queue.forEach(sendSocketMessage); | ||
queue = []; | ||
|
@@ -31,6 +38,22 @@ function connect(needsReload) { | |
|
||
ws.addEventListener('message', handleMessage); | ||
ws.addEventListener('error', handleError); | ||
ws.addEventListener('close', reconnect); | ||
} | ||
|
||
function reconnect() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently reconnect only gets called once, should this be called again if establishing connection in |
||
connectDelay = Math.min(connectDelay * 2, 30000) || 500; | ||
connectTimer = setTimeout(() => { | ||
if (ws) ws.close(); | ||
connect(false); | ||
}, connectDelay); | ||
addEventListener('visibilitychange', connectIfVisible); | ||
} | ||
|
||
function connectIfVisible() { | ||
if (document.visibilityState === 'visible') { | ||
connect(false); | ||
} | ||
} | ||
|
||
connect(); | ||
|
@@ -44,7 +67,7 @@ function handleMessage(e) { | |
const data = JSON.parse(e.data); | ||
switch (data.type) { | ||
case 'reload': | ||
window.location.reload(); | ||
location.reload(); | ||
break; | ||
case 'update': | ||
if (errorOverlay) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also listen for something like focussing (or visibilitychange) the browser again. This removes the need for exponential backoff, ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's a great idea. Thtat's what I'm using to run my code for waking up ios wrapper app
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the exponential dropoff with 30s limit will not solve the case for a sleep longer than 30s, but visibilitychange certainly would.
Thanks guys!