typing: Convert module to TypeScript.

This commit is contained in:
Varun Singh 2024-01-24 02:45:42 +05:30 committed by Tim Abbott
parent 391e2a8e01
commit e213d9505b
2 changed files with 37 additions and 16 deletions

View File

@ -248,7 +248,7 @@ EXEMPT_FILES = make_set(
"web/src/topic_popover.js",
"web/src/tutorial.js",
"web/src/types.ts",
"web/src/typing.js",
"web/src/typing.ts",
"web/src/typing_events.js",
"web/src/ui_init.js",
"web/src/ui_report.ts",

View File

@ -1,6 +1,7 @@
import $ from "jquery";
import * as typing_status from "../shared/src/typing_status";
import type {Recipient} from "../shared/src/typing_status";
import * as blueslip from "./blueslip";
import * as channel from "./channel";
@ -11,16 +12,26 @@ import * as people from "./people";
import * as stream_data from "./stream_data";
import {user_settings} from "./user_settings";
type TypingAPIRequest = {op: "start" | "stop"} & (
| {
to: string;
}
| {
type: string;
stream_id: string;
topic: string;
}
);
// This module handles the outbound side of typing indicators.
// We detect changes in the compose box and notify the server
// when we are typing. For the inbound side see typing_events.js.
// See docs/subsystems/typing-indicators.md for more details.
function send_typing_notification_ajax(data) {
channel.post({
function send_typing_notification_ajax(data: TypingAPIRequest): void {
void channel.post({
url: "/json/typing",
data,
success() {},
error(xhr) {
if (xhr.readyState !== 0) {
blueslip.warn("Failed to send typing event: " + xhr.responseText);
@ -29,7 +40,10 @@ function send_typing_notification_ajax(data) {
});
}
function send_direct_message_typing_notification(user_ids_array, operation) {
function send_direct_message_typing_notification(
user_ids_array: number[],
operation: "start" | "stop",
): void {
const data = {
to: JSON.stringify(user_ids_array),
op: operation,
@ -37,7 +51,11 @@ function send_direct_message_typing_notification(user_ids_array, operation) {
send_typing_notification_ajax(data);
}
function send_stream_typing_notification(stream_id, topic, operation) {
function send_stream_typing_notification(
stream_id: number,
topic: string,
operation: "start" | "stop",
): void {
const data = {
type: "stream",
stream_id: JSON.stringify(stream_id),
@ -47,7 +65,10 @@ function send_stream_typing_notification(stream_id, topic, operation) {
send_typing_notification_ajax(data);
}
function send_typing_notification_based_on_message_type(to, operation) {
function send_typing_notification_based_on_message_type(
to: Recipient,
operation: "start" | "stop",
): void {
if (to.message_type === "direct" && user_settings.send_private_typing_notifications) {
send_direct_message_typing_notification(to.ids, operation);
} else if (to.message_type === "stream" && user_settings.send_stream_typing_notifications) {
@ -55,7 +76,7 @@ function send_typing_notification_based_on_message_type(to, operation) {
}
}
function get_user_ids_array() {
function get_user_ids_array(): number[] | null {
const user_ids_string = compose_pm_pill.get_user_ids_string();
if (user_ids_string === "") {
return null;
@ -64,7 +85,7 @@ function get_user_ids_array() {
return people.user_ids_string_to_ids_array(user_ids_string);
}
function is_valid_conversation() {
function is_valid_conversation(): boolean {
const compose_empty = !compose_state.has_message_content();
if (compose_empty) {
return false;
@ -73,29 +94,29 @@ function is_valid_conversation() {
return true;
}
function get_current_time() {
function get_current_time(): number {
return Date.now();
}
function notify_server_start(to) {
function notify_server_start(to: Recipient): void {
send_typing_notification_based_on_message_type(to, "start");
}
function notify_server_stop(to) {
function notify_server_stop(to: Recipient): void {
send_typing_notification_based_on_message_type(to, "stop");
}
export function get_recipient() {
export function get_recipient(): Recipient | null {
const message_type = compose_state.get_message_type();
if (message_type === "private") {
return {
message_type: "direct",
ids: get_user_ids_array(),
ids: get_user_ids_array()!,
};
}
if (message_type === "stream") {
const stream_name = compose_state.stream_name();
const stream_id = stream_data.get_stream_id(stream_name);
const stream_id = stream_data.get_stream_id(stream_name)!;
const topic = compose_state.topic();
return {
message_type: "stream",
@ -106,7 +127,7 @@ export function get_recipient() {
return null;
}
export function initialize() {
export function initialize(): void {
const worker = {
get_current_time,
notify_server_start,