Skip to content

Commit

Permalink
fix undefined errors;
Browse files Browse the repository at this point in the history
fix recurrency misscount
  • Loading branch information
Leandro Almeida authored and Leandro Almeida committed Aug 5, 2014
1 parent 48fa677 commit 2df511b
Showing 1 changed file with 52 additions and 40 deletions.
92 changes: 52 additions & 40 deletions lib/node-social-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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];
Expand All @@ -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)
Expand Down

0 comments on commit 2df511b

Please sign in to comment.