Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

util: Add call_function_periodically helper function.

Sahil Batraf772eeddd682
diff --git a/static/js/activity.js b/static/js/activity.js
index ee98b5ad7b5..065287fc516 100644
--- a/static/js/activity.js
+++ b/static/js/activity.js
@@ -15,6 +15,7 @@ import * as popovers from "./popovers";
import * as presence from "./presence";
import * as ui_util from "./ui_util";
import {UserSearch} from "./user_search";
+import * as util from "./util";
import * as watchdog from "./watchdog";
export let user_cursor;
@@ -242,32 +243,10 @@ export function initialize() {
buddy_list.start_scroll_handler();
function get_full_presence_list_update() {
- // Schedule the next presence update request to the server.
- // This implementation aims to simulate setInterval; in
- // particular, we schedule the next request immediately,
- // rather than waiting for the current request to
- // finish/succeed.
- //
- // We previously used setInterval for this purpose, but
- // empirically observed that after unsuspend, Chrome can end
- // up trying to "catch up" by doing dozens of these requests
- // at once, wasting resources as well as hitting rate limits
- // on the server. We have not been able to reproduce this
- // reliably enough to be certain whether the setInterval
- // requests are those that would have happened while the
- // laptop was suspended or during a window after unsuspend
- // before the user focuses the browser tab.
- //
- // But using setTimeout this instead ensures that we're only
- // scheduling a next request if the browser will actually be
- // calling send_presence_to_server.
- setTimeout(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);
-
send_presence_to_server(true);
}
- // Schedule our first full presence update.
- setTimeout(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);
+ util.call_function_periodically(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);
// Let the server know we're here, but pass "false" for
// want_redraw, since we just got all this info in page_params.
diff --git a/static/js/reload.js b/static/js/reload.js
index 862f533f9ef..9980cf2737b 100644
--- a/static/js/reload.js
+++ b/static/js/reload.js
@@ -248,17 +248,10 @@ function do_reload_app(send_after_reload, save_pointer, save_narrow, save_compos
});
function retry_reload() {
- // Schedule the next attempt to reload. This implementation
- // aims to simulate setInterval, which was used previously.
- //
- // See comment for get_full_presence_list_update in
- // activity.js for more details.
- setTimeout(retry_reload, 30000);
-
blueslip.log("Retrying page reload due to 30s timer");
window.location.reload(true);
}
- setTimeout(retry_reload, 30000);
+ util.call_function_periodically(retry_reload, 30000);
try {
server_events.cleanup_event_queue();
diff --git a/static/js/util.js b/static/js/util.js
index 64fafcc9d24..f20aad4bc5e 100644
--- a/static/js/util.js
+++ b/static/js/util.js
@@ -360,3 +360,24 @@ export function get_time_from_date_muted(date_muted) {
}
return date_muted * 1000;
}
+
+export function call_function_periodically(callback, delay) {
+ // We previously used setInterval for this purpose, but
+ // empirically observed that after unsuspend, Chrome can end
+ // up trying to "catch up" by doing dozens of these requests
+ // at once, wasting resources as well as hitting rate limits
+ // on the server. We have not been able to reproduce this
+ // reliably enough to be certain whether the setInterval
+ // requests are those that would have happened while the
+ // laptop was suspended or during a window after unsuspend
+ // before the user focuses the browser tab.
+
+ // But using setTimeout this instead ensures that we're only
+ // scheduling a next call if the browser will actually be
+ // calling "callback".
+ setTimeout(() => {
+ call_function_periodically(callback, delay);
+ }, delay);
+
+ callback();
+}