SVGHMI: JS refactoring continued : "dispatch_value_to_widget" becomes widget class member "new_hmi_value" (was also broken by previous relativeness commit) svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Wed, 05 Aug 2020 18:53:56 +0200
branchsvghmi
changeset 3006 bbffdefd2eed
parent 3005 ff9ae4f4e3be
child 3007 360300a8b995
SVGHMI: JS refactoring continued : "dispatch_value_to_widget" becomes widget class member "new_hmi_value" (was also broken by previous relativeness commit)
svghmi/svghmi.js
svghmi/widgets_common.ysl2
--- a/svghmi/svghmi.js	Wed Aug 05 18:49:29 2020 +0200
+++ b/svghmi/svghmi.js	Wed Aug 05 18:53:56 2020 +0200
@@ -4,25 +4,6 @@
 var updates = {};
 var need_cache_apply = []; 
 
-function dispatch_value_to_widget(widget, index, value, oldval) {
-    try {
-        let idx = widget.offset ? index - widget.offset : index;
-        let idxidx = widget.indexes.indexOf(idx);
-        let d = widget.dispatch;
-        if(typeof(d) == "function" && idxidx == 0){
-            d.call(widget, value, oldval);
-        }
-        else if(typeof(d) == "object" && d.length >= idxidx){
-            d[idxidx].call(widget, value, oldval);
-        }
-        /* else dispatch_0, ..., dispatch_n ? */
-        /*else {
-            throw new Error("Dunno how to dispatch to widget at index = " + index);
-        }*/
-    } catch(err) {
-        console.log(err);
-    }
-}
 
 function dispatch_value(index, value) {
     let widgets = subscribers[index];
@@ -32,7 +13,7 @@
 
     if(widgets.size > 0) {
         for(let widget of widgets){
-            dispatch_value_to_widget(widget, index, value, oldval);
+            widget.new_hmi_value(index, value, oldval);
         }
     }
 };
@@ -190,7 +171,7 @@
     /* type: "Watchdog", */
     frequency: 1,
     indexes: [heartbeat_index],
-    dispatch: function(value) {
+    new_hmi_value: function(index, value, oldval) {
         apply_hmi_value(heartbeat_index, value+1);
     }
 });
--- a/svghmi/widgets_common.ysl2	Wed Aug 05 18:49:29 2020 +0200
+++ b/svghmi/widgets_common.ysl2	Wed Aug 05 18:53:56 2020 +0200
@@ -109,7 +109,7 @@
                 let realindex = index+this.offset;
                 let cached_val = cache[realindex];
                 if(cached_val != undefined)
-                    dispatch_value_to_widget(this, realindex, cached_val, cached_val);
+                    this.new_hmi_value(realindex, cached_val, cached_val);
             }
         }
 
@@ -124,6 +124,34 @@
         apply_hmi_value(index, new_val) {
             return apply_hmi_value(this.get_idx(0), new_val);
         }
+
+        new_hmi_value(index, value, oldval) {
+            try {
+                // TODO avoid searching, store index at sub()
+                for(let i = 0; i < this.indexes.length; i++) {
+                    let refindex = this.indexes[i];
+                    if(this.relativeness[i])
+                        refindex += this.offset;
+
+                    if(index == refindex) {
+                        let d = this.dispatch;
+                        if(typeof(d) == "function"){
+                            d.call(this, value, oldval, i);
+                        }
+                        else if(typeof(d) == "object"){
+                            d[i].call(this, value, oldval);
+                        }
+                        /* else dispatch_0, ..., dispatch_n ? */
+                        /*else {
+                            throw new Error("Dunno how to dispatch to widget at index = " + index);
+                        }*/
+                        break;
+                    }
+                }
+            } catch(err) {
+                console.log(err);
+            }
+        }
     }
     ||
 }