js: Break cyclic dependency between `reload,js` and `server_events.js`.

Removes direct dependency cycle between `reload.js` and `server_events.js`
by calling `reload.add_reload_hook(cleanup_event_queue)` from `server_events`
initialize function which registers a hook to run on reloading.

Created a function `call_hooks` which loops over all the registered hooks
and executes them on performing a reload.
This commit is contained in:
Lalit 2023-04-09 08:04:13 +05:30 committed by Tim Abbott
parent 915b6cd9e7
commit cfa92aa60b
2 changed files with 16 additions and 3 deletions

View File

@ -14,11 +14,23 @@ import * as message_lists from "./message_lists";
import * as narrow_state from "./narrow_state";
import {page_params} from "./page_params";
import * as reload_state from "./reload_state";
import * as server_events from "./server_events";
import * as ui_report from "./ui_report";
import * as util from "./util";
// Read https://zulip.readthedocs.io/en/latest/subsystems/hashchange-system.html
const reload_hooks = [];
export function add_reload_hook(hook) {
reload_hooks.push(hook);
}
function call_reload_hooks() {
for (const hook of reload_hooks) {
hook();
}
}
function preserve_state(send_after_reload, save_pointer, save_narrow, save_compose) {
if (!localstorage.supported()) {
// If local storage is not supported by the browser, we can't
@ -255,7 +267,7 @@ function do_reload_app(send_after_reload, save_pointer, save_narrow, save_compos
util.call_function_periodically(retry_reload, 30000);
try {
server_events.cleanup_event_queue();
call_reload_hooks();
} catch (error) {
blueslip.error("Failed to clean up before reloading", undefined, error);
}

View File

@ -269,6 +269,7 @@ export function home_view_loaded() {
}
export function initialize() {
reload.add_reload_hook(cleanup_event_queue);
watchdog.on_unsuspend(() => {
// Immediately poll for new events on unsuspend
blueslip.log("Restarting get_events due to unsuspend");
@ -278,7 +279,7 @@ export function initialize() {
get_events();
}
export function cleanup_event_queue() {
function cleanup_event_queue() {
// Submit a request to the server to clean up our event queue
if (page_params.event_queue_expired === true || page_params.no_event_queue === true) {
return;