SVGHMI: more attempts to prevent losing memory in JS closure svghmi
authorEdouard Tisserant
Wed, 02 Dec 2020 14:30:58 +0100
branchsvghmi
changeset 3081 9e55061c87fa
parent 3080 e5fa1f49f0b9
child 3082 20a5eb6a02e6
SVGHMI: more attempts to prevent losing memory in JS closure
svghmi/widget_jsontable.ysl2
svghmi/widget_jump.ysl2
--- a/svghmi/widget_jsontable.ysl2	Fri Nov 27 18:06:34 2020 +0100
+++ b/svghmi/widget_jsontable.ysl2	Wed Dec 02 14:30:58 2020 +0100
@@ -42,8 +42,9 @@
             this.do_http_request();
         }
         make_on_click(...options){
+            let that = this;
             return function(evt){
-                this.do_http_request(...options);
+                that.do_http_request(...options);
             }
         }
         // on_click(evt, ...options) {
@@ -117,7 +118,7 @@
 
     choose {
         when "count($from_list) > 0" {
-            |         id("«@id»").setAttribute("xlink:href", 
+            |         id("«@id»").setAttribute("xlink:href",
             // obtain new target id from HMI:List widget
             |             "#"+hmi_widgets["«$from_list/@id»"].items[«$expressions/expression[1]/@content»]);
         }
@@ -185,8 +186,7 @@
 
     const "elt",".";
     foreach "$new_expressions/expression[position() > 1][starts-with(@name,'onClick')]"
-    //|         id("«$elt/@id»").setAttribute("onclick", "hmi_widgets['"+this.element_id+"'].on_click(evt, '«@name»', '"+«@content»+"')");
-    |         id("«$elt/@id»").onclick = this.make_on_click('«@name»', '"+«@content»+"');
+    |         id("«$elt/@id»").onclick = this.make_on_click('«@name»', «@content»);
 
     apply ".", mode="json_table_elt_render"
         with "expressions", "$new_expressions";
@@ -217,7 +217,6 @@
     }
 
     // revert hiding in case it did happen before
-    // |           id("«@id»").setAttribute("style", "«@style»");
     |           id("«@id»").style = "«@style»";
 
     apply "*", mode="json_table_render_except_comments" {
@@ -225,7 +224,6 @@
         with "widget_elts", "$widget_elts";
     }
     |         } catch(err) {
-    // |           id("«$gid»").setAttribute("style", "display:none");
     |           id("«$gid»").style = "display:none";
     |         }
 }
--- a/svghmi/widget_jump.ysl2	Fri Nov 27 18:06:34 2020 +0100
+++ b/svghmi/widget_jump.ysl2	Wed Dec 02 14:30:58 2020 +0100
@@ -57,11 +57,6 @@
     }
     |     },
     }
-    |     on_click: function(evt) {
-    |         const index = this.indexes.length > 0 ? this.indexes[0] + this.offset : undefined;
-    |         const name = this.args[0];
-    |         switch_page(name, index);
-    |     },
     if "$have_activity" {
     |     notify_page_change: function(page_name, index){
     |         const ref_index = this.indexes.length > 0 ? this.indexes[0] + this.offset : undefined;
@@ -70,13 +65,21 @@
     |         this.update();
     |     },
     }
+    |     make_on_click(){
+    |         let that = this;
+    |         const name = this.args[0];
+    |         return function(evt){
+    |             const index = that.indexes.length > 0 ? that.indexes[0] + that.offset : undefined;
+    |             switch_page(name, index);
+    |         }
+    |     },
     |     init: function() {
     /* registering event this way does not "click" through svg:use 
     |     this.element.onclick = evt => switch_page(this.args[0]);
     event must be registered by adding attribute to element instead
     TODO : generalize mouse event handling by global event capture + getElementsAtPoint()
     */
-    |         this.element.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_click(evt)");
+    |         this.element.onclick = this.make_on_click();
     if "$have_activity" {
     |         this.active_elt_style = this.active_elt.getAttribute("style");
     |         this.inactive_elt_style = this.inactive_elt.getAttribute("style");