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'];
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);
});
}
......@@ -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);
})();
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