js: Convert static/js/message_fetch.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-27 16:11:16 -08:00
committed by Tim Abbott
parent 7a45ed46af
commit 21c6a3d87a
9 changed files with 48 additions and 51 deletions

View File

@@ -152,7 +152,6 @@
"localStorage": false, "localStorage": false,
"location": false, "location": false,
"message_events": false, "message_events": false,
"message_fetch": false,
"message_list": false, "message_list": false,
"narrow": false, "narrow": false,
"padded_widget": false, "padded_widget": false,

View File

@@ -20,7 +20,8 @@ set_global("current_msg_list", {});
const hashchange = {__esModule: true}; const hashchange = {__esModule: true};
rewiremock("../../static/js/hashchange").with(hashchange); rewiremock("../../static/js/hashchange").with(hashchange);
set_global("home_msg_list", {}); set_global("home_msg_list", {});
const message_fetch = set_global("message_fetch", {}); const message_fetch = {__esModule: true};
rewiremock("../../static/js/message_fetch").with(message_fetch);
const message_list = set_global("message_list", { const message_list = set_global("message_list", {
set_narrowed(value) { set_narrowed(value) {
this.narrowed = value; this.narrowed = value;

View File

@@ -76,7 +76,7 @@ rewiremock("../../static/js/gear_menu").with({initialize() {}});
rewiremock("../../static/js/hashchange").with({initialize() {}}); rewiremock("../../static/js/hashchange").with({initialize() {}});
set_global("hotspots", {initialize() {}}); set_global("hotspots", {initialize() {}});
// Accesses home_msg_list, which is a lot of complexity to set up // Accesses home_msg_list, which is a lot of complexity to set up
set_global("message_fetch", {initialize() {}}); rewiremock("../../static/js/message_fetch").with({initialize() {}});
rewiremock("../../static/js/message_scroll").with({initialize() {}}); rewiremock("../../static/js/message_scroll").with({initialize() {}});
const message_viewport = { const message_viewport = {
__esModule: true, __esModule: true,

View File

@@ -31,7 +31,6 @@ import "../composebox_typeahead";
import "../hotkey"; import "../hotkey";
import "../notifications"; import "../notifications";
import "../message_events"; import "../message_events";
import "../message_fetch";
import "../server_events"; import "../server_events";
import "../zulip"; import "../zulip";
import "../presence"; import "../presence";

View File

@@ -25,7 +25,6 @@ declare let hotspots: any;
declare let i18n: any; declare let i18n: any;
declare let input_pill: any; declare let input_pill: any;
declare let message_events: any; declare let message_events: any;
declare let message_fetch: any;
declare let message_list: any; declare let message_list: any;
declare let narrow: any; declare let narrow: any;
declare let padded_widget: any; declare let padded_widget: any;

View File

@@ -1,15 +1,13 @@
"use strict"; import * as channel from "./channel";
import {Filter} from "./filter";
const channel = require("./channel"); import * as huddle_data from "./huddle_data";
const {Filter} = require("./filter"); import * as message_scroll from "./message_scroll";
const huddle_data = require("./huddle_data"); import * as message_store from "./message_store";
const message_scroll = require("./message_scroll"); import * as message_util from "./message_util";
const message_store = require("./message_store"); import * as people from "./people";
const message_util = require("./message_util"); import * as pm_list from "./pm_list";
const people = require("./people"); import * as stream_data from "./stream_data";
const pm_list = require("./pm_list"); import * as ui_report from "./ui_report";
const stream_data = require("./stream_data");
const ui_report = require("./ui_report");
const consts = { const consts = {
backfill_idle_time: 10 * 1000, backfill_idle_time: 10 * 1000,
@@ -128,7 +126,7 @@ function get_messages_success(data, opts) {
// The server occasionally returns no data during a // The server occasionally returns no data during a
// restart. Ignore those responses and try again // restart. Ignore those responses and try again
setTimeout(() => { setTimeout(() => {
exports.load_messages(opts); load_messages(opts);
}, 0); }, 0);
return; return;
} }
@@ -178,7 +176,7 @@ function handle_operators_supporting_id_based_api(data) {
return data; return data;
} }
exports.load_messages = function (opts) { export function load_messages(opts) {
if (typeof opts.anchor === "number") { if (typeof opts.anchor === "number") {
// Messages that have been locally echoed messages have // Messages that have been locally echoed messages have
// floating point temporary IDs, which is intended to be a. // floating point temporary IDs, which is intended to be a.
@@ -269,25 +267,25 @@ exports.load_messages = function (opts) {
// We might want to be more clever here // We might want to be more clever here
$("#connection-error").addClass("show"); $("#connection-error").addClass("show");
setTimeout(() => { setTimeout(() => {
exports.load_messages(opts); load_messages(opts);
}, consts.error_retry_time); }, consts.error_retry_time);
}, },
}); });
}; }
exports.load_messages_for_narrow = function (opts) { export function load_messages_for_narrow(opts) {
const msg_list = message_list.narrowed; const msg_list = message_list.narrowed;
exports.load_messages({ load_messages({
anchor: opts.anchor, anchor: opts.anchor,
num_before: consts.narrow_before, num_before: consts.narrow_before,
num_after: consts.narrow_after, num_after: consts.narrow_after,
msg_list, msg_list,
cont: opts.cont, cont: opts.cont,
}); });
}; }
exports.get_backfill_anchor = function (msg_list) { export function get_backfill_anchor(msg_list) {
if (msg_list === home_msg_list) { if (msg_list === home_msg_list) {
msg_list = message_list.all; msg_list = message_list.all;
} }
@@ -300,9 +298,9 @@ exports.get_backfill_anchor = function (msg_list) {
// msg_list is empty, which is an impossible // msg_list is empty, which is an impossible
// case, raise a fatal error. // case, raise a fatal error.
throw new Error("There are no message available to backfill."); throw new Error("There are no message available to backfill.");
}; }
exports.get_frontfill_anchor = function (msg_list) { export function get_frontfill_anchor(msg_list) {
if (msg_list === home_msg_list) { if (msg_list === home_msg_list) {
msg_list = message_list.all; msg_list = message_list.all;
} }
@@ -319,9 +317,9 @@ exports.get_frontfill_anchor = function (msg_list) {
// fetch more data, and if user is, then the available data is wrong // fetch more data, and if user is, then the available data is wrong
// and we raise a fatal error. // and we raise a fatal error.
throw new Error("There are no message available to frontfill."); throw new Error("There are no message available to frontfill.");
}; }
exports.maybe_load_older_messages = function (opts) { export function maybe_load_older_messages(opts) {
// This function gets called when you scroll to the top // This function gets called when you scroll to the top
// of your window, and you want to get messages older // of your window, and you want to get messages older
// than what the browsers originally fetched. // than what the browsers originally fetched.
@@ -332,17 +330,17 @@ exports.maybe_load_older_messages = function (opts) {
return; return;
} }
exports.do_backfill({ do_backfill({
msg_list, msg_list,
num_before: consts.backward_batch_size, num_before: consts.backward_batch_size,
}); });
}; }
exports.do_backfill = function (opts) { export function do_backfill(opts) {
const msg_list = opts.msg_list; const msg_list = opts.msg_list;
const anchor = exports.get_backfill_anchor(msg_list); const anchor = get_backfill_anchor(msg_list);
exports.load_messages({ load_messages({
anchor, anchor,
num_before: opts.num_before, num_before: opts.num_before,
num_after: 0, num_after: 0,
@@ -353,9 +351,9 @@ exports.do_backfill = function (opts) {
} }
}, },
}); });
}; }
exports.maybe_load_newer_messages = function (opts) { export function maybe_load_newer_messages(opts) {
// This function gets called when you scroll to the bottom // This function gets called when you scroll to the bottom
// of your window, and you want to get messages newer // of your window, and you want to get messages newer
// than what the browsers originally fetched. // than what the browsers originally fetched.
@@ -367,37 +365,37 @@ exports.maybe_load_newer_messages = function (opts) {
return; return;
} }
const anchor = exports.get_frontfill_anchor(msg_list); const anchor = get_frontfill_anchor(msg_list);
function load_more(data, args) { function load_more(data, args) {
if (args.fetch_again && args.msg_list === current_msg_list) { if (args.fetch_again && args.msg_list === current_msg_list) {
exports.maybe_load_newer_messages({msg_list: current_msg_list}); maybe_load_newer_messages({msg_list: current_msg_list});
} }
} }
exports.load_messages({ load_messages({
anchor, anchor,
num_before: 0, num_before: 0,
num_after: consts.forward_batch_size, num_after: consts.forward_batch_size,
msg_list, msg_list,
cont: load_more, cont: load_more,
}); });
}; }
exports.start_backfilling_messages = function () { export function start_backfilling_messages() {
// backfill more messages after the user is idle // backfill more messages after the user is idle
$(document).idle({ $(document).idle({
idle: consts.backfill_idle_time, idle: consts.backfill_idle_time,
onIdle() { onIdle() {
exports.do_backfill({ do_backfill({
num_before: consts.backfill_batch_size, num_before: consts.backfill_batch_size,
msg_list: home_msg_list, msg_list: home_msg_list,
}); });
}, },
}); });
}; }
exports.initialize = function () { export function initialize() {
// get the initial message list // get the initial message list
function load_more(data) { function load_more(data) {
// If we haven't selected a message in the home view yet, and // If we haven't selected a message in the home view yet, and
@@ -415,7 +413,7 @@ exports.initialize = function () {
if (data.found_newest) { if (data.found_newest) {
server_events.home_view_loaded(); server_events.home_view_loaded();
exports.start_backfilling_messages(); start_backfilling_messages();
return; return;
} }
@@ -424,7 +422,7 @@ exports.initialize = function () {
const messages = data.messages; const messages = data.messages;
const latest_id = messages[messages.length - 1].id; const latest_id = messages[messages.length - 1].id;
exports.load_messages({ load_messages({
anchor: latest_id, anchor: latest_id,
num_before: 0, num_before: 0,
num_after: consts.catch_up_batch_size, num_after: consts.catch_up_batch_size,
@@ -443,7 +441,7 @@ exports.initialize = function () {
// the user's unmuted history as our anchor. // the user's unmuted history as our anchor.
anchor = "first_unread"; anchor = "first_unread";
} }
exports.load_messages({ load_messages({
anchor, anchor,
num_before: consts.num_before_home_anchor, num_before: consts.num_before_home_anchor,
num_after: consts.num_after_home_anchor, num_after: consts.num_after_home_anchor,
@@ -476,12 +474,10 @@ exports.initialize = function () {
filter: new Filter([{operator: "in", operand: "home"}]), filter: new Filter([{operator: "in", operand: "home"}]),
excludes_muted_topics: true, excludes_muted_topics: true,
}); });
exports.load_messages({ load_messages({
anchor: "newest", anchor: "newest",
num_before: consts.recent_topics_initial_fetch_size, num_before: consts.recent_topics_initial_fetch_size,
num_after: 0, num_after: 0,
msg_list: recent_topics_message_list, msg_list: recent_topics_message_list,
}); });
}; }
window.message_fetch = exports;

View File

@@ -3,6 +3,7 @@ import _ from "lodash";
import * as floating_recipient_bar from "./floating_recipient_bar"; import * as floating_recipient_bar from "./floating_recipient_bar";
import * as hash_util from "./hash_util"; import * as hash_util from "./hash_util";
import * as loading from "./loading"; import * as loading from "./loading";
import * as message_fetch from "./message_fetch";
import * as message_viewport from "./message_viewport"; import * as message_viewport from "./message_viewport";
import * as narrow_state from "./narrow_state"; import * as narrow_state from "./narrow_state";

View File

@@ -8,6 +8,7 @@ const condense = require("./condense");
const {Filter} = require("./filter"); const {Filter} = require("./filter");
const hashchange = require("./hashchange"); const hashchange = require("./hashchange");
const message_edit = require("./message_edit"); const message_edit = require("./message_edit");
const message_fetch = require("./message_fetch");
const {MessageListData} = require("./message_list_data"); const {MessageListData} = require("./message_list_data");
const message_scroll = require("./message_scroll"); const message_scroll = require("./message_scroll");
const message_store = require("./message_store"); const message_store = require("./message_store");

View File

@@ -24,6 +24,7 @@ const lightbox = require("./lightbox");
const markdown = require("./markdown"); const markdown = require("./markdown");
const markdown_config = require("./markdown_config"); const markdown_config = require("./markdown_config");
const message_edit = require("./message_edit"); const message_edit = require("./message_edit");
const message_fetch = require("./message_fetch");
const message_scroll = require("./message_scroll"); const message_scroll = require("./message_scroll");
const message_viewport = require("./message_viewport"); const message_viewport = require("./message_viewport");
const muting = require("./muting"); const muting = require("./muting");