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.

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

Anders Kaseorgf499c4a858e4
diff --git a/.eslintrc.json b/.eslintrc.json
index c3a1301b2d9..2cef39786e3 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -196,7 +196,6 @@
"search": false,
"search_pill_widget": false,
"search_suggestion": false,
- "sent_messages": false,
"server_events": false,
"settings": false,
"settings_account": false,
diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.js
index 7027ab975c9..45567edc675 100644
--- a/frontend_tests/node_tests/compose.js
+++ b/frontend_tests/node_tests/compose.js
@@ -38,6 +38,7 @@ const _drafts = {
};
const sent_messages = {
+ __esModule: true,
start_tracking_message: noop,
};
const _notifications = {
@@ -53,7 +54,7 @@ rewiremock("../../static/js/drafts").with(_drafts);
set_global("navigator", _navigator);
set_global("notifications", _notifications);
set_global("reminder", reminder);
-set_global("sent_messages", sent_messages);
+rewiremock("../../static/js/sent_messages").with(sent_messages);
rewiremock("../../static/js/rendered_markdown").with({
update_elements: () => {},
});
diff --git a/frontend_tests/node_tests/echo.js b/frontend_tests/node_tests/echo.js
index 94c627189e7..23d8c72fbee 100644
--- a/frontend_tests/node_tests/echo.js
+++ b/frontend_tests/node_tests/echo.js
@@ -24,7 +24,7 @@ set_global("ui", {
show_failed_message_success: () => {},
});
-set_global("sent_messages", {
+rewiremock("../../static/js/sent_messages").with({
mark_disparity: (local_id) => {
disparities.push(local_id);
},
diff --git a/frontend_tests/node_tests/server_events.js b/frontend_tests/node_tests/server_events.js
index 67fbad372b0..e04eb1771ec 100644
--- a/frontend_tests/node_tests/server_events.js
+++ b/frontend_tests/node_tests/server_events.js
@@ -55,7 +55,6 @@ rewiremock.enable();
zrequire("message_store");
const server_events = zrequire("server_events");
-zrequire("sent_messages");
set_global("$", $);
diff --git a/frontend_tests/node_tests/transmit.js b/frontend_tests/node_tests/transmit.js
index e6e41eaf796..0ab59000974 100644
--- a/frontend_tests/node_tests/transmit.js
+++ b/frontend_tests/node_tests/transmit.js
@@ -16,10 +16,13 @@ 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", {
+const sent_messages = {
+ __esModule: true,
start_tracking_message: noop,
report_server_ack: noop,
-});
+};
+
+rewiremock("../../static/js/sent_messages").with(sent_messages);
rewiremock.enable();
diff --git a/frontend_tests/node_tests/ui_init.js b/frontend_tests/node_tests/ui_init.js
index 76203fd9948..67ff7d507b9 100644
--- a/frontend_tests/node_tests/ui_init.js
+++ b/frontend_tests/node_tests/ui_init.js
@@ -128,7 +128,6 @@ zrequire("search_suggestion");
zrequire("search");
zrequire("notifications");
zrequire("stream_list");
-zrequire("sent_messages");
zrequire("starred_messages");
zrequire("recent_topics");
diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js
index dccd2ba4aa5..056609f3f75 100644
--- a/static/js/bundles/app.js
+++ b/static/js/bundles/app.js
@@ -24,7 +24,6 @@ import "../widgetize";
import "../message_list";
import "../narrow";
import "../reload";
-import "../sent_messages";
import "../compose_state";
import "../compose_actions";
import "../transmit";
diff --git a/static/js/compose.js b/static/js/compose.js
index 028c771e238..a369141cb63 100644
--- a/static/js/compose.js
+++ b/static/js/compose.js
@@ -21,6 +21,7 @@ const peer_data = require("./peer_data");
const people = require("./people");
const rendered_markdown = require("./rendered_markdown");
const rtl = require("./rtl");
+const sent_messages = require("./sent_messages");
const settings_config = require("./settings_config");
const util = require("./util");
const zcommand = require("./zcommand");
diff --git a/static/js/echo.js b/static/js/echo.js
index 0375b536ea2..360c2d26b76 100644
--- a/static/js/echo.js
+++ b/static/js/echo.js
@@ -5,6 +5,7 @@ import * as narrow_state from "./narrow_state";
import * as people from "./people";
import * as pm_list from "./pm_list";
import * as rows from "./rows";
+import * as sent_messages from "./sent_messages";
import * as util from "./util";
// Docs: https://zulip.readthedocs.io/en/latest/subsystems/sending-messages.html
diff --git a/static/js/global.d.ts b/static/js/global.d.ts
index d2969f11b34..3a3cfd4c8b2 100644
--- a/static/js/global.d.ts
+++ b/static/js/global.d.ts
@@ -69,7 +69,6 @@ declare let scroll_bar: any;
declare let search: any;
declare let search_pill_widget: any;
declare let search_suggestion: any;
-declare let sent_messages: any;
declare let server_events: any;
declare let settings: any;
declare let settings_account: any;
diff --git a/static/js/sent_messages.js b/static/js/sent_messages.js
index c879567f5e5..36686040fab 100644
--- a/static/js/sent_messages.js
+++ b/static/js/sent_messages.js
@@ -1,18 +1,17 @@
-"use strict";
+import * as channel from "./channel";
-const channel = require("./channel");
+export let next_local_id;
+export const messages = new Map();
-exports.messages = new Map();
-
-exports.reset_id_state = function () {
- exports.next_local_id = 0;
-};
+export function reset_id_state() {
+ next_local_id = 0;
+}
-exports.get_new_local_id = function () {
- exports.next_local_id += 1;
- const local_id = exports.next_local_id;
+export function get_new_local_id() {
+ next_local_id += 1;
+ const local_id = next_local_id;
return "loc-" + local_id.toString();
-};
+}
function report_send_time(send_time, receive_time, locally_echoed, rendered_changed) {
const data = {
@@ -31,7 +30,7 @@ function report_send_time(send_time, receive_time, locally_echoed, rendered_chan
});
}
-class MessageState {
+export class MessageState {
start = new Date();
received = undefined;
@@ -102,9 +101,8 @@ class MessageState {
return this.send_finished !== undefined && this.received !== undefined;
}
}
-exports.MessageState = MessageState;
-exports.start_tracking_message = function (opts) {
+export function start_tracking_message(opts) {
const local_id = opts.local_id;
if (!opts.local_id) {
@@ -112,63 +110,61 @@ exports.start_tracking_message = function (opts) {
return;
}
- if (exports.messages.has(local_id)) {
+ if (messages.has(local_id)) {
blueslip.error("We are re-using a local_id");
return;
}
- const state = new exports.MessageState(opts);
+ const state = new MessageState(opts);
- exports.messages.set(local_id, state);
-};
+ messages.set(local_id, state);
+}
-exports.get_message_state = function (local_id) {
- const state = exports.messages.get(local_id);
+export function get_message_state(local_id) {
+ const state = messages.get(local_id);
if (!state) {
blueslip.warn("Unknown local_id: " + local_id);
}
return state;
-};
+}
-exports.mark_disparity = function (local_id) {
- const state = exports.get_message_state(local_id);
+export function mark_disparity(local_id) {
+ const state = get_message_state(local_id);
if (!state) {
return;
}
state.mark_disparity();
-};
+}
-exports.report_event_received = function (local_id) {
- const state = exports.get_message_state(local_id);
+export function report_event_received(local_id) {
+ const state = get_message_state(local_id);
if (!state) {
return;
}
state.report_event_received();
-};
+}
-exports.start_resend = function (local_id) {
- const state = exports.get_message_state(local_id);
+export function start_resend(local_id) {
+ const state = get_message_state(local_id);
if (!state) {
return;
}
state.start_resend();
-};
+}
-exports.report_server_ack = function (local_id) {
- const state = exports.get_message_state(local_id);
+export function report_server_ack(local_id) {
+ const state = get_message_state(local_id);
if (!state) {
return;
}
state.report_server_ack();
-};
-
-exports.initialize = function () {
- exports.reset_id_state();
-};
+}
-window.sent_messages = exports;
+export function initialize() {
+ reset_id_state();
+}
diff --git a/static/js/server_events.js b/static/js/server_events.js
index 48c6b761e3f..daf7a3dedcd 100644
--- a/static/js/server_events.js
+++ b/static/js/server_events.js
@@ -6,6 +6,7 @@ const channel = require("./channel");
const echo = require("./echo");
const reload = require("./reload");
const reload_state = require("./reload_state");
+const sent_messages = require("./sent_messages");
const server_events_dispatch = require("./server_events_dispatch");
// Docs: https://zulip.readthedocs.io/en/latest/subsystems/events-system.html
diff --git a/static/js/transmit.js b/static/js/transmit.js
index 83f540b1168..c2ebaa95b1e 100644
--- a/static/js/transmit.js
+++ b/static/js/transmit.js
@@ -4,6 +4,7 @@ const channel = require("./channel");
const people = require("./people");
const reload = require("./reload");
const reload_state = require("./reload_state");
+const sent_messages = require("./sent_messages");
exports.send_message = function (request, on_success, error) {
channel.post({
diff --git a/static/js/ui_init.js b/static/js/ui_init.js
index e3a719b509b..1a8acce4463 100644
--- a/static/js/ui_init.js
+++ b/static/js/ui_init.js
@@ -24,6 +24,7 @@ const pm_conversations = require("./pm_conversations");
const pm_list = require("./pm_list");
const reload = require("./reload");
const rows = require("./rows");
+const sent_messages = require("./sent_messages");
const spoilers = require("./spoilers");
const topic_list = require("./topic_list");
const topic_zoom = require("./topic_zoom");