refactor: Extract stream_topic_history_util.

This breaks an indirect dependency of stream_data
on the channel module.

It's a verbatim code move, apart from the one-line
helper `has_history_for`. It's not totally clear
to me why the original code doesn't call into
`is_complete_for_stream_id` to early-exit, but
figuring that out is outside the scope of my
change.

It's possible that we will eventually just subsume
this tiny module into topic_list once we finish
breaking all dependencies, but we may want to
reuse this for something like Recent Topics
or other similar UIs.

It's also possible that we'll want to rename
stream_topic_history -> stream_topic_history_data
sometime soon, possibly after we clean up its
dependency on message_util soon.
This commit is contained in:
Steve Howell 2021-04-15 16:51:44 +00:00 committed by Tim Abbott
parent 545cd961f4
commit 12650c1bec
4 changed files with 28 additions and 21 deletions

View File

@ -12,6 +12,7 @@ const all_messages_data = zrequire("all_messages_data");
const unread = zrequire("unread"); const unread = zrequire("unread");
const stream_data = zrequire("stream_data"); const stream_data = zrequire("stream_data");
const stream_topic_history = zrequire("stream_topic_history"); const stream_topic_history = zrequire("stream_topic_history");
const stream_topic_history_util = zrequire("stream_topic_history_util");
function test(label, f) { function test(label, f) {
run_test(label, (override) => { run_test(label, (override) => {
@ -313,7 +314,7 @@ test("server_history_end_to_end", () => {
get_success_callback = opts.success; get_success_callback = opts.success;
}; };
stream_topic_history.get_server_history(stream_id, () => { stream_topic_history_util.get_server_history(stream_id, () => {
on_success_called = true; on_success_called = true;
}); });
@ -331,7 +332,7 @@ test("server_history_end_to_end", () => {
}; };
on_success_called = false; on_success_called = false;
stream_topic_history.get_server_history(stream_id, () => { stream_topic_history_util.get_server_history(stream_id, () => {
on_success_called = true; on_success_called = true;
}); });
assert(on_success_called); assert(on_success_called);

View File

@ -1,5 +1,4 @@
import {all_messages_data} from "./all_messages_data"; import {all_messages_data} from "./all_messages_data";
import * as channel from "./channel";
import {FoldDict} from "./fold_dict"; import {FoldDict} from "./fold_dict";
import * as message_util from "./message_util"; import * as message_util from "./message_util";
import * as stream_data from "./stream_data"; import * as stream_data from "./stream_data";
@ -291,23 +290,8 @@ export function add_history(stream_id, server_history) {
fetched_stream_ids.add(stream_id); fetched_stream_ids.add(stream_id);
} }
export function get_server_history(stream_id, on_success) { export function has_history_for(stream_id) {
if (fetched_stream_ids.has(stream_id)) { return fetched_stream_ids.has(stream_id);
on_success();
return;
}
const url = "/json/users/me/" + stream_id + "/topics";
channel.get({
url,
data: {},
success(data) {
const server_history = data.topics;
add_history(stream_id, server_history);
on_success();
},
});
} }
export function get_recent_topic_names(stream_id) { export function get_recent_topic_names(stream_id) {

View File

@ -0,0 +1,21 @@
import * as channel from "./channel";
import * as stream_topic_history from "./stream_topic_history";
export function get_server_history(stream_id, on_success) {
if (stream_topic_history.has_history_for(stream_id)) {
on_success();
return;
}
const url = "/json/users/me/" + stream_id + "/topics";
channel.get({
url,
data: {},
success(data) {
const server_history = data.topics;
stream_topic_history.add_history(stream_id, server_history);
on_success();
},
});
}

View File

@ -10,6 +10,7 @@ import * as narrow from "./narrow";
import * as stream_data from "./stream_data"; import * as stream_data from "./stream_data";
import * as stream_popover from "./stream_popover"; import * as stream_popover from "./stream_popover";
import * as stream_topic_history from "./stream_topic_history"; import * as stream_topic_history from "./stream_topic_history";
import * as stream_topic_history_util from "./stream_topic_history_util";
import * as topic_list_data from "./topic_list_data"; import * as topic_list_data from "./topic_list_data";
import * as ui from "./ui"; import * as ui from "./ui";
import * as vdom from "./vdom"; import * as vdom from "./vdom";
@ -251,7 +252,7 @@ export function zoom_in() {
const spinner = true; const spinner = true;
active_widget.build(spinner); active_widget.build(spinner);
stream_topic_history.get_server_history(stream_id, on_success); stream_topic_history_util.get_server_history(stream_id, on_success);
} }
export function initialize() { export function initialize() {