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/stream_topic_history.js to ES6 module.

Anders Kaseorgad3390e395fc
diff --git a/.eslintrc.json b/.eslintrc.json
index ea32f55d4e0..da10ab6a92f 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -214,7 +214,6 @@
"starred_messages": false,
"stream_edit": false,
"stream_events": false,
- "stream_topic_history": false,
"stream_list": false,
"stream_muting": false,
"stream_popover": false,
diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.js
index 18c014996ac..5a7954ce002 100644
--- a/frontend_tests/node_tests/composebox_typeahead.js
+++ b/frontend_tests/node_tests/composebox_typeahead.js
@@ -10,7 +10,9 @@ const $ = require("../zjsunit/zjquery");
const noop = () => {};
-const stream_topic_history = set_global("stream_topic_history", {});
+const stream_topic_history = {__esModule: true};
+
+rewiremock("../../static/js/stream_topic_history").with(stream_topic_history);
const message_store = set_global("message_store", {
user_ids: () => [],
diff --git a/frontend_tests/node_tests/message_store.js b/frontend_tests/node_tests/message_store.js
index d46505ac8ae..a8ee8d7a13b 100644
--- a/frontend_tests/node_tests/message_store.js
+++ b/frontend_tests/node_tests/message_store.js
@@ -12,7 +12,7 @@ const noop = () => {};
set_global("document", "document-stub");
-set_global("stream_topic_history", {
+rewiremock("../../static/js/stream_topic_history").with({
add_message: noop,
});
diff --git a/frontend_tests/node_tests/narrow.js b/frontend_tests/node_tests/narrow.js
index d7c3eb7cf69..d7ea21bb111 100644
--- a/frontend_tests/node_tests/narrow.js
+++ b/frontend_tests/node_tests/narrow.js
@@ -2,6 +2,8 @@
const {strict: assert} = require("assert");
+const rewiremock = require("rewiremock/node");
+
const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
@@ -14,13 +16,16 @@ set_global("resize", {
resize_stream_filters_container: () => {},
});
+rewiremock.enable();
+
const hash_util = zrequire("hash_util");
zrequire("hashchange");
const compose_state = zrequire("compose_state");
const narrow_state = zrequire("narrow_state");
const people = zrequire("people");
const stream_data = zrequire("stream_data");
-const stream_topic_history = set_global("stream_topic_history", {});
+const stream_topic_history = {__esModule: true};
+rewiremock("../../static/js/stream_topic_history").with(stream_topic_history);
const {Filter} = zrequire("Filter", "js/filter");
const narrow = zrequire("narrow");
@@ -372,3 +377,4 @@ run_test("narrow_to_compose_target PMs", (override) => {
assert.equal(args.called, true);
assert.deepEqual(args.operators, [{operator: "is", operand: "private"}]);
});
+rewiremock.disable();
diff --git a/frontend_tests/node_tests/topic_generator.js b/frontend_tests/node_tests/topic_generator.js
index a34c7079cd5..497a8294f02 100644
--- a/frontend_tests/node_tests/topic_generator.js
+++ b/frontend_tests/node_tests/topic_generator.js
@@ -85,7 +85,7 @@ run_test("topics", () => {
devel: devel_stream_id,
};
- stream_topic_history.get_recent_topic_names = (stream_id) => {
+ stream_topic_history.__Rewire__("get_recent_topic_names", (stream_id) => {
switch (stream_id) {
case muted_stream_id:
return ["ms-topic1", "ms-topic2"];
@@ -94,7 +94,7 @@ run_test("topics", () => {
}
return [];
- };
+ });
stream_data.__Rewire__("get_stream_id", (stream_name) => stream_id_dct[stream_name]);
diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js
index 93a77797566..457c757b1ae 100644
--- a/static/js/bundles/app.js
+++ b/static/js/bundles/app.js
@@ -25,7 +25,6 @@ import "../message_list";
import "../narrow";
import "../reload";
import "../compose_actions";
-import "../stream_topic_history";
import "../stream_muting";
import "../stream_events";
import "../stream_edit";
diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.js
index f7100572614..55c228c6ba1 100644
--- a/static/js/composebox_typeahead.js
+++ b/static/js/composebox_typeahead.js
@@ -17,6 +17,7 @@ const people = require("./people");
const rows = require("./rows");
const settings_data = require("./settings_data");
const stream_data = require("./stream_data");
+const stream_topic_history = require("./stream_topic_history");
const user_groups = require("./user_groups");
const user_pill = require("./user_pill");
diff --git a/static/js/echo.js b/static/js/echo.js
index 0e0852bcf9e..4bccb4f7460 100644
--- a/static/js/echo.js
+++ b/static/js/echo.js
@@ -7,6 +7,7 @@ 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 stream_topic_history from "./stream_topic_history";
import * as transmit from "./transmit";
import * as util from "./util";
diff --git a/static/js/global.d.ts b/static/js/global.d.ts
index 4a007b29f91..f0f270075ff 100644
--- a/static/js/global.d.ts
+++ b/static/js/global.d.ts
@@ -93,7 +93,6 @@ declare let stream_popover: any;
declare let subs: any;
declare let message_view_header: any;
declare let timerender: any;
-declare let stream_topic_history: any;
declare let typeahead_helper: any;
declare let typing_events: any;
declare let ui: any;
diff --git a/static/js/message_events.js b/static/js/message_events.js
index 45999fdd6dd..674d0b6d845 100644
--- a/static/js/message_events.js
+++ b/static/js/message_events.js
@@ -10,6 +10,7 @@ const narrow_state = require("./narrow_state");
const pm_list = require("./pm_list");
const recent_senders = require("./recent_senders");
const stream_data = require("./stream_data");
+const stream_topic_history = require("./stream_topic_history");
const unread_ui = require("./unread_ui");
const util = require("./util");
diff --git a/static/js/message_store.js b/static/js/message_store.js
index 459ade3df16..c1fc0e03993 100644
--- a/static/js/message_store.js
+++ b/static/js/message_store.js
@@ -4,6 +4,7 @@ const alert_words = require("./alert_words");
const people = require("./people");
const pm_conversations = require("./pm_conversations");
const recent_senders = require("./recent_senders");
+const stream_topic_history = require("./stream_topic_history");
const util = require("./util");
const stored_messages = new Map();
diff --git a/static/js/narrow.js b/static/js/narrow.js
index 75b95a4adf2..706d1664306 100644
--- a/static/js/narrow.js
+++ b/static/js/narrow.js
@@ -10,6 +10,7 @@ const narrow_state = require("./narrow_state");
const people = require("./people");
const search_pill = require("./search_pill");
const stream_data = require("./stream_data");
+const stream_topic_history = require("./stream_topic_history");
const top_left_corner = require("./top_left_corner");
const topic_generator = require("./topic_generator");
const util = require("./util");
diff --git a/static/js/search_suggestion.js b/static/js/search_suggestion.js
index ab3aa8b935c..410def28e40 100644
--- a/static/js/search_suggestion.js
+++ b/static/js/search_suggestion.js
@@ -9,6 +9,7 @@ const narrow_state = require("./narrow_state");
const people = require("./people");
const settings_data = require("./settings_data");
const stream_data = require("./stream_data");
+const stream_topic_history = require("./stream_topic_history");
exports.max_num_of_search_results = 12;
diff --git a/static/js/server_events_dispatch.js b/static/js/server_events_dispatch.js
index 6209b939e98..fee7802cdad 100644
--- a/static/js/server_events_dispatch.js
+++ b/static/js/server_events_dispatch.js
@@ -11,6 +11,7 @@ import * as people from "./people";
import * as reload from "./reload";
import * as settings_config from "./settings_config";
import * as stream_data from "./stream_data";
+import * as stream_topic_history from "./stream_topic_history";
import * as submessage from "./submessage";
import * as user_groups from "./user_groups";
import * as user_status from "./user_status";
diff --git a/static/js/stream_data.js b/static/js/stream_data.js
index d1c80bacb55..f152dac31cc 100644
--- a/static/js/stream_data.js
+++ b/static/js/stream_data.js
@@ -4,6 +4,7 @@ import * as peer_data from "./peer_data";
import * as people from "./people";
import * as settings_config from "./settings_config";
import * as stream_color from "./stream_color";
+import * as stream_topic_history from "./stream_topic_history";
import * as util from "./util";
// Expose get_subscriber_count for our automated puppeteer tests.
diff --git a/static/js/stream_topic_history.js b/static/js/stream_topic_history.js
index d476bdde1db..92157003ecf 100644
--- a/static/js/stream_topic_history.js
+++ b/static/js/stream_topic_history.js
@@ -1,13 +1,11 @@
-"use strict";
-
-const channel = require("./channel");
-const {FoldDict} = require("./fold_dict");
-const stream_data = require("./stream_data");
+import * as channel from "./channel";
+import {FoldDict} from "./fold_dict";
+import * as stream_data from "./stream_data";
const stream_dict = new Map(); // stream_id -> PerStreamHistory object
const fetched_stream_ids = new Set();
-exports.is_complete_for_stream_id = (stream_id) => {
+export function is_complete_for_stream_id(stream_id) {
if (fetched_stream_ids.has(stream_id)) {
return true;
}
@@ -34,9 +32,9 @@ exports.is_complete_for_stream_id = (stream_id) => {
}
return in_cache;
-};
+}
-exports.stream_has_topics = function (stream_id) {
+export function stream_has_topics(stream_id) {
if (!stream_dict.has(stream_id)) {
return false;
}
@@ -44,9 +42,9 @@ exports.stream_has_topics = function (stream_id) {
const history = stream_dict.get(stream_id);
return history.has_topics();
-};
+}
-class PerStreamHistory {
+export class PerStreamHistory {
/*
For a given stream, this structure has a dictionary of topics.
The main getter of this object is get_recent_topic_names, and
@@ -196,9 +194,8 @@ class PerStreamHistory {
return this.max_message_id;
}
}
-exports.PerStreamHistory = PerStreamHistory;
-exports.remove_messages = function (opts) {
+export function remove_messages(opts) {
const stream_id = opts.stream_id;
const topic_name = opts.topic_name;
const num_messages = opts.num_messages;
@@ -236,9 +233,9 @@ exports.remove_messages = function (opts) {
if (history.max_message_id <= max_removed_msg_id) {
history.max_message_id = message_util.get_max_message_id_in_stream(stream_id);
}
-};
+}
-exports.find_or_create = function (stream_id) {
+export function find_or_create(stream_id) {
let history = stream_dict.get(stream_id);
if (!history) {
@@ -247,28 +244,28 @@ exports.find_or_create = function (stream_id) {
}
return history;
-};
+}
-exports.add_message = function (opts) {
+export function add_message(opts) {
const stream_id = opts.stream_id;
const message_id = opts.message_id;
const topic_name = opts.topic_name;
- const history = exports.find_or_create(stream_id);
+ const history = find_or_create(stream_id);
history.add_or_update({
topic_name,
message_id,
});
-};
+}
-exports.add_history = function (stream_id, server_history) {
- const history = exports.find_or_create(stream_id);
+export function add_history(stream_id, server_history) {
+ const history = find_or_create(stream_id);
history.add_history(server_history);
fetched_stream_ids.add(stream_id);
-};
+}
-exports.get_server_history = function (stream_id, on_success) {
+export function get_server_history(stream_id, on_success) {
if (fetched_stream_ids.has(stream_id)) {
on_success();
return;
@@ -281,28 +278,26 @@ exports.get_server_history = function (stream_id, on_success) {
data: {},
success(data) {
const server_history = data.topics;
- exports.add_history(stream_id, server_history);
+ add_history(stream_id, server_history);
on_success();
},
});
-};
+}
-exports.get_recent_topic_names = function (stream_id) {
- const history = exports.find_or_create(stream_id);
+export function get_recent_topic_names(stream_id) {
+ const history = find_or_create(stream_id);
return history.get_recent_topic_names();
-};
+}
-exports.get_max_message_id = function (stream_id) {
- const history = exports.find_or_create(stream_id);
+export function get_max_message_id(stream_id) {
+ const history = find_or_create(stream_id);
return history.get_max_message_id();
-};
+}
-exports.reset = function () {
+export function reset() {
// This is only used by tests.
stream_dict.clear();
fetched_stream_ids.clear();
-};
-
-window.stream_topic_history = exports;
+}
diff --git a/static/js/topic_generator.js b/static/js/topic_generator.js
index 95fec255f03..c3ab08913f4 100644
--- a/static/js/topic_generator.js
+++ b/static/js/topic_generator.js
@@ -2,6 +2,7 @@ import * as muting from "./muting";
import * as pm_conversations from "./pm_conversations";
import * as stream_data from "./stream_data";
import * as stream_sort from "./stream_sort";
+import * as stream_topic_history from "./stream_topic_history";
export function next_topic(streams, get_topics, has_unread_messages, curr_stream, curr_topic) {
const curr_stream_index = streams.indexOf(curr_stream); // -1 if not found
diff --git a/static/js/topic_list.js b/static/js/topic_list.js
index 9587f13fd51..af3b05e22fa 100644
--- a/static/js/topic_list.js
+++ b/static/js/topic_list.js
@@ -5,6 +5,7 @@ import render_more_topics_spinner from "../templates/more_topics_spinner.hbs";
import render_topic_list_item from "../templates/topic_list_item.hbs";
import * as stream_data from "./stream_data";
+import * as stream_topic_history from "./stream_topic_history";
import * as topic_list_data from "./topic_list_data";
import * as vdom from "./vdom";
diff --git a/static/js/topic_list_data.js b/static/js/topic_list_data.js
index 4808bcb45ab..3b7331054ca 100644
--- a/static/js/topic_list_data.js
+++ b/static/js/topic_list_data.js
@@ -1,5 +1,6 @@
import * as muting from "./muting";
import * as narrow_state from "./narrow_state";
+import * as stream_topic_history from "./stream_topic_history";
const max_topics = 5;
const max_topics_with_unread = 8;