From 820dcc50a0b4f7d19cdaee71a36d6f585836c174 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 10 Oct 2023 16:39:55 -0700 Subject: [PATCH] sent_messages: Convert module to TypeScript. Signed-off-by: Anders Kaseorg --- docs/subsystems/logging.md | 2 +- tools/test-js-with-node | 2 +- .../{sent_messages.js => sent_messages.ts} | 42 +++++++++---------- 3 files changed, 23 insertions(+), 23 deletions(-) rename web/src/{sent_messages.js => sent_messages.ts} (68%) diff --git a/docs/subsystems/logging.md b/docs/subsystems/logging.md index c9b4092a7c..33773b0b0a 100644 --- a/docs/subsystems/logging.md +++ b/docs/subsystems/logging.md @@ -256,7 +256,7 @@ and report to the server the following whenever a message is sent: the server-rendered content, which can be used for statistics on how effective our [local echo system](markdown.md) is. -The code is all in `zerver/lib/report.py` and `web/src/sent_messages.js`. +The code is all in `zerver/lib/report.py` and `web/src/sent_messages.ts`. We have similar reporting for the time it takes to narrow / switch to a new view: diff --git a/tools/test-js-with-node b/tools/test-js-with-node index c5cef1c248..cde1f65838 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -184,7 +184,7 @@ EXEMPT_FILES = make_set( "web/src/scroll_bar.ts", "web/src/scroll_util.ts", "web/src/search.js", - "web/src/sent_messages.js", + "web/src/sent_messages.ts", "web/src/sentry.ts", "web/src/server_events.js", "web/src/settings.js", diff --git a/web/src/sent_messages.js b/web/src/sent_messages.ts similarity index 68% rename from web/src/sent_messages.js rename to web/src/sent_messages.ts index 0ca758a063..4cdf5c4982 100644 --- a/web/src/sent_messages.js +++ b/web/src/sent_messages.ts @@ -3,31 +3,31 @@ import * as Sentry from "@sentry/browser"; import * as blueslip from "./blueslip"; export let next_local_id = 0; -export const messages = new Map(); +export const messages = new Map(); -export function get_new_local_id() { +export function get_new_local_id(): string { next_local_id += 1; const local_id = next_local_id; return "loc-" + local_id.toString(); } export class MessageState { - local_id = undefined; - locally_echoed = undefined; + local_id: string; + locally_echoed: boolean; rendered_changed = false; server_acked = false; saw_event = false; - txn = undefined; - event_span = undefined; + txn: Sentry.Transaction | undefined = undefined; + event_span: Sentry.Span | undefined = undefined; - constructor(opts) { + constructor(opts: {local_id: string; locally_echoed: boolean}) { this.local_id = opts.local_id; this.locally_echoed = opts.locally_echoed; } - start_send() { + start_send(): Sentry.Transaction { this.txn = Sentry.startTransaction({ op: "function", description: "message send", @@ -40,16 +40,16 @@ export class MessageState { return this.txn; } - mark_disparity() { + mark_disparity(): void { this.rendered_changed = true; } - report_server_ack() { + report_server_ack(): void { this.server_acked = true; this.maybe_finish_txn(); } - report_event_received() { + report_event_received(): void { if (!this.event_span) { return; } @@ -58,23 +58,23 @@ export class MessageState { this.maybe_finish_txn(); } - maybe_finish_txn() { + maybe_finish_txn(): void { if (!this.saw_event || !this.server_acked) { return; } - const setTag = (name, val) => { + const setTag = (name: string, val: boolean): void => { const str_val = val ? "true" : "false"; - this.event_span.setTag(name, str_val); - this.txn.setTag(name, str_val); + this.event_span!.setTag(name, str_val); + this.txn!.setTag(name, str_val); }; setTag("rendered_changed", this.rendered_changed); setTag("locally_echoed", this.locally_echoed); - this.txn.finish(); + this.txn!.finish(); messages.delete(this.local_id); } } -export function start_tracking_message(opts) { +export function start_tracking_message(opts: {local_id: string; locally_echoed: boolean}): void { const local_id = opts.local_id; if (!opts.local_id) { @@ -92,7 +92,7 @@ export function start_tracking_message(opts) { messages.set(local_id, state); } -export function get_message_state(local_id) { +export function get_message_state(local_id: string): MessageState | undefined { const state = messages.get(local_id); if (!state) { @@ -102,7 +102,7 @@ export function get_message_state(local_id) { return state; } -export function start_send(local_id) { +export function start_send(local_id: string): Sentry.Transaction | undefined { const state = get_message_state(local_id); if (!state) { return undefined; @@ -111,7 +111,7 @@ export function start_send(local_id) { return state.start_send(); } -export function mark_disparity(local_id) { +export function mark_disparity(local_id: string): void { const state = get_message_state(local_id); if (!state) { return; @@ -119,7 +119,7 @@ export function mark_disparity(local_id) { state.mark_disparity(); } -export function report_event_received(local_id) { +export function report_event_received(local_id: string): void { if (local_id === undefined) { return; }