-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from alallier/waitServer
Changed timeout_millis to delay, allowing reload to wait until the server is up
- Loading branch information
Showing
4 changed files
with
54 additions
and
12 deletions.
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
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
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 |
---|---|---|
@@ -1,10 +1,44 @@ | ||
;(function refresh () { | ||
var RLD_TIMEOUT = 300; | ||
var sock = new SockJS(window.location.origin + '/sockreload'); | ||
var waitForServer = false; | ||
var sock; | ||
var checkDelay = 1; | ||
var checkDelayCounter = 1; | ||
|
||
sock.onclose = function() { | ||
setTimeout(function() { | ||
var newConn = function() { | ||
//try and connect a new socket to the server. | ||
sock = new SockJS(window.location.origin + '/sockreload'); | ||
|
||
//if the server is up then, the sockert will reach this event handler and then call refreshPage | ||
sock.onopen = function() { | ||
window.location.reload(); | ||
},RLD_TIMEOUT); | ||
}; | ||
|
||
//else the server is down try and connect again with another call to newConn | ||
|
||
//this will exponentially increase the wait time to slow down the calls to checkServerUp to prevent this script from spamming requests if the server doesn’t come back up quickly. | ||
//this is useful in the case when the user brings down the server but forgets to close their dev tab or a server has a long restart time. | ||
checkDelay = (Math.pow(checkDelayCounter,6)) / 10000000000; | ||
checkDelayCounter++; | ||
|
||
setTimeout(function () { | ||
newConn(); | ||
}, checkDelay); | ||
}; | ||
|
||
sock.onclose = function() { | ||
//check for the server only if the user entered in true for a delay | ||
if (waitForServer) { | ||
sock = null; | ||
|
||
newConn(); | ||
} | ||
//else use the default delay or user specified delay | ||
else { | ||
setTimeout(function() { | ||
window.location.reload(); | ||
},RLD_TIMEOUT); | ||
} | ||
}; | ||
})(); | ||
})(); |
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