Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "onionskin", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Tadeu Zagallo <tadeuzagallo@gmail.com>" |
{ | ||
"name": "onionskin", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Multilayer Cache Manager for JavaScript", | ||
@@ -38,3 +38,3 @@ "main": "src/onionskin.js", | ||
"dependencies": { | ||
"bluebird": "~1.2.4" | ||
"bluebird": "~2.0.5" | ||
}, | ||
@@ -41,0 +41,0 @@ "testling": { |
# [![OnionSkin](logo/onionskin.png)](http://onionskin.io) [OnionSkin](http://onionskin.io) # | ||
[![NPM version](https://badge.fury.io/js/onionskin.svg)](http://badge.fury.io/js/onionskin) [![Dependency Status](https://david-dm.org/onionskin/onionskin.svg)](https://david-dm.org/onionskin/onionskin) [![Build Status](https://travis-ci.org/onionskin/onionskin.svg?branch=master)](https://travis-ci.org/onionskin/onionskin) [![Code Climate](https://codeclimate.com/github/onionskin/onionskin.png)](https://codeclimate.com/github/onionskin/onionskin) [![Code Climate](https://codeclimate.com/github/onionskin/onionskin/coverage.png)](https://codeclimate.com/github/onionskin/onionskin) | ||
@@ -30,5 +31,8 @@ OnionSkin is multi-layer cache manager library that works with Node.js and vanilla javascript | ||
pool.get('my/key/path').catch(function (err) { | ||
pool.get('my/key/path', function (err) { | ||
// Data is either inexistent or expired | ||
return slowFuncThatReturnsPromise().then(this.save); | ||
return slowFuncThatReturnsPromise(); | ||
}).then(function (value) { | ||
// The value that was either on cache or was just generated | ||
console.log(value); | ||
}); | ||
@@ -104,1 +108,7 @@ ``` | ||
If you want to join just follow the [instructions](#want-to-help), any help will be very welcome. | ||
## Changelog | ||
### 1.0.1 | ||
* The function to generate missing cache should be passed as the last parameter to `pool.get`, although generating cache `Promise.catch` is still supported the cache will never be unlocked if `item.save` or `item.set` are not called. | ||
* Also when the cache generator function is passed as parameter, there is no need to call either `item.save` or `item.set`, just return the value you want to be cached and it will automatically saved and passed along to the promise chain. |
@@ -102,4 +102,5 @@ 'use strict'; | ||
Item.prototype.save = function (value, expiration) { | ||
this.set(value, expiration); | ||
return value; | ||
return this.set(value, expiration).then(function () { | ||
return value; | ||
}); | ||
}; | ||
@@ -106,0 +107,0 @@ |
@@ -29,15 +29,30 @@ var Promise = require('bluebird'); | ||
Pool.prototype.get = function (key, cachePolicy, policyData) { | ||
Pool.prototype.get = function (key, cachePolicy, policyData, generator) { | ||
var item = this.getItem(key); | ||
return new Promise(function (resolve, reject) { | ||
item.get(cachePolicy, policyData).then(function (data) { | ||
item.isMiss().then(function (missed) { | ||
if (missed) { | ||
item.lock(); | ||
reject('Cache is missing'); | ||
} else { | ||
resolve(data); | ||
if (typeof cachePolicy === 'function') { | ||
generator = cachePolicy; | ||
cachePolicy = void 0; | ||
} else if (typeof policyData === 'function') { | ||
generator = policyData; | ||
policyData = void 0; | ||
} | ||
return item.get(cachePolicy, policyData).then(function (data) { | ||
return item.isMiss().then(function (missed) { | ||
if (missed) { | ||
item.lock(); | ||
if (!generator) { | ||
throw 'Cache is missing'; | ||
} | ||
}); | ||
return Promise.try(generator) | ||
.then(function (val) { | ||
return item.save(val); | ||
}).catch(function () { | ||
return item.unlock(); | ||
}); | ||
} else { | ||
return data; | ||
} | ||
}); | ||
@@ -44,0 +59,0 @@ }).bind(item); |
@@ -51,6 +51,8 @@ describe('OnionSkin::Pool', function () { | ||
it('should fail if the item isn\'t present', function (done) { | ||
pool.get('non_existing_key').then(function (data) { | ||
done(new Error('It should have failed', data)); | ||
}).catch(function () { | ||
done(); | ||
pool.get('non_existing_key').catch(function () { | ||
return 10; | ||
}).then(function (data) { | ||
catching(done, function () { | ||
expect(data).to.be.equal(10); | ||
}); | ||
}); | ||
@@ -60,4 +62,4 @@ }); | ||
it('should allow save on fail', function (done) { | ||
pool.get('non_existing_key2').catch(function (err) { | ||
return this.set('bar'); | ||
pool.get('non_existing_key2', function (err) { | ||
return 'bar'; | ||
}).then(function () { | ||
@@ -74,3 +76,3 @@ return pool.get('non_existing_key2'); | ||
var key = 'pool_get_3'; | ||
pool.get(key).catch(function (err) { | ||
pool.get(key, function (err) { | ||
pool.getItem(key).isLocked().then(function (locked) { | ||
@@ -84,3 +86,28 @@ catching(done, function () { | ||
it('should automatically unlock the cache after save', function (done) { | ||
pool.get('non_existing_key3', function () { | ||
return 1; | ||
}).then(function (value) { | ||
expect(value).to.be.equal(1); | ||
this.isLocked().then(function (locked) { | ||
catching(done, function () { | ||
expect(locked).to.be.false; | ||
}); | ||
}); | ||
}).done(); | ||
}); | ||
it('should automatically unlock the cache if generation fails', function (done) { | ||
pool.get('non_existing_key3', function () { | ||
throw 'Some error!'; | ||
}).finally(function () { | ||
this.isLocked().then(function (locked) { | ||
catching(done, function () { | ||
expect(locked).to.be.false; | ||
}); | ||
}); | ||
}).done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
1783487
6979
113
14
13
+ Addedbluebird@2.0.7(transitive)
- Removedbluebird@1.2.4(transitive)
Updatedbluebird@~2.0.5