diff options
author | Christopher Baines <cb15g11@soton.ac.uk> | 2015-03-22 09:48:08 +0000 |
---|---|---|
committer | Christopher Baines <cb15g11@soton.ac.uk> | 2015-03-22 09:48:08 +0000 |
commit | 903b508fd12fa6bec08963f9c86e066149afb8c6 (patch) | |
tree | 7c12883c86a11b5838d511f09772aa951b3ff4ef | |
parent | a6a9f0263e1d28b87df726a35621e16ba2607527 (diff) | |
download | uos-live.js-903b508fd12fa6bec08963f9c86e066149afb8c6.tar uos-live.js-903b508fd12fa6bec08963f9c86e066149afb8c6.tar.gz |
The request is still made when you subscribe, but the callback will be called
instantly if the data is cached.
-rw-r--r-- | uos-live.js | 10 |
1 files changed, 10 insertions, 0 deletions
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); }); |