From 5f9b6d82a4b0f024f979444d140096c073ce77c8 Mon Sep 17 00:00:00 2001 From: Varun Singh Date: Sun, 11 Feb 2024 00:05:11 +0530 Subject: [PATCH] typing_events: Convert module to TypeScript. --- tools/test-js-with-node | 2 +- .../{typing_events.js => typing_events.ts} | 36 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) rename web/src/{typing_events.js => typing_events.ts} (82%) diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 51e8423968..d4e521b049 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -249,7 +249,7 @@ EXEMPT_FILES = make_set( "web/src/tutorial.js", "web/src/types.ts", "web/src/typing.ts", - "web/src/typing_events.js", + "web/src/typing_events.ts", "web/src/ui_init.js", "web/src/ui_report.ts", "web/src/ui_util.ts", diff --git a/web/src/typing_events.js b/web/src/typing_events.ts similarity index 82% rename from web/src/typing_events.js rename to web/src/typing_events.ts index 4c3e51e210..07f89a7645 100644 --- a/web/src/typing_events.js +++ b/web/src/typing_events.ts @@ -1,4 +1,5 @@ import $ from "jquery"; +import assert from "minimalistic-assert"; import render_typing_notifications from "../templates/typing_notifications.hbs"; @@ -22,10 +23,35 @@ const MAX_USERS_TO_DISPLAY_NAME = 3; // Note!: There are also timing constants in typing_status.js // that make typing indicators work. -function get_users_typing_for_narrow() { +type UserInfo = { + email: string; + user_id: number; +}; + +type TypingEvent = { + id: number; + op: "start" | "stop"; + type: "typing"; +} & ( + | { + message_type: "stream"; + sender: UserInfo; + stream_id: number; + topic: string; + } + | { + message_type: "direct"; + recipients: UserInfo[]; + sender: UserInfo; + } +); + +function get_users_typing_for_narrow(): number[] { if (narrow_state.narrowed_by_topic_reply()) { const current_stream_id = narrow_state.stream_id(); const current_topic = narrow_state.topic(); + assert(current_stream_id !== undefined); + assert(current_topic !== undefined); return typing_data.get_topic_typists(current_stream_id, current_topic); } @@ -58,7 +84,7 @@ function get_users_typing_for_narrow() { return typing_data.get_all_direct_message_typists(); } -export function render_notifications_for_narrow() { +export function render_notifications_for_narrow(): void { const user_ids = get_users_typing_for_narrow(); const users_typing = user_ids .map((user_id) => people.get_user_by_id_assert_valid(user_id)) @@ -78,7 +104,7 @@ export function render_notifications_for_narrow() { } } -function get_key(event) { +function get_key(event: TypingEvent): string { if (event.message_type === "stream") { return typing_data.get_topic_key(event.stream_id, event.topic); } @@ -90,7 +116,7 @@ function get_key(event) { throw new Error("Invalid typing notification type", event); } -export function hide_notification(event) { +export function hide_notification(event: TypingEvent): void { const key = get_key(event); typing_data.clear_inbound_timer(key); @@ -101,7 +127,7 @@ export function hide_notification(event) { } } -export function display_notification(event) { +export function display_notification(event: TypingEvent): void { const sender_id = event.sender.user_id; const key = get_key(event);