mirror of
https://github.com/zulip/zulip.git
synced 2025-11-11 17:36:27 +00:00
util: Add call_function_periodically helper function.
This commit adds call_function_periodically helper function which will be used to call functions periodically using setTimeout. Currently, this new function is used to send presence requests and trying reload.
This commit is contained in:
@@ -15,6 +15,7 @@ import * as popovers from "./popovers";
|
|||||||
import * as presence from "./presence";
|
import * as presence from "./presence";
|
||||||
import * as ui_util from "./ui_util";
|
import * as ui_util from "./ui_util";
|
||||||
import {UserSearch} from "./user_search";
|
import {UserSearch} from "./user_search";
|
||||||
|
import * as util from "./util";
|
||||||
import * as watchdog from "./watchdog";
|
import * as watchdog from "./watchdog";
|
||||||
|
|
||||||
export let user_cursor;
|
export let user_cursor;
|
||||||
@@ -242,32 +243,10 @@ export function initialize() {
|
|||||||
buddy_list.start_scroll_handler();
|
buddy_list.start_scroll_handler();
|
||||||
|
|
||||||
function get_full_presence_list_update() {
|
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);
|
send_presence_to_server(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Schedule our first full presence update.
|
util.call_function_periodically(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);
|
||||||
setTimeout(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);
|
|
||||||
|
|
||||||
// Let the server know we're here, but pass "false" for
|
// Let the server know we're here, but pass "false" for
|
||||||
// want_redraw, since we just got all this info in page_params.
|
// want_redraw, since we just got all this info in page_params.
|
||||||
|
|||||||
@@ -248,17 +248,10 @@ function do_reload_app(send_after_reload, save_pointer, save_narrow, save_compos
|
|||||||
});
|
});
|
||||||
|
|
||||||
function retry_reload() {
|
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");
|
blueslip.log("Retrying page reload due to 30s timer");
|
||||||
window.location.reload(true);
|
window.location.reload(true);
|
||||||
}
|
}
|
||||||
setTimeout(retry_reload, 30000);
|
util.call_function_periodically(retry_reload, 30000);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
server_events.cleanup_event_queue();
|
server_events.cleanup_event_queue();
|
||||||
|
|||||||
@@ -360,3 +360,24 @@ export function get_time_from_date_muted(date_muted) {
|
|||||||
}
|
}
|
||||||
return date_muted * 1000;
|
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();
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user