Improved fullstack reload_upon_connect timeout (#2436)

Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
This commit is contained in:
luvetiandEvan Almloff authored and GitHub committed 2024-05-22 07:35:07 -05:00
1 parent 2523581cf4
commit d3f96ec323
1 file changed
+15 -4
+15 -4
View File
@@ -1,9 +1,12 @@
(function () {
const POLL_INTERVAL_MIN = 250;
const POLL_INTERVAL_MAX = 4000;
const POLL_INTERVAL_SCALE_FACTOR = 2;
var protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
var url = protocol + "//" + window.location.host + "/_dioxus/ws";
var poll_interval = 8080;
var reload_upon_connect = (event) => {
var reload_upon_connect = (event, poll_interval) => {
// Firefox will send a 1001 code when the connection is closed because the page is reloaded
// Only firefox will trigger the onclose event when the page is reloaded manually: https://stackoverflow.com/questions/10965720/should-websocket-onclose-be-triggered-by-user-navigation-or-refresh
// We should not reload the page in this case
@@ -13,7 +16,15 @@
window.setTimeout(() => {
var ws = new WebSocket(url);
ws.onopen = () => window.location.reload();
ws.onclose = reload_upon_connect;
ws.onclose = (event) => {
reload_upon_connect(
event,
Math.min(
POLL_INTERVAL_MAX,
poll_interval * POLL_INTERVAL_SCALE_FACTOR
)
);
};
}, poll_interval);
};
@@ -27,5 +38,5 @@
}
};
ws.onclose = reload_upon_connect;
ws.onclose = (event) => reload_upon_connect(event, POLL_INTERVAL_MIN);
})();