From 6eccab717234b5a37c36ada2fb8552fb13108e97 Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Thu, 9 Apr 2020 10:06:59 +0200
Subject: [PATCH] [fix] API: Add missing lib qrcode, as a workaround of issue
 #903

---
 www/api/index.html                   |  2 ++
 www/js/services/bma-services.js      |  7 +++----
 www/js/services/currency-services.js | 21 +++++++++++++++++++--
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/www/api/index.html b/www/api/index.html
index d3c3c2d2..e2d4fabf 100644
--- a/www/api/index.html
+++ b/www/api/index.html
@@ -39,6 +39,8 @@
 <script src="../lib/numeral/languages/es.js"></script>
 <script src="../lib/numeral/languages/it.js"></script>
 <script src="../js/vendor/numeral.eo.js"></script>
+<script src="../lib/qrcode-generator/js/qrcode.js"></script> <!-- FIXME remove this lib (see #903) -->
+
 <script src="../lib/socket.io-client/dist/socket.io.min.js"></script>
 <script src="../lib/underscore/underscore-min.js"></script>
 
diff --git a/www/js/services/bma-services.js b/www/js/services/bma-services.js
index b23484cd..e045aeb8 100644
--- a/www/js/services/bma-services.js
+++ b/www/js/services/bma-services.js
@@ -300,8 +300,8 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
     };
 
     that.ready = function() {
-      if (that.started) return $q.when(true);
-      return that._startPromise || that.start();
+      if (that.started) return $q.when(that.alive);
+      return (that._startPromise || that.start());
     };
 
     that.start = function() {
@@ -321,11 +321,10 @@ angular.module('cesium.bma.services', ['ngApi', 'cesium.http.services', 'cesium.
       }
 
       console.debug("[BMA] Starting {0} {ssl: {1})...".format(that.server, that.useSsl));
-
       var now = Date.now();
 
       that._startPromise = $q.all([
-          csSettings.ready,
+          csSettings.ready(),
           that.isAlive()
         ])
         .then(function(res) {
diff --git a/www/js/services/currency-services.js b/www/js/services/currency-services.js
index a65054d0..351953cf 100644
--- a/www/js/services/currency-services.js
+++ b/www/js/services/currency-services.js
@@ -209,7 +209,7 @@ angular.module('cesium.currency.services', ['ngApi', 'cesium.bma.services'])
 
   function ready() {
     if (started) return $q.when(data);
-    return startPromise || start();
+    return (startPromise || start());
   }
 
   function stop() {
@@ -224,13 +224,30 @@ angular.module('cesium.currency.services', ['ngApi', 'cesium.bma.services'])
   }
 
   function start() {
+    if (startPromise) return startPromise;
+    if (started) return $q.when(data);
+
+    if (!BMA.started) {
+      return BMA.ready()
+        .then(function(alive) {
+          if (alive) return start(); // Loop
+          return $timeout(start, 500); // Loop, after a delay, because BMA node seems to be not alive...
+        });
+    }
+
     console.debug('[currency] Starting...');
     var now = Date.now();
 
     startPromise = BMA.ready()
+      .then(function(started) {
+        if (started) return true;
+        return $timeout(function() {return start(true);}, 500);
+      })
 
       // Load data
-      .then(loadData)
+      .then(function(started) {
+        return loadData();
+      })
 
       // Emit ready event
       .then(function() {
-- 
GitLab