Skip to content

Commit

Permalink
Updated to allow retry to also work on
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevalstar committed Apr 15, 2017
1 parent 55fec70 commit e896975
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -78,6 +78,7 @@ _You may also clear cache items using a wildcard characters e.g. Medusa.clear('s
Send in an updated settings object:

* debug: _will output logging_
* retry: _will allow for the concurrency queue to be bypassed after this interval, default: 5000_
* returnMutator: _a function to mutate the return value for output (good for using something like lodash.cloneDeep)_

## Policies
Expand Down
36 changes: 36 additions & 0 deletions __tests__/concurrency/concurrency.js
Expand Up @@ -36,6 +36,42 @@ describe('concurrency', () => {

});

pit('Clears the concurrent queue properly', () => {

return new Promise(resolve => {

jest.useRealTimers();

var calledCount = 0;

var incrementor = function(res) {
setTimeout(()=> {
res(calledCount += 1);
}, 1000);
};

var t1 = Medusa.get('sampleConcurrentClear', incrementor, 500)
.then(res => expect(res).toEqual(1));

var t2 = Medusa.get('sampleConcurrentClear', incrementor, 500)
.then(res => expect(res).toEqual(1));

setTimeout(()=> {
var t3 = Medusa.get('sampleConcurrentClear', incrementor, 1500)
.then(res => expect(res).toEqual(2));

//console.error("here 2", setTimeout.mock, jest.useRealTimers);
jest.runAllTimers();

Promise.all([t1, t2, t3]).then(() => {
resolve(true);
});
}, 400);

});

});

pit('Only calls the function once, and rejects both', () => {

return new Promise(resolve => {
Expand Down
10 changes: 7 additions & 3 deletions index.js
Expand Up @@ -103,7 +103,7 @@ var Medusa = (function() {
}
}else{
// Add current task to list
currentTasks[key] = [];
currentTasks[key] = [{queued: new Date()}];
}

return new Promise(function(resolve, reject) {
Expand All @@ -118,7 +118,9 @@ var Medusa = (function() {

if (hOP.call(currentTasks, key)) {
for (var i in currentTasks[key]) {
currentTasks[key][i].res(v);
if(currentTasks[key][i].res){
currentTasks[key][i].res(v);
}
}
delete currentTasks[key];
}
Expand All @@ -129,7 +131,9 @@ var Medusa = (function() {

if (hOP.call(currentTasks, key)) {
for (var i in currentTasks[key]) {
currentTasks[key][i].rej(v);
if(currentTasks[key][i].rej){
currentTasks[key][i].rej(v);
}
}
delete currentTasks[key];
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "medusajs",
"version": "1.2.0",
"version": "1.2.1",
"description": "a promise based caching library",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e896975

Please sign in to comment.