unread_ui: Add hooks for update_unread_counts method.

Refactored `update_unread_counts` method to adapt a hook pattern. This
helps us to cut off many dependencies for `unread_ui` module.
This commit is contained in:
Tim Abbott
2023-05-31 16:46:19 -07:00
parent e7d19866d9
commit 16acb54e6a
3 changed files with 35 additions and 15 deletions

View File

@@ -123,7 +123,9 @@ export function redraw_title() {
document.title = new_title; document.title = new_title;
} }
export function update_unread_counts(new_unread_count, new_pm_count) { export function update_unread_counts(counts) {
const new_unread_count = unread.calculate_notifiable_count(counts);
const new_pm_count = counts.private_message_count;
if (new_unread_count === unread_count && new_pm_count === pm_count) { if (new_unread_count === unread_count && new_pm_count === pm_count) {
return; return;
} }

View File

@@ -457,6 +457,27 @@ export function initialize_kitchen_sink_stuff() {
} }
} }
function initialize_unread_ui() {
unread_ui.register_update_unread_counts_hook((counts) =>
activity.update_dom_with_unread_counts(counts),
);
unread_ui.register_update_unread_counts_hook((counts, skip_animations) =>
top_left_corner.update_dom_with_unread_counts(counts, skip_animations),
);
unread_ui.register_update_unread_counts_hook((counts) =>
stream_list.update_dom_with_unread_counts(counts),
);
unread_ui.register_update_unread_counts_hook((counts) =>
pm_list.update_dom_with_unread_counts(counts),
);
unread_ui.register_update_unread_counts_hook(() => topic_list.update());
unread_ui.register_update_unread_counts_hook((counts) =>
notifications.update_unread_counts(counts),
);
unread_ui.initialize({notify_server_messages_read: unread_ops.notify_server_messages_read});
}
export function initialize_everything() { export function initialize_everything() {
/* /*
When we initialize our various modules, a lot When we initialize our various modules, a lot
@@ -707,7 +728,7 @@ export function initialize_everything() {
// All overlays must be initialized before hashchange.js // All overlays must be initialized before hashchange.js
hashchange.initialize(); hashchange.initialize();
unread_ui.initialize({notify_server_messages_read: unread_ops.notify_server_messages_read}); initialize_unread_ui();
activity.initialize(); activity.initialize();
emoji_picker.initialize(); emoji_picker.initialize();
pm_list.initialize(); pm_list.initialize();

View File

@@ -4,21 +4,21 @@ import render_mark_as_read_disabled_banner from "../templates/unread_banner/mark
import render_mark_as_read_only_in_conversation_view from "../templates/unread_banner/mark_as_read_only_in_conversation_view.hbs"; import render_mark_as_read_only_in_conversation_view from "../templates/unread_banner/mark_as_read_only_in_conversation_view.hbs";
import render_mark_as_read_turned_off_banner from "../templates/unread_banner/mark_as_read_turned_off_banner.hbs"; import render_mark_as_read_turned_off_banner from "../templates/unread_banner/mark_as_read_turned_off_banner.hbs";
import * as activity from "./activity";
import * as message_lists from "./message_lists"; import * as message_lists from "./message_lists";
import * as narrow_state from "./narrow_state"; import * as narrow_state from "./narrow_state";
import * as notifications from "./notifications";
import {page_params} from "./page_params"; import {page_params} from "./page_params";
import * as pm_list from "./pm_list";
import {web_mark_read_on_scroll_policy_values} from "./settings_config"; import {web_mark_read_on_scroll_policy_values} from "./settings_config";
import * as stream_list from "./stream_list";
import * as top_left_corner from "./top_left_corner";
import * as topic_list from "./topic_list";
import * as unread from "./unread"; import * as unread from "./unread";
import {user_settings} from "./user_settings"; import {user_settings} from "./user_settings";
let user_closed_unread_banner = false; let user_closed_unread_banner = false;
const update_unread_counts_hooks = [];
export function register_update_unread_counts_hook(f) {
update_unread_counts_hooks.push(f);
}
export function update_unread_banner() { export function update_unread_banner() {
const filter = narrow_state.filter(); const filter = narrow_state.filter();
const is_conversation_view = filter === undefined ? false : filter.is_conversation_view(); const is_conversation_view = filter === undefined ? false : filter.is_conversation_view();
@@ -81,13 +81,10 @@ export function update_unread_counts(skip_animations = false) {
// Side effects from here down: // Side effects from here down:
// This updates some DOM elements directly, so try to // This updates some DOM elements directly, so try to
// avoid excessive calls to this. // avoid excessive calls to this.
activity.update_dom_with_unread_counts(res); // See `ui_init.initialize_unread_ui` for the registered hooks.
top_left_corner.update_dom_with_unread_counts(res, skip_animations); for (const hook of update_unread_counts_hooks) {
stream_list.update_dom_with_unread_counts(res); hook(res, skip_animations);
pm_list.update_dom_with_unread_counts(res); }
topic_list.update();
const notifiable_unread_count = unread.calculate_notifiable_count(res);
notifications.update_unread_counts(notifiable_unread_count, res.private_message_count);
// Set the unread counts that we show in the buttons that // Set the unread counts that we show in the buttons that
// toggle open the sidebar menus when we have a thin window. // toggle open the sidebar menus when we have a thin window.