sent_messages: Convert module to TypeScript.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-10-10 16:39:55 -07:00 committed by Tim Abbott
parent 082a291bd1
commit 820dcc50a0
3 changed files with 23 additions and 23 deletions

View File

@ -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:

View File

@ -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",

View File

@ -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<string, MessageState>();
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;
}