SVGHMI: refactor an factorize widget dispatch() method calling
authorEdouard Tisserant
Fri, 09 Sep 2022 10:06:02 +0200
changeset 3600 c9ecf7d50888
parent 3599 0d7c41546854
child 3601 26a2808038fd
SVGHMI: refactor an factorize widget dispatch() method calling
svghmi/widgets_common.ysl2
--- a/svghmi/widgets_common.ysl2	Fri Sep 09 09:45:27 2022 +0200
+++ b/svghmi/widgets_common.ysl2	Fri Sep 09 10:06:02 2022 +0200
@@ -61,7 +61,7 @@
 in xsl decl widget_defs(%name, match="widget[@type='%name']", mode="widget_defs") alias template {
     param "hmi_element";
     // all widget potentially has a "disabled" labeled element
-    const "disability" optional_labels("disabled");
+    const "disability" optional_labels("/disabled");
     value "$disability";
     const "has_disability","string-length($disability)>0";
     content;
@@ -372,7 +372,7 @@
                 if(realindex == undefined) continue;
                 let cached_val = cache[realindex];
                 if(cached_val != undefined)
-                    this._dispatch(cached_val, cached_val, index);
+                    this.feed_data_for_dispatch(cached_val, cached_val, index);
             }
         }
 
@@ -463,7 +463,7 @@
                 if(refindex == undefined) continue;
 
                 if(index == refindex) {
-                    this._dispatch(value, oldval, i);
+                    this.feed_data_for_dispatch(value, oldval, i);
                     break;
                 }
             }
@@ -473,8 +473,7 @@
             this.deafen[index] = undefined;
             let [new_val, old_val] = this.incoming[index];
             this.incoming[index] = undefined;
-            if(this.dispatch)
-                this.dispatch(new_val, old_val, index);
+            this.do_dispatch(new_val, old_val, index);
         }
 
         enable(enabled){
@@ -516,26 +515,15 @@
             }
         }
 
-        _dispatch(value, oldval, varnum) {
-            let dispatch = this.dispatch;
-            let has_dispatch = dispatch != undefined;
-            if(has_dispatch || this.enable_expr){
+        feed_data_for_dispatch(value, oldval, varnum) {
+            if(this.dispatch || this.enable_expr){
                 if(this.deafen[varnum] == undefined){
                     let now = Date.now();
                     let min_interval = 1000/this.frequency;
                     let lastdispatch = this.lastdispatch[varnum];
                     if(lastdispatch == undefined || now > lastdispatch + min_interval){
                         this.lastdispatch[varnum] = now;
-                        if(has_dispatch) try {
-                            dispatch.call(this, value, oldval, varnum);
-                        } catch(err) {
-                            console.log(err);
-                        }
-                        if(this.enable_expr) try {
-                            this.compute_enable(value, oldval, varnum);
-                        } catch(err) {
-                            console.log(err);
-                        }
+                        this.do_dispatch(value, oldval, varnum)
                     }
                     else {
                         let elapsed = now - lastdispatch;
@@ -549,6 +537,19 @@
             }
         }
 
+        do_dispatch(value, oldval, varnum) {
+            if(this.dispatch) try {
+                this.dispatch(value, oldval, varnum);
+            } catch(err) {
+                console.log(err);
+            }
+            if(this.enable_expr) try {
+                this.compute_enable(value, oldval, varnum);
+            } catch(err) {
+                console.log(err);
+            }
+        }
+
         _animate(){
             if(this.enable_expr)
                 this.animate_enable();