From f1db30418b542d2304c716bee96717e45a93dac6 Mon Sep 17 00:00:00 2001 From: Varun Singh Date: Thu, 28 Mar 2024 20:33:20 +0530 Subject: [PATCH] submessage: Convert module to TypeScript. --- tools/test-js-with-node | 2 +- web/src/{submessage.js => submessage.ts} | 27 +++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) rename web/src/{submessage.js => submessage.ts} (84%) diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 76c442cf37..31099659b0 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -243,7 +243,7 @@ EXEMPT_FILES = make_set( "web/src/stream_settings_containers.ts", "web/src/stream_settings_ui.js", "web/src/stream_ui_updates.js", - "web/src/submessage.js", + "web/src/submessage.ts", "web/src/subscriber_api.ts", "web/src/timerender.ts", "web/src/tippyjs.ts", diff --git a/web/src/submessage.js b/web/src/submessage.ts similarity index 84% rename from web/src/submessage.js rename to web/src/submessage.ts index 93014a535e..ddc8f788e1 100644 --- a/web/src/submessage.js +++ b/web/src/submessage.ts @@ -3,6 +3,8 @@ import z from "zod"; import * as blueslip from "./blueslip"; import * as channel from "./channel"; import * as message_store from "./message_store"; +import type {Message} from "./message_store"; +import type {Submessage} from "./types"; import * as widgetize from "./widgetize"; export const zform_widget_extra_data_schema = z.object({ @@ -47,7 +49,9 @@ const submessages_event_schema = z .tuple([widget_data_event_schema]) .rest(inbound_data_event_schema); -export function get_message_events(message) { +type SubmessageEvents = z.infer; + +export function get_message_events(message: Message): SubmessageEvents | undefined { if (message.locally_echoed) { return undefined; } @@ -70,18 +74,19 @@ export function get_message_events(message) { return clean_events; } -export function process_submessages(in_opts) { +export function process_submessages(in_opts: {$row: JQuery; message_id: number}): void { // This happens in our rendering path, so we try to limit any // damage that may be triggered by one rogue message. try { - return do_process_submessages(in_opts); + do_process_submessages(in_opts); + return; } catch (error) { blueslip.error("Failed to do_process_submessages", undefined, error); - return undefined; + return; } } -export function do_process_submessages(in_opts) { +export function do_process_submessages(in_opts: {$row: JQuery; message_id: number}): void { const message_id = in_opts.message_id; const message = message_store.get(message_id); @@ -129,7 +134,7 @@ export function do_process_submessages(in_opts) { }); } -export function update_message(submsg) { +export function update_message(submsg: Submessage): void { const message = message_store.get(submsg.message_id); if (message === undefined) { @@ -154,7 +159,7 @@ export function update_message(submsg) { message.submessages.push(submsg); } -export function handle_event(submsg) { +export function handle_event(submsg: Submessage): void { // Update message.submessages in case we haven't actually // activated the widget yet, so that when the message does // come in view, the data will be complete. @@ -184,11 +189,13 @@ export function handle_event(submsg) { }); } -export function make_server_callback(message_id) { - return function (opts) { +export function make_server_callback( + message_id: number, +): (opts: {msg_type: string; data: string}) => void { + return function (opts: {msg_type: string; data: string}) { const url = "/json/submessage"; - channel.post({ + void channel.post({ url, data: { message_id,