svghmi/svghmi.js
branchsvghmi
changeset 2864 36f78f6cfabd
parent 2860 b7650c6abeda
child 2865 545902730141
--- a/svghmi/svghmi.js	Mon Mar 09 13:43:34 2020 +0100
+++ b/svghmi/svghmi.js	Tue Mar 10 13:57:29 2020 +0100
@@ -2,7 +2,6 @@
 
 var cache = hmitree_types.map(_ignored => undefined);
 var updates = {};
-var page_switch = null;
 
 function dispatch_value_to_widget(widget, index, value, oldval) {
     try {
@@ -70,9 +69,8 @@
 // Do the page swith if any one pending
 // Called on requestAnimationFrame, modifies DOM
 function animate() {
-    if(page_switch != null){
-        do_switch_page(page_switch);
-        page_switch=null;
+    if(current_subscribed_page != current_visible_page){
+        switch_visible_page(current_subscribed_page);
     }
 
     for(let index in updates){
@@ -81,15 +79,14 @@
         dispatch_value(Number(index), updates[index]);
         delete updates[index];
     }
+    requestAnimationFrameID = null;
 }
 
 var requestAnimationFrameID = null;
 function requestHMIAnimation() {
-    if(requestAnimationFrameID != null){
-        window.cancelAnimationFrame(requestAnimationFrameID);
-        requestAnimationFrameID = null;
-    }
-    requestAnimationFrameID = window.requestAnimationFrame(animate);
+    if(requestAnimationFrameID == null){
+        requestAnimationFrameID = window.requestAnimationFrame(animate);
+    }
 }
 
 // Message reception handler
@@ -122,7 +119,7 @@
             }
         };
         // register for rendering on next frame, since there are updates
-        window.requestAnimationFrame(animate);
+        requestHMIAnimation();
     } catch(err) {
         // 1003 is for "Unsupported Data"
         // ws.close(1003, err.message);
@@ -247,7 +244,8 @@
     return new_val;
 }
 
-var current_page;
+var current_visible_page;
+var current_subscribed_page;
 
 function prepare_svg() {
     for(let eltid in detachable_elements){
@@ -257,12 +255,20 @@
 };
 
 function switch_page(page_name) {
-    page_switch = page_name;
-    window.requestAnimationFrame(animate);
-}
-
-function do_switch_page(page_name) {
-    let old_desc = page_desc[current_page];
+    if(current_subscribed_page != current_visible_page){
+        /* page switch already going */
+        /* TODO LOG ERROR */
+        return;
+    } else if(page_name == current_visible_page){
+        /* already in that page */
+        /* TODO LOG ERROR */
+        return;
+    }
+    switch_subscribed_page(page_name);
+};
+
+function switch_subscribed_page(page_name) {
+    let old_desc = page_desc[current_subscribed_page];
     let new_desc = page_desc[page_name];
 
     if(new_desc == undefined){
@@ -277,6 +283,27 @@
                 subscribers[index].delete(widget);
             }
         }
+    }
+    for(let widget of new_desc.widgets){
+        /* add widget's subsribers */
+        for(let index of widget.indexes){
+            subscribers[index].add(widget);
+        }
+    }
+
+    update_subscriptions();
+
+    current_subscribed_page = page_name;
+
+    requestHMIAnimation();
+}
+
+function switch_visible_page(page_name) {
+
+    let old_desc = page_desc[current_visible_page];
+    let new_desc = page_desc[page_name];
+
+    if(old_desc){
         for(let eltid in old_desc.required_detachables){
             if(!(eltid in new_desc.required_detachables)){
                 let [element, parent] = old_desc.required_detachables[eltid];
@@ -297,9 +324,7 @@
     }
 
     for(let widget of new_desc.widgets){
-        /* add widget's subsribers */
         for(let index of widget.indexes){
-            subscribers[index].add(widget);
             /* dispatch current cache in newly opened page widgets */
             let cached_val = cache[index];
             if(cached_val != undefined)
@@ -308,9 +333,7 @@
     }
 
     svg_root.setAttribute('viewBox',new_desc.bbox.join(" "));
-    current_page = page_name;
-
-    window.setTimeout(update_subscriptions,0);
+    current_visible_page = page_name;
 };