From cd6113d79a75f4e13974f28d42debe2a7fc0a5c9 Mon Sep 17 00:00:00 2001 From: mar77i Date: Sat, 19 Oct 2024 16:35:34 +0200 Subject: [PATCH] break up the ws_receive_msg functions --- chat/static/chat/chat.js | 41 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/chat/static/chat/chat.js b/chat/static/chat/chat.js index 1dacf6c..391ba39 100644 --- a/chat/static/chat/chat.js +++ b/chat/static/chat/chat.js @@ -1,19 +1,19 @@ (function () { var current_channel = null, private_manager = null, channel_manager = null; - var bottom_callback, no_bottom_callback; - bottom_callback = no_bottom_callback = function () { + var bottom_callback; + function no_bottom_callback() { bottom_callback = no_bottom_callback; }; + bottom_callback = no_bottom_callback; function xhr(method, url, callback, data) { - var cb = callback - ? function () { callback(JSON.parse(this.responseText)); } - : null; return _.xhr( method, url, - cb, + callback + ? function () { callback(JSON.parse(this.responseText)); } + : null, function (msg) { add_msg(null, "xhr_error", msg); }, function (msg) { add_msg(null, "xhr_abort", msg); }, data, @@ -302,10 +302,9 @@ } } - function ws_receive_msg(data) { - var messages = _.dgEBCN0("messages"); - if (data.op === "INSERT") { - stick_to_bottom(messages); + var ws_receive_msg = { + INSERT: function (data) { + stick_to_bottom(_.dgEBCN0("messages")); xhr( "get", current_channel.msg_url + data.obj.id + "/", @@ -314,7 +313,9 @@ bottom_callback(); } ); - } else if (data.op === "UPDATE") { + }, + UPDATE: function (data) { + var messages = _.dgEBCN0("messages"); stick_to_bottom(messages); find_existing_msg( data.obj.id, @@ -324,22 +325,22 @@ "get", current_channel.msg_url + get_data_id(tag).toString() + "/", function (msg) { - set_msg( - tag, msg[current_channel.msg_user_field], msg.text - ); + set_msg(tag, msg[current_channel.msg_user_field], msg.text); bottom_callback(); } ); }, ); - } else if (data.op === "DELETE") { + }, + DELETE: function (data) { find_existing_msg( data.obj.id, function (tag) { tag.parentElement.removeChild(tag); } ); - } else if (data.op === "TRUNCATE") { - _.clear_children(messages); - } - } + }, + TRUNCATE: function (data) { + _.clear_children(_.dgEBCN0("messages")); + }, + }; function ws_receive(msg) { var data = JSON.parse(msg.data); @@ -350,7 +351,7 @@ private_manager.items_reload(); } if (current_channel !== null && current_channel.match_channel(data)) { - ws_receive_msg(data); + ws_receive_msg[data.op](data); } } -- 2.51.0