From 8320d9b5cb78d682760868845c4928e1962d3db2 Mon Sep 17 00:00:00 2001
From: Kasalehlia <kasalehlia@clonejo.de>
Date: Tue, 17 Nov 2015 19:42:49 +0100
Subject: [PATCH] bus module now properly initializes

---
 modules/bus.js            | 33 ++++++++++++++++++++++-----------
 public/modules/bus/bus.js |  7 +++++--
 2 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/modules/bus.js b/modules/bus.js
index 64060ff..da46594 100644
--- a/modules/bus.js
+++ b/modules/bus.js
@@ -10,7 +10,7 @@ var STOPS = ['Ludwigstraße','Hamburger Straße'];
 var ENTRIES = 6;
 
 /// VARS
-var CACHED = null;
+var CACHED = [];
 var TEMPLATES = {};
 fs.readFile('modules/bus/outer.mustache', 'utf-8', function (err, data) {
     TEMPLATES.outer = data;
@@ -68,8 +68,8 @@ function getData(stop, count, cb) {
     });
 }
 
-function update(io) {
-    var done = 0;
+function update(io, allStopsDoneCb) {
+    var done = [];
     var context = [];
     var innerGetData = function (stop, i) {
         var ns = normalizeStop(stop);
@@ -77,6 +77,12 @@ function update(io) {
             try {
                 context[i] = {stop: stop, normalizedStop: ns, deps: deps};
                 io.emit('bus.'+ns, Mustache.render(TEMPLATES.inner, context[i]));
+                if (done.indexOf(stop) === -1) {
+                    done.push(stop);
+                    if (done.length === STOPS.length) {
+                        allStopsDoneCb();
+                    }
+                }
                 CACHED = context;
             } catch (e) {console.log(e);}
             // calculate when to update next
@@ -91,7 +97,7 @@ function update(io) {
         });
     }
     STOPS.forEach(function (stop, i) {
-        innerGetData(stop,i);
+        innerGetData(stop, i);
     });
 }
 
@@ -100,12 +106,17 @@ function normalizeStop(stop) {
 }
 
 module.exports = function (io) {
-    update(io);
-    io.on('connect', function (sock) {
-        sock.emit('bus', Mustache.render(TEMPLATES.outer, CACHED));
-        for (var i in CACHED) {
-            sock.emit('bus.'+CACHED[i].normalizedStop,
-                Mustache.render(TEMPLATES.inner, CACHED[i]));
-        }
+    update(io, function () {
+        var pushToClient = function (sock) {
+            sock.emit('bus', Mustache.render(TEMPLATES.outer, CACHED));
+            setTimeout(function () {
+                for (var i in CACHED) {
+                    sock.emit('bus.'+CACHED[i].normalizedStop,
+                        Mustache.render(TEMPLATES.inner, CACHED[i]));
+                }
+            }, 3000); //wait a second to let the client process the outlets
+        };
+        io.on('connect', pushToClient);
+        pushToClient(io);
     });
 }
diff --git a/public/modules/bus/bus.js b/public/modules/bus/bus.js
index e09e299..19f386a 100644
--- a/public/modules/bus/bus.js
+++ b/public/modules/bus/bus.js
@@ -6,7 +6,7 @@
         if (minute != lastMinute) {
             lastMinute = minute;
             var hour = d.getHours();
-            $('#bus .time').each(function () {
+            var found = $('#bus .time').each(function () {
                 var depart = $(this).children('span:first').text().split(':');
                 depart = depart.map(function (a) {return parseInt(a);});
                 if (depart[0] < hour) {
@@ -16,7 +16,10 @@
                 var text = diff < 0 ? 'now' : diff+' min'
                 var color = diff <= 5 ? '#dc322f' : '';
                 $(this).find('.until').text(text).attr('color', color);
-            });
+            }).length;
+            if (found === 0) {
+                lastMinute = null;
+            }
         }
     }, 1000);
 })();
-- 
GitLab