diff --git a/lib/node-social-counter.js b/lib/node-social-counter.js index c7aa891..b37ab36 100644 --- a/lib/node-social-counter.js +++ b/lib/node-social-counter.js @@ -42,14 +42,15 @@ var key = 'node_'+nid; - // add to queue - requestQueue[key] = {}; - if(typeof requestsInProgress[key] === 'undefined') { requestsInProgress[key] = {}; } - if(requestQueue[key] !== 'undefined') { + if(typeof requestQueue[key] === 'undefined') { + // add to queue + requestQueue[key] = {}; + } + if(typeof requestQueue[key] !== 'undefined') { // Visits getVisitsCount(key, target_url); // Facebook @@ -59,14 +60,11 @@ // Google+ getGoogleCount(key, target_url); } - - }; - getFacebookCount = function(key, target_url){ - if(typeof requestsInProgress[key].facebook === 'undefined'){ - + if(typeof requestsInProgress[key].facebook === 'undefined'){ + requestsInProgress[key].facebook = Math.round(new Date().getTime() / 1000); var req = request.get( @@ -157,15 +155,18 @@ if(typeof requestQueue[key].fb_count === 'number'){ store[key].fb_count = requestQueue[key].fb_count; - delete requestQueue[key].fb_count; - } + delete requestQueue[key].fb_count; + } + if(typeof requestQueue[key].tw_count === 'number'){ store[key].tw_count = requestQueue[key].tw_count; delete requestQueue[key].tw_count; + } + if(typeof requestQueue[key].g_count === 'number'){ store[key].g_count = requestQueue[key].g_count; - delete requestQueue[key].g_count; + delete requestQueue[key].g_count; } store[key].visits = visits_counter[key]; @@ -175,49 +176,60 @@ typeof requestQueue[key].tw_count === 'undefined' && typeof requestQueue[key].g_count === 'undefined' ){ delete requestQueue[key]; // remove from queue only when each count is removed - } + } } + }; - // clear expired keys + return store; + }; + + clearExpiredKeys = function(){ nowTimestamp = Math.round(new Date().getTime() / 1000); - for (var key in requestsInProgress){ - if(requestsInProgress[key].hasOwnProperty('facebook')){ - expired = nowTimestamp - requestsInProgress[key].facebook > config.recalc_after; - if(expired){ - //console.log(key+': expired facebook ... may recalculate'); - delete requestsInProgress[key].facebook; // remove from queue + for (var key in requestsInProgress){ + if (requestsInProgress.hasOwnProperty(key)) { + + if(typeof requestsInProgress[key].facebook === 'number'){ + //console.log('FB ETA: ' + (nowTimestamp - requestsInProgress[key].facebook)); + expired = nowTimestamp - requestsInProgress[key].facebook > config.recalc_after; + if(expired){ + //console.log(key+': expired facebook ... may recalculate'); + delete requestsInProgress[key].facebook; // remove from queue + } } - } - if(requestsInProgress[key].hasOwnProperty('twitter')){ - expired = nowTimestamp - requestsInProgress[key].twitter > config.recalc_after; - if(expired){ - //console.log(key+': expired twitter ... may recalculate'); - delete requestsInProgress[key].twitter; // remove from queue + + if(typeof requestsInProgress[key].twitter === 'number'){ + //console.log('TW ETA: ' + (nowTimestamp - requestsInProgress[key].twitter)); + expired = nowTimestamp - requestsInProgress[key].twitter > config.recalc_after; + if(expired){ + //console.log(key+': expired twitter ... may recalculate'); + delete requestsInProgress[key].twitter; // remove from queue + } } - } - if(requestsInProgress[key].hasOwnProperty('google')){ - expired = nowTimestamp - requestsInProgress[key].google > config.recalc_after; - if(expired){ - //console.log(key+': expired google ... may recalculate'); - delete requestsInProgress[key].google; // remove from queue + if(typeof requestsInProgress[key].google === 'number'){ + //console.log('GO ETA: ' + (nowTimestamp - requestsInProgress[key].google)); + expired = nowTimestamp - requestsInProgress[key].google > config.recalc_after; + if(expired){ + //console.log(key+': expired google ... may recalculate'); + delete requestsInProgress[key].google; // remove from queue + } } - } - if(typeof requestsInProgress[key].facebook === 'undefined' && - typeof requestsInProgress[key].twitter === 'undefined' && - typeof requestsInProgress[key].google === 'undefined' ){ - delete requestsInProgress[key]; // remove from queue only when each count is removed - } - } + if(typeof requestsInProgress[key].facebook === 'undefined' && + typeof requestsInProgress[key].twitter === 'undefined' && + typeof requestsInProgress[key].google === 'undefined' ){ + delete requestsInProgress[key]; // remove from queue only when each count is removed + } - return store; + } + } }; serialize = function() { var data; getCompletedKeys(); + clearExpiredKeys(); if(JSON.stringify(store) != '{}'){ data = { json: JSON.stringify(store)