diff --git a/.eslintrc.json b/.eslintrc.json index 68b11e8f33..5802418918 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -193,7 +193,6 @@ "realm_logo": false, "realm_night_logo": false, "recent_topics": false, - "reload": false, "reminder": false, "resize": false, "scroll_bar": false, diff --git a/frontend_tests/node_tests/channel.js b/frontend_tests/node_tests/channel.js index 247add709d..0131d6999a 100644 --- a/frontend_tests/node_tests/channel.js +++ b/frontend_tests/node_tests/channel.js @@ -3,17 +3,22 @@ const {strict: assert} = require("assert"); const _ = require("lodash"); +const rewiremock = require("rewiremock/node"); const {set_global, zrequire} = require("../zjsunit/namespace"); const {run_test} = require("../zjsunit/test"); -const reload = set_global("reload", {}); +const reload = {__esModule: true}; + +rewiremock("../../static/js/reload").with(reload); set_global("setTimeout", (f, delay) => { assert.equal(delay, 0); f(); }); +rewiremock.enable(); + const channel = zrequire("channel"); const default_stub_xhr = "default-stub-xhr"; @@ -310,3 +315,4 @@ run_test("xhr_error_message", () => { msg = "some message"; assert.equal(channel.xhr_error_message(msg, xhr), "some message: file not found"); }); +rewiremock.disable(); diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index d869cdfa86..9fa0c3af2f 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -41,7 +41,8 @@ const notifications = set_global("notifications", {}); const reactions = set_global("reactions", {}); const realm_icon = set_global("realm_icon", {}); const realm_logo = set_global("realm_logo", {}); -const reload = set_global("reload", {}); +const reload = {__esModule: true}; +rewiremock("../../static/js/reload").with(reload); const scroll_bar = set_global("scroll_bar", {}); const settings_account = set_global("settings_account", {}); const settings_bots = set_global("settings_bots", {}); diff --git a/frontend_tests/node_tests/transmit.js b/frontend_tests/node_tests/transmit.js index fd86927de1..e6e41eaf79 100644 --- a/frontend_tests/node_tests/transmit.js +++ b/frontend_tests/node_tests/transmit.js @@ -12,7 +12,8 @@ const noop = () => {}; const page_params = set_global("page_params", {}); const channel = {__esModule: true}; rewiremock("../../static/js/channel").with(channel); -const reload = set_global("reload", {}); +const reload = {__esModule: true}; +rewiremock("../../static/js/reload").with(reload); const reload_state = {__esModule: true}; rewiremock("../../static/js/reload_state").with(reload_state); const sent_messages = set_global("sent_messages", { diff --git a/frontend_tests/node_tests/ui_init.js b/frontend_tests/node_tests/ui_init.js index 755b83d841..a69221a43b 100644 --- a/frontend_tests/node_tests/ui_init.js +++ b/frontend_tests/node_tests/ui_init.js @@ -83,7 +83,7 @@ const message_viewport = { rewiremock("../../static/js/message_viewport").with(message_viewport); set_global("panels", {initialize() {}}); set_global("popovers", {initialize() {}}); -set_global("reload", {initialize() {}}); +rewiremock("../../static/js/reload").with({initialize() {}}); set_global("scroll_bar", {initialize() {}}); const server_events = set_global("server_events", {initialize() {}}); set_global("settings_sections", {initialize() {}}); diff --git a/static/js/channel.js b/static/js/channel.js index 616998c0c5..52964e7c76 100644 --- a/static/js/channel.js +++ b/static/js/channel.js @@ -1,3 +1,4 @@ +import * as reload from "./reload"; import * as reload_state from "./reload_state"; const pending_requests = []; diff --git a/static/js/global.d.ts b/static/js/global.d.ts index f7994f54d3..aca5865b3d 100644 --- a/static/js/global.d.ts +++ b/static/js/global.d.ts @@ -66,7 +66,6 @@ declare let reactions: any; declare let realm_icon: any; declare let realm_logo: any; declare let recent_topics: any; -declare let reload: any; declare let reminder: any; declare let resize: any; declare let scroll_bar: any; diff --git a/static/js/reload.js b/static/js/reload.js index 03ddf072dc..6279199ab9 100644 --- a/static/js/reload.js +++ b/static/js/reload.js @@ -1,9 +1,7 @@ -"use strict"; - -const {localstorage} = require("./localstorage"); -const narrow_state = require("./narrow_state"); -const reload_state = require("./reload_state"); -const util = require("./util"); +import {localstorage} from "./localstorage"; +import * as narrow_state from "./narrow_state"; +import * as reload_state from "./reload_state"; +import * as util from "./util"; // Read https://zulip.readthedocs.io/en/latest/subsystems/hashchange-system.html function preserve_state(send_after_reload, save_pointer, save_narrow, save_compose) { @@ -95,7 +93,7 @@ function preserve_state(send_after_reload, save_pointer, save_narrow, save_compo // Check if we're doing a compose-preserving reload. This must be // done before the first call to get_events -exports.initialize = function () { +export function initialize() { const location = window.location.toString(); const hash_fragment = location.slice(location.indexOf("#") + 1); @@ -173,7 +171,7 @@ exports.initialize = function () { activity.set_new_user_input(false); hashchange.changehash(vars.oldhash); -}; +} function do_reload_app(send_after_reload, save_pointer, save_narrow, save_compose, message) { if (reload_state.is_in_progress()) { @@ -223,7 +221,7 @@ function do_reload_app(send_after_reload, save_pointer, save_narrow, save_compos window.location.reload(true); } -exports.initiate = function (options) { +export function initiate(options) { options = { immediate: false, save_pointer: true, @@ -324,7 +322,7 @@ exports.initiate = function (options) { idle_control = $(document).idle({idle: basic_idle_timeout, onIdle: reload_from_idle}); $(document).on("compose_started.zulip", compose_started_handler); } -}; +} window.addEventListener("beforeunload", () => { // When navigating away from the page do not try to reload. @@ -335,5 +333,3 @@ window.addEventListener("beforeunload", () => { blueslip.log("Setting reload_in_progress in beforeunload handler"); reload_state.set_state_to_in_progress(); }); - -window.reload = exports; diff --git a/static/js/server_events.js b/static/js/server_events.js index da8146d0e1..48c6b761e3 100644 --- a/static/js/server_events.js +++ b/static/js/server_events.js @@ -4,6 +4,7 @@ const _ = require("lodash"); const channel = require("./channel"); const echo = require("./echo"); +const reload = require("./reload"); const reload_state = require("./reload_state"); const server_events_dispatch = require("./server_events_dispatch"); // Docs: https://zulip.readthedocs.io/en/latest/subsystems/events-system.html diff --git a/static/js/server_events_dispatch.js b/static/js/server_events_dispatch.js index cc4555a9c9..64ed8d5331 100644 --- a/static/js/server_events_dispatch.js +++ b/static/js/server_events_dispatch.js @@ -5,6 +5,7 @@ import * as muting_ui from "./muting_ui"; import * as narrow_state from "./narrow_state"; import * as peer_data from "./peer_data"; import * as people from "./people"; +import * as reload from "./reload"; import * as settings_config from "./settings_config"; import * as submessage from "./submessage"; import * as user_groups from "./user_groups"; diff --git a/static/js/transmit.js b/static/js/transmit.js index fefe91a26c..83f540b116 100644 --- a/static/js/transmit.js +++ b/static/js/transmit.js @@ -2,6 +2,7 @@ const channel = require("./channel"); const people = require("./people"); +const reload = require("./reload"); const reload_state = require("./reload_state"); exports.send_message = function (request, on_success, error) { diff --git a/static/js/ui_init.js b/static/js/ui_init.js index 920c875c96..0ea3564c75 100644 --- a/static/js/ui_init.js +++ b/static/js/ui_init.js @@ -21,6 +21,7 @@ const muting = require("./muting"); const people = require("./people"); const pm_conversations = require("./pm_conversations"); const pm_list = require("./pm_list"); +const reload = require("./reload"); const rows = require("./rows"); const spoilers = require("./spoilers"); const topic_list = require("./topic_list"); diff --git a/static/js/unread_ops.js b/static/js/unread_ops.js index 21e5112592..34f8135daf 100644 --- a/static/js/unread_ops.js +++ b/static/js/unread_ops.js @@ -2,6 +2,7 @@ const channel = require("./channel"); const message_viewport = require("./message_viewport"); +const reload = require("./reload"); const unread_ui = require("./unread_ui"); exports.mark_all_as_read = function () {