Skip to content
Snippets Groups Projects
Commit 8320d9b5 authored by Kasalehlia's avatar Kasalehlia
Browse files

bus module now properly initializes

parent 2971c148
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,7 @@ var STOPS = ['Ludwigstraße','Hamburger Straße']; ...@@ -10,7 +10,7 @@ var STOPS = ['Ludwigstraße','Hamburger Straße'];
var ENTRIES = 6; var ENTRIES = 6;
/// VARS /// VARS
var CACHED = null; var CACHED = [];
var TEMPLATES = {}; var TEMPLATES = {};
fs.readFile('modules/bus/outer.mustache', 'utf-8', function (err, data) { fs.readFile('modules/bus/outer.mustache', 'utf-8', function (err, data) {
TEMPLATES.outer = data; TEMPLATES.outer = data;
...@@ -68,8 +68,8 @@ function getData(stop, count, cb) { ...@@ -68,8 +68,8 @@ function getData(stop, count, cb) {
}); });
} }
function update(io) { function update(io, allStopsDoneCb) {
var done = 0; var done = [];
var context = []; var context = [];
var innerGetData = function (stop, i) { var innerGetData = function (stop, i) {
var ns = normalizeStop(stop); var ns = normalizeStop(stop);
...@@ -77,6 +77,12 @@ function update(io) { ...@@ -77,6 +77,12 @@ function update(io) {
try { try {
context[i] = {stop: stop, normalizedStop: ns, deps: deps}; context[i] = {stop: stop, normalizedStop: ns, deps: deps};
io.emit('bus.'+ns, Mustache.render(TEMPLATES.inner, context[i])); 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; CACHED = context;
} catch (e) {console.log(e);} } catch (e) {console.log(e);}
// calculate when to update next // calculate when to update next
...@@ -91,7 +97,7 @@ function update(io) { ...@@ -91,7 +97,7 @@ function update(io) {
}); });
} }
STOPS.forEach(function (stop, i) { STOPS.forEach(function (stop, i) {
innerGetData(stop,i); innerGetData(stop, i);
}); });
} }
...@@ -100,12 +106,17 @@ function normalizeStop(stop) { ...@@ -100,12 +106,17 @@ function normalizeStop(stop) {
} }
module.exports = function (io) { module.exports = function (io) {
update(io); update(io, function () {
io.on('connect', function (sock) { var pushToClient = function (sock) {
sock.emit('bus', Mustache.render(TEMPLATES.outer, CACHED)); sock.emit('bus', Mustache.render(TEMPLATES.outer, CACHED));
for (var i in CACHED) { setTimeout(function () {
sock.emit('bus.'+CACHED[i].normalizedStop, for (var i in CACHED) {
Mustache.render(TEMPLATES.inner, CACHED[i])); 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);
}); });
} }
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
if (minute != lastMinute) { if (minute != lastMinute) {
lastMinute = minute; lastMinute = minute;
var hour = d.getHours(); var hour = d.getHours();
$('#bus .time').each(function () { var found = $('#bus .time').each(function () {
var depart = $(this).children('span:first').text().split(':'); var depart = $(this).children('span:first').text().split(':');
depart = depart.map(function (a) {return parseInt(a);}); depart = depart.map(function (a) {return parseInt(a);});
if (depart[0] < hour) { if (depart[0] < hour) {
...@@ -16,7 +16,10 @@ ...@@ -16,7 +16,10 @@
var text = diff < 0 ? 'now' : diff+' min' var text = diff < 0 ? 'now' : diff+' min'
var color = diff <= 5 ? '#dc322f' : ''; var color = diff <= 5 ? '#dc322f' : '';
$(this).find('.until').text(text).attr('color', color); $(this).find('.until').text(text).attr('color', color);
}); }).length;
if (found === 0) {
lastMinute = null;
}
} }
}, 1000); }, 1000);
})(); })();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment