diff --git a/modules/weather.js b/modules/weather.js index 1c4e71104cc9ed3d53cbd9478cc03a44ca2426ba..e5d9a8b4fd7dbc88216163fdc77f74169604777d 100644 --- a/modules/weather.js +++ b/modules/weather.js @@ -64,22 +64,29 @@ function degToDirection(deg) { return dir; } -//fetchCurrent(CITYID, console.log); -//fetchForecast(CITYID, 3, console.log); - module.exports = function (io) { var context = {}; - var update = function () { + var update = function (firstUpdateCb) { + var then = function () { + if (context.current && context.forecast) { + firstUpdateCb(); + } + }; fetchCurrent(CITYID, function (current) { context.current = current; + then(); }); fetchForecast(CITYID, 6, function (forecast) { context.forecast = forecast; + then(); }); }; - update(); - setInterval(update, 10*60*1000); - io.on('connect', function (sock) { - sock.emit('weather', Mustache.render(TEMPLATE, context)); + update(function () { + var pushToClients = function (sock) { + sock.emit('weather', Mustache.render(TEMPLATE, context)); + }; + io.on('connect', pushToClients); + pushToClients(io); }); + setInterval(update, 10*60*1000); }