]> git.mar77i.info Git - chat/commitdiff
break up the ws_receive_msg functions
authormar77i <mar77i@protonmail.ch>
Sat, 19 Oct 2024 14:35:34 +0000 (16:35 +0200)
committermar77i <mar77i@protonmail.ch>
Sat, 19 Oct 2024 14:35:34 +0000 (16:35 +0200)
chat/static/chat/chat.js

index 1dacf6c5db988b3f2fd9f6951f9fbe52e66185cb..391ba39fe66115b1402122f70fd8ff5683ffe0e1 100644 (file)
@@ -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,
         }
     }
 
-    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 + "/",
                     bottom_callback();
                 }
             );
-        } else if (data.op === "UPDATE") {
+        },
+        UPDATE: function (data) {
+            var messages = _.dgEBCN0("messages");
             stick_to_bottom(messages);
             find_existing_msg(
                 data.obj.id,
                         "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);
             private_manager.items_reload();
         }
         if (current_channel !== null && current_channel.match_channel(data)) {
-            ws_receive_msg(data);
+            ws_receive_msg[data.op](data);
         }
     }