unread_ops: Centralize window focused logic.

This commit is contained in:
Tim Abbott 2023-05-31 16:35:55 -07:00
parent c961ad4f37
commit f99f567521
8 changed files with 52 additions and 62 deletions

View File

@ -3,8 +3,6 @@ import $ from "jquery";
import * as blueslip from "./blueslip";
import * as message_lists from "./message_lists";
import * as message_scroll from "./message_scroll";
import * as notifications from "./notifications";
import * as overlays from "./overlays";
import * as popovers from "./popovers";
import * as rows from "./rows";
import * as util from "./util";
@ -501,14 +499,3 @@ export function initialize() {
stop_auto_scrolling();
});
}
export function is_visible_and_focused() {
if (
overlays.is_overlay_or_modal_open() ||
!notifications.is_window_focused() ||
!$("#message_feed_container").is(":visible")
) {
return false;
}
return true;
}

View File

@ -23,16 +23,11 @@ import * as spoilers from "./spoilers";
import * as stream_data from "./stream_data";
import * as ui_util from "./ui_util";
import * as unread from "./unread";
import * as unread_ops from "./unread_ops";
import {user_settings} from "./user_settings";
import * as user_topics from "./user_topics";
const notice_memory = new Map();
// When you start Zulip, window_focused should be true, but it might not be the
// case after a server-initiated reload.
let window_focused = document.hasFocus && document.hasFocus();
let NotificationAPI;
export function set_notification_api(n) {
@ -73,21 +68,11 @@ export function get_notifications() {
}
export function initialize() {
$(window)
.on("focus", () => {
window_focused = true;
$(window).on("focus", () => {
for (const notice_mem_entry of notice_memory.values()) {
notice_mem_entry.obj.close();
}
notice_memory.clear();
// Update many places on the DOM to reflect unread
// counts.
unread_ops.process_visible();
})
.on("blur", () => {
window_focused = false;
});
update_notification_sound_source($("#user-notification-sound-audio"), user_settings);
@ -160,10 +145,6 @@ export function update_unread_counts(new_unread_count, new_pm_count) {
redraw_title();
}
export function is_window_focused() {
return window_focused;
}
function notify_unmute(muted_narrow, stream_id, topic_name) {
const $unmute_notification = $(
render_unmute_topic_banner({

View File

@ -107,6 +107,7 @@ import * as topic_zoom from "./topic_zoom";
import * as tutorial from "./tutorial";
import * as typing from "./typing";
import * as unread from "./unread";
import * as unread_ops from "./unread_ops";
import * as unread_ui from "./unread_ui";
import * as user_group_edit from "./user_group_edit";
import * as user_group_edit_members from "./user_group_edit_members";
@ -685,6 +686,7 @@ export function initialize_everything() {
search.initialize();
tutorial.initialize();
notifications.initialize();
unread_ops.initialize();
gear_menu.initialize();
giphy.initialize();
presence.initialize(presence_params);

View File

@ -14,6 +14,7 @@ import * as message_store from "./message_store";
import * as message_viewport from "./message_viewport";
import * as narrow_state from "./narrow_state";
import * as notifications from "./notifications";
import * as overlays from "./overlays";
import * as people from "./people";
import * as recent_topics_ui from "./recent_topics_ui";
import * as ui_report from "./ui_report";
@ -30,6 +31,14 @@ let loading_indicator_displayed = false;
const INITIAL_BATCH_SIZE = 1000;
const FOLLOWUP_BATCH_SIZE = 1000;
// When you start Zulip, window_focused should be true, but it might not be the
// case after a server-initiated reload.
let window_focused = document.hasFocus && document.hasFocus();
export function is_window_focused() {
return window_focused;
}
export function confirm_mark_all_as_read() {
const html_body = render_confirm_mark_all_as_read();
@ -423,7 +432,7 @@ export function process_scrolled_to_bottom() {
// If we ever materially change the algorithm for this function, we
// may need to update notifications.received_messages as well.
export function process_visible() {
if (message_viewport.is_visible_and_focused() && message_viewport.bottom_message_visible()) {
if (viewport_is_visible_and_focused() && message_viewport.bottom_message_visible()) {
process_scrolled_to_bottom();
}
}
@ -455,3 +464,28 @@ export function mark_pm_as_read(user_ids_string) {
const unread_msg_ids = unread.get_msg_ids_for_user_ids_string(user_ids_string);
message_flags.mark_as_read(unread_msg_ids);
}
export function viewport_is_visible_and_focused() {
if (
overlays.is_overlay_or_modal_open() ||
!is_window_focused() ||
!$("#message_feed_container").is(":visible")
) {
return false;
}
return true;
}
export function initialize() {
$(window)
.on("focus", () => {
window_focused = true;
// Update many places on the DOM to reflect unread
// counts.
process_visible();
})
.on("blur", () => {
window_focused = false;
});
}

View File

@ -2,8 +2,9 @@
const {strict: assert} = require("assert");
const {mock_esm, zrequire} = require("./lib/namespace");
const {mock_esm, set_global, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
const $ = require("./lib/zjquery");
/*
@ -49,6 +50,8 @@ const {run_test} = require("./lib/test");
value.)
*/
set_global("document", {hasFocus: () => true});
const channel = mock_esm("../src/channel");
const message_lists = mock_esm("../src/message_lists");
const message_viewport = mock_esm("../src/message_viewport");
@ -98,7 +101,7 @@ run_test("unread_ops", ({override}) => {
unread.process_loaded_messages(test_messages);
// Make our message_viewport appear visible.
override(message_viewport, "is_visible_and_focused", () => true);
$("#message_feed_container").show();
// Make our "test" message appear visible.
override(message_viewport, "bottom_message_visible", () => true);

View File

@ -8,6 +8,8 @@ const {run_test} = require("./lib/test");
const channel = mock_esm("../src/channel");
const message_live_update = mock_esm("../src/message_live_update");
set_global("document", {hasFocus: () => true});
mock_esm("../src/starred_messages", {
add() {},
get_starred_msg_ids: () => [1, 2, 3, 4, 5],

View File

@ -4,19 +4,12 @@ const {strict: assert} = require("assert");
const {addDays} = require("date-fns");
const {set_global, zrequire} = require("./lib/namespace");
const {zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
const {page_params} = require("./lib/zpage_params");
page_params.is_spectator = false;
// Dependencies
set_global("document", {
hasFocus() {
return true;
},
});
const {localstorage} = zrequire("localstorage");
const navbar_alerts = zrequire("navbar_alerts");
const notifications = zrequire("notifications");

View File

@ -2,23 +2,11 @@
const {strict: assert} = require("assert");
const {set_global, zrequire} = require("./lib/namespace");
const {zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
const $ = require("./lib/zjquery");
const {page_params, user_settings} = require("./lib/zpage_params");
// Dependencies
set_global("document", {
hasFocus() {
return true;
},
});
const _navigator = {
userAgent: "Mozilla/5.0 AppleWebKit/537.36 Chrome/64.0.3282.167 Safari/537.36",
};
set_global("navigator", _navigator);
const user_topics = zrequire("user_topics");
const stream_data = zrequire("stream_data");
const spoilers = zrequire("spoilers");