advancedrequest
Advanced tools
Comparing version 1.0.6 to 1.0.7
42
index.js
@@ -103,3 +103,3 @@ | ||
console.log("[!]", additionalMsg || 'Req fail!', | ||
console.log("[!]", additionalMsg || 'AdvancedRequest.fail call -', | ||
"Status:", this.responseStatusCode, "url:", this.opts.url, "name:", this.name, | ||
@@ -115,7 +115,12 @@ "tries left:", this.maxRetries - this.numTriesSoFar, "options:", this.reqOptions); | ||
getLastRunHash () { | ||
// Allow setting a lastRunHash on the class (probably subclass) or use the module one instead | ||
return this.lastRunHash || lastRunHash; | ||
} | ||
onFinish (result) { | ||
// Update last run time for this request if applicable. (in future add username?) | ||
if (lastRunHash[this.name]) { | ||
if (this.getLastRunHash()[this.name]) { | ||
// We use THIS moment, the END of the request as the marker. | ||
lastRunHash[this.name].lastReqTime = new Date(); | ||
this.getLastRunHash()[this.name].lastReqTime = new Date(); | ||
} | ||
@@ -144,10 +149,10 @@ | ||
// If this is a request with limitations | ||
if (lastRunHash[this.name]) { | ||
if (this.getLastRunHash()[this.name]) { | ||
// initialize now if not initialized yet | ||
if (!lastRunHash[this.name].lastReqTime) { | ||
lastRunHash[this.name].lastReqTime = new Date(); | ||
if (!this.getLastRunHash()[this.name].lastReqTime) { | ||
this.getLastRunHash()[this.name].lastReqTime = new Date(); | ||
} | ||
var millisecondsSinceLastRequest = (new Date() - lastRunHash[this.name].lastReqTime); | ||
return millisecondsSinceLastRequest < lastRunHash[this.name].requiredInterval; | ||
var millisecondsSinceLastRequest = (new Date() - this.getLastRunHash()[this.name].lastReqTime); | ||
return millisecondsSinceLastRequest < this.getLastRunHash()[this.name].requiredInterval; | ||
} | ||
@@ -159,7 +164,7 @@ return false; | ||
if (this.isSleepIntervalNecessary()) { | ||
var millisecondsSinceLastRequest = (new Date() - lastRunHash[this.name].lastReqTime); | ||
var timeToSleep = lastRunHash[this.name].requiredInterval - millisecondsSinceLastRequest; | ||
var millisecondsSinceLastRequest = (new Date() - this.getLastRunHash()[this.name].lastReqTime); | ||
var timeToSleep = this.getLastRunHash()[this.name].requiredInterval - millisecondsSinceLastRequest; | ||
console.log("[D] Sleeping", timeToSleep/(1000), "seconds now"); | ||
setTimeout(() => {this.sleepIntervalIfNecessary(callback);}, timeToSleep); | ||
setTimeout(() => { this.sleepIntervalIfNecessary(callback); }, timeToSleep); | ||
} else { | ||
@@ -175,3 +180,3 @@ return callback(); | ||
if (this.isSleepIntervalNecessary()) { | ||
return this.sleepIntervalIfNecessary(() => {this.run.apply(this, arguments); }); | ||
return this.sleepIntervalIfNecessary(() => { this.run.apply(this, arguments); }); | ||
} | ||
@@ -240,3 +245,14 @@ | ||
lastRunHash = newLastRunHash; | ||
} | ||
}, | ||
addToLastRunHash: function (items) { | ||
// Merge options to pass on additional options to request | ||
lastRunHash = Object.assign({}, lastRunHash, items); | ||
}, | ||
removeFromLastRunHash: function (items) { | ||
for (var i in items) { | ||
if (i in lastRunHash) { | ||
delete lastRunHash[i]; | ||
} | ||
} | ||
}, | ||
}; |
{ | ||
"name": "advancedrequest", | ||
"private": false, | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "ES6 JS class for intervaled requests with retries and timeouts", | ||
@@ -6,0 +6,0 @@ "keywords": ["advancedrequest", "advanced request", "http", "request", |
13304
211