From 903b508fd12fa6bec08963f9c86e066149afb8c6 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Sun, 22 Mar 2015 09:48:08 +0000 Subject: Cache busStop data The request is still made when you subscribe, but the callback will be called instantly if the data is cached. --- uos-live.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/uos-live.js b/uos-live.js index 629fd58..c89c6ae 100644 --- a/uos-live.js +++ b/uos-live.js @@ -3,6 +3,7 @@ window.UoSLive = {}; var busStopRequests = {}; + var busStopCache = {}; UoSLive.getBusStopURL = function(busStop) { var base = "http://bus.southampton.ac.uk/bus-stop/"; @@ -14,6 +15,12 @@ UoSLive.subscribeToBusStop = function(busStop, callback) { + if (busStop in busStopCache) { + var result = busStopCache[busStop]; + if (result.timeFetched > (Date.now() - 60000)) { + callback(result); + } + } var url = UoSLive.getBusStopURL(busStop); @@ -24,6 +31,9 @@ req.recurring = true; req.on('finished', function(code, result, headers) { + result.timeFetched = Date.now(); + busStopCache[busStop] = result; + callback(result); }); -- cgit v1.2.3