echo: Extract the waiting for id and ack into separate module.

This commit extracts the "waiting_for_id" and "waiting_for_ack"
data structures of "echo.js" into a separate module in "echo_state".

This is a preparatory commit so as to be able to use them for
"stream_topic_history" module, without causing import cycles.
This commit is contained in:
roanster007 2024-08-03 17:43:29 +05:30 committed by Tim Abbott
parent 19d56f77b5
commit e104fed6ef
3 changed files with 45 additions and 18 deletions

View File

@ -7,6 +7,7 @@ import {all_messages_data} from "./all_messages_data";
import * as blueslip from "./blueslip"; import * as blueslip from "./blueslip";
import * as compose_notifications from "./compose_notifications"; import * as compose_notifications from "./compose_notifications";
import * as compose_ui from "./compose_ui"; import * as compose_ui from "./compose_ui";
import * as echo_state from "./echo_state";
import * as local_message from "./local_message"; import * as local_message from "./local_message";
import * as markdown from "./markdown"; import * as markdown from "./markdown";
import * as message_events_util from "./message_events_util"; import * as message_events_util from "./message_events_util";
@ -95,9 +96,6 @@ type LocalMessage = MessageRequestObject & {
type PostMessageAPIData = z.output<typeof send_message_api_response_schema>; type PostMessageAPIData = z.output<typeof send_message_api_response_schema>;
const waiting_for_id = new Map<string, Message>();
let waiting_for_ack = new Map<string, Message>();
// These retry spinner functions return true if and only if the // These retry spinner functions return true if and only if the
// spinner already is in the requested state, which can be used to // spinner already is in the requested state, which can be used to
// avoid sending duplicate requests. // avoid sending duplicate requests.
@ -273,8 +271,8 @@ export function insert_local_message(
const [message] = insert_new_messages([local_message], true, true); const [message] = insert_new_messages([local_message], true, true);
assert(message !== undefined); assert(message !== undefined);
assert(message.local_id !== undefined); assert(message.local_id !== undefined);
waiting_for_id.set(message.local_id, message); echo_state.set_message_waiting_for_id(message.local_id, message);
waiting_for_ack.set(message.local_id, message); echo_state.set_message_waiting_for_ack(message.local_id, message);
return message; return message;
} }
@ -428,8 +426,8 @@ export function edit_locally(message: Message, request: LocalEditRequest): Messa
} }
export function reify_message_id(local_id: string, server_id: number): void { export function reify_message_id(local_id: string, server_id: number): void {
const message = waiting_for_id.get(local_id); const message = echo_state.get_message_waiting_for_id(local_id);
waiting_for_id.delete(local_id); echo_state.remove_message_from_waiting_for_id(local_id);
// reify_message_id is called both on receiving a self-sent message // reify_message_id is called both on receiving a self-sent message
// from the server, and on receiving the response to the send request // from the server, and on receiving the response to the send request
@ -481,7 +479,7 @@ export function process_from_server(messages: ServerMessage[]): ServerMessage[]
continue; continue;
} }
const client_message = waiting_for_ack.get(local_id); const client_message = echo_state.get_message_waiting_for_ack(local_id);
if (client_message === undefined) { if (client_message === undefined) {
non_echo_messages.push(message); non_echo_messages.push(message);
continue; continue;
@ -519,7 +517,7 @@ export function process_from_server(messages: ServerMessage[]): ServerMessage[]
client_message.submessages = message.submessages; client_message.submessages = message.submessages;
msgs_to_rerender_or_add_to_narrow.push(client_message); msgs_to_rerender_or_add_to_narrow.push(client_message);
waiting_for_ack.delete(local_id); echo_state.remove_message_from_waiting_for_ack(local_id);
} }
if (msgs_to_rerender_or_add_to_narrow.length > 0) { if (msgs_to_rerender_or_add_to_narrow.length > 0) {
@ -547,11 +545,6 @@ export function process_from_server(messages: ServerMessage[]): ServerMessage[]
return non_echo_messages; return non_echo_messages;
} }
export function _patch_waiting_for_ack(data: Map<string, Message>): void {
// Only for testing
waiting_for_ack = data;
}
export function message_send_error(message_id: number, error_response: string): void { export function message_send_error(message_id: number, error_response: string): void {
// Error sending message, show inline // Error sending message, show inline
const message = message_store.get(message_id)!; const message = message_store.get(message_id)!;
@ -616,7 +609,7 @@ export function initialize({
const local_id = rows.local_echo_id($row); const local_id = rows.local_echo_id($row);
// Message should be waiting for ack and only have a local id, // Message should be waiting for ack and only have a local id,
// otherwise send would not have failed // otherwise send would not have failed
const message = waiting_for_ack.get(local_id); const message = echo_state.get_message_waiting_for_ack(local_id);
if (message === undefined) { if (message === undefined) {
blueslip.warn( blueslip.warn(
"Got resend or retry on failure request but did not find message in ack list " + "Got resend or retry on failure request but did not find message in ack list " +

33
web/src/echo_state.ts Normal file
View File

@ -0,0 +1,33 @@
import type {Message} from "./message_store";
const waiting_for_id = new Map<string, Message>();
let waiting_for_ack = new Map<string, Message>();
export function set_message_waiting_for_id(local_id: string, message: Message): void {
waiting_for_id.set(local_id, message);
}
export function set_message_waiting_for_ack(local_id: string, message: Message): void {
waiting_for_ack.set(local_id, message);
}
export function get_message_waiting_for_id(local_id: string): Message | undefined {
return waiting_for_id.get(local_id);
}
export function get_message_waiting_for_ack(local_id: string): Message | undefined {
return waiting_for_ack.get(local_id);
}
export function remove_message_from_waiting_for_id(local_id: string): void {
waiting_for_id.delete(local_id);
}
export function remove_message_from_waiting_for_ack(local_id: string): void {
waiting_for_ack.delete(local_id);
}
export function _patch_waiting_for_ack(data: Map<string, Message>): void {
// Only for testing
waiting_for_ack = data;
}

View File

@ -67,6 +67,7 @@ const home_msg_list = {
message_lists.all_rendered_message_lists = () => [home_msg_list, message_lists.current]; message_lists.all_rendered_message_lists = () => [home_msg_list, message_lists.current];
const echo = zrequire("echo"); const echo = zrequire("echo");
const echo_state = zrequire("echo_state");
const people = zrequire("people"); const people = zrequire("people");
const stream_data = zrequire("stream_data"); const stream_data = zrequire("stream_data");
@ -84,7 +85,7 @@ run_test("process_from_server for un-echoed messages", () => {
local_id: "100.1", local_id: "100.1",
}, },
]; ];
echo._patch_waiting_for_ack(waiting_for_ack); echo_state._patch_waiting_for_ack(waiting_for_ack);
const non_echo_messages = echo.process_from_server(server_messages); const non_echo_messages = echo.process_from_server(server_messages);
assert.deepEqual(non_echo_messages, server_messages); assert.deepEqual(non_echo_messages, server_messages);
}); });
@ -122,7 +123,7 @@ run_test("process_from_server for differently rendered messages", ({override}) =
topic_links: new_value, topic_links: new_value,
}, },
]; ];
echo._patch_waiting_for_ack(waiting_for_ack); echo_state._patch_waiting_for_ack(waiting_for_ack);
disparities = []; disparities = [];
const non_echo_messages = echo.process_from_server(server_messages); const non_echo_messages = echo.process_from_server(server_messages);
assert.deepEqual(non_echo_messages, []); assert.deepEqual(non_echo_messages, []);
@ -171,7 +172,7 @@ run_test("process_from_server for messages to add to narrow", ({override}) => {
topic_links: new_value, topic_links: new_value,
}, },
]; ];
echo._patch_waiting_for_ack(waiting_for_ack); echo_state._patch_waiting_for_ack(waiting_for_ack);
const non_echo_messages = echo.process_from_server(server_messages); const non_echo_messages = echo.process_from_server(server_messages);
assert.deepEqual(non_echo_messages, []); assert.deepEqual(non_echo_messages, []);
assert.deepEqual(messages_to_add_to_narrow, [ assert.deepEqual(messages_to_add_to_narrow, [