Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
poll widget: Eliminate handler nesting.
Steve Howelld7deec72048c
diff --git a/web/src/poll_data.ts b/web/src/poll_data.tsindex 8d0dfa0dde5..67b25908108 100644--- a/web/src/poll_data.ts+++ b/web/src/poll_data.ts@@ -64,23 +64,6 @@ type Vote = {type: string; key: string; vote: number};/* ---------------------- */-export type InboundData = unknown;--type PollHandle = {- new_option: {- outbound: (option: string) => NewOption;- inbound: (sender_id: number | string, data: InboundData) => void;- };- question: {- outbound: (question: string) => Question | undefined;- inbound: (sender_id: number, data: InboundData) => void;- };- vote: {- outbound: (key: string) => Vote;- inbound: (sender_id: number, data: InboundData) => void;- };-};-export const poll_widget_extra_data_schema = z.object({question: z.optional(z.string()),options: z.optional(z.array(z.string())),@@ -109,7 +92,6 @@ export class PollData {input_mode: boolean;comma_separated_names: (user_ids: number[]) => string;report_error_function: (error_message: string) => void;- handle: PollHandle;constructor({message_sender_id,@@ -132,34 +114,8 @@ export class PollData {this.set_question(question);}- this.handle = {- new_option: {- outbound: (option): NewOption => this.new_option_event(option),-- inbound: (sender_id, data) => {- this.handle_new_option_event(sender_id, data);- },- },-- question: {- outbound: (question: string): Question | undefined => this.question_event(question),-- inbound: (sender_id, data) => {- this.handle_question_event(sender_id, data);- },- },-- vote: {- outbound: (key: string): Vote => this.vote_event(key),-- inbound: (sender_id, data) => {- this.handle_vote_event(sender_id, data);- },- },- };-for (const [i, option] of options.entries()) {- this.handle.new_option.inbound("canned", {+ this.handle_new_option_event("canned", {idx: i,option,type: "new_option",@@ -328,7 +284,7 @@ export class PollData {return widget_data;}- handle_event(sender_id: number, data: InboundData): void {+ handle_event(sender_id: number, data: unknown): void {assert(typeof data === "object" &&data !== null &&@@ -336,10 +292,22 @@ export class PollData {typeof data.type === "string",);const type = data.type;- if (type === "new_option" || type === "question" || type === "vote") {- this.handle[type].inbound(sender_id, data);- } else {- this.report_error_function(`poll widget: unknown inbound type: ${type}`);+ switch (type) {+ case "new_option": {+ this.handle_new_option_event(sender_id, data);+ break;+ }+ case "question": {+ this.handle_question_event(sender_id, data);+ break;+ }+ case "vote": {+ this.handle_vote_event(sender_id, data);+ break;+ }+ default: {+ this.report_error_function(`poll widget: unknown inbound type: ${type}`);+ }}}diff --git a/web/src/poll_widget.ts b/web/src/poll_widget.tsindex 461f506c283..fcfa6b20647 100644--- a/web/src/poll_widget.ts+++ b/web/src/poll_widget.ts@@ -106,7 +106,7 @@ export function activate({}// Broadcast the new question to our peers.- const data = poll_data.handle.question.outbound(new_question);+ const data = poll_data.question_event(new_question);if (data) {callback(data);}@@ -127,12 +127,12 @@ export function activate({$poll_option_input.val("").trigger("focus");- const data = poll_data.handle.new_option.outbound(option);+ const data = poll_data.new_option_event(option);callback(data);}function submit_vote(key: string): void {- const data = poll_data.handle.vote.outbound(key);+ const data = poll_data.vote_event(key);callback(data);}diff --git a/web/tests/poll_widget.test.cjs b/web/tests/poll_widget.test.cjsindex 8efc6a725ee..ffd391650d3 100644--- a/web/tests/poll_widget.test.cjs+++ b/web/tests/poll_widget.test.cjs@@ -145,7 +145,7 @@ run_test("PollData my question", () => {data_holder.handle_event(me.user_id, invalid_vote_event);data = data_holder.get_widget_data();- const option_outbound_event = data_holder.handle.new_option.outbound("new option");+ const option_outbound_event = data_holder.new_option_event("new option");assert.deepEqual(option_outbound_event, {type: "new_option",idx: 2,@@ -153,13 +153,13 @@ run_test("PollData my question", () => {});const new_question = "Any new plan?";- const question_outbound_event = data_holder.handle.question.outbound(new_question);+ const question_outbound_event = data_holder.question_event(new_question);assert.deepEqual(question_outbound_event, {type: "question",question: new_question,});- const vote_outbound_event = data_holder.handle.vote.outbound("99,1");+ const vote_outbound_event = data_holder.vote_event("99,1");assert.deepEqual(vote_outbound_event, {type: "vote", key: "99,1", vote: -1});vote_event = {