SVGHMI: Switch from {object} to Map for "updates" global, for performance but also preventing wierd behaviour when iterating svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Fri, 19 Feb 2021 10:03:00 +0100
branchsvghmi
changeset 3152 c80a5a7198ea
parent 3151 8e5d383a58cb
child 3153 671283404554
SVGHMI: Switch from {object} to Map for "updates" global, for performance but also preventing wierd behaviour when iterating
svghmi/svghmi.js
svghmi/widgets_common.ysl2
--- a/svghmi/svghmi.js	Thu Feb 18 12:02:28 2021 +0100
+++ b/svghmi/svghmi.js	Fri Feb 19 10:03:00 2021 +0100
@@ -51,12 +51,10 @@
 
 // Apply updates recieved through ws.onmessage to subscribed widgets
 function apply_updates() {
-    for(let index in updates){
-        // serving as a key, index becomes a string
-        // -> pass Number(index) instead
-        dispatch_value(Number(index), updates[index]);
-        delete updates[index];
-    }
+    updates.forEach((value, index) => {
+        dispatch_value(index, value);
+    });
+    updates.clear();
 }
 
 // Called on requestAnimationFrame, modifies DOM
@@ -110,7 +108,7 @@
             if(iectype != undefined){
                 let dvgetter = dvgetters[iectype];
                 let [value, bytesize] = dvgetter(dv,i);
-                updates[index] = value;
+                updates.set(index, value);
                 i += bytesize;
             } else {
                 throw new Error("Unknown index "+index);
@@ -292,7 +290,7 @@
 
 function send_hmi_value(index, value) {
     if(index > last_remote_index){
-        updates[index] = value;
+        updates.set(index, value);
 
         if(persistent_indexes.has(index)){
             let varname = persistent_indexes.get(index);
--- a/svghmi/widgets_common.ysl2	Thu Feb 18 12:02:28 2021 +0100
+++ b/svghmi/widgets_common.ysl2	Fri Feb 19 10:03:00 2021 +0100
@@ -96,7 +96,7 @@
     ]);
     var persistent_indexes = new Map();
     var cache = hmitree_types.map(_ignored => undefined);
-    var updates = {};
+    var updates = new Map();
 
     function page_local_index(varname, pagename){
         let pagevars = hmi_locals[pagename];
@@ -116,7 +116,7 @@
         let defaultval = local_defaults[varname];
         if(defaultval != undefined) {
             cache[new_index] = defaultval; 
-            updates[new_index] = defaultval;
+            updates.set(new_index, defaultval);
             if(persistent_locals.has(varname))
                 persistent_indexes.set(new_index, varname);
         }