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.

channel: Sever dependency on reload.

Anders Kaseorg6540285a9caf
diff --git a/frontend_tests/node_tests/channel.js b/frontend_tests/node_tests/channel.js
index 9b6e641cb1f..49dbe40ac9b 100644
--- a/frontend_tests/node_tests/channel.js
+++ b/frontend_tests/node_tests/channel.js
@@ -4,16 +4,15 @@ const {strict: assert} = require("assert");
const _ = require("lodash");
-const {mock_module, set_global, zrequire} = require("../zjsunit/namespace");
+const {set_global, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
-const reload = mock_module("reload");
-
set_global("setTimeout", (f, delay) => {
assert.equal(delay, 0);
f();
});
+const reload_state = zrequire("reload_state");
const channel = zrequire("channel");
const default_stub_xhr = "default-stub-xhr";
@@ -220,19 +219,13 @@ run_test("reload_on_403_error", () => {
},
check_ajax_options(options) {
- let reload_initiated;
- reload.initiate = (options) => {
- reload_initiated = true;
- assert.deepEqual(options, {
- immediate: true,
- save_pointer: true,
- save_narrow: true,
- save_compose: true,
- });
- };
+ let handler_called = false;
+ reload_state.set_csrf_failed_handler(() => {
+ handler_called = true;
+ });
options.simulate_error();
- assert(reload_initiated);
+ assert(handler_called);
},
});
});
diff --git a/static/js/channel.js b/static/js/channel.js
index 52d62deb2ed..5f0c4c1f669 100644
--- a/static/js/channel.js
+++ b/static/js/channel.js
@@ -1,4 +1,3 @@
-import * as reload from "./reload";
import * as reload_state from "./reload_state";
const pending_requests = [];
@@ -52,13 +51,11 @@ function call(args, idempotent) {
if (xhr.status === 403) {
try {
- if (JSON.parse(xhr.responseText).code === "CSRF_FAILED") {
- reload.initiate({
- immediate: true,
- save_pointer: true,
- save_narrow: true,
- save_compose: true,
- });
+ if (
+ JSON.parse(xhr.responseText).code === "CSRF_FAILED" &&
+ reload_state.csrf_failed_handler !== undefined
+ ) {
+ reload_state.csrf_failed_handler();
}
} catch (error) {
blueslip.error(
diff --git a/static/js/reload.js b/static/js/reload.js
index 548d4643cef..b16d8fea8b4 100644
--- a/static/js/reload.js
+++ b/static/js/reload.js
@@ -341,3 +341,12 @@ window.addEventListener("beforeunload", () => {
blueslip.log("Setting reload_in_progress in beforeunload handler");
reload_state.set_state_to_in_progress();
});
+
+reload_state.set_csrf_failed_handler(() => {
+ initiate({
+ immediate: true,
+ save_pointer: true,
+ save_narrow: true,
+ save_compose: true,
+ });
+});
diff --git a/static/js/reload_state.js b/static/js/reload_state.js
index cfff3fc7d79..6084fdac5c2 100644
--- a/static/js/reload_state.js
+++ b/static/js/reload_state.js
@@ -9,6 +9,7 @@
let reload_in_progress = false;
let reload_pending = false;
+export let csrf_failed_handler;
export function is_pending() {
return reload_pending;
@@ -28,3 +29,7 @@ export function set_state_to_pending() {
export function set_state_to_in_progress() {
reload_in_progress = true;
}
+
+export function set_csrf_failed_handler(handler) {
+ csrf_failed_handler = handler;
+}