cached-callback
Advanced tools
Comparing version 2.0.0 to 2.1.0
14
index.js
@@ -43,5 +43,6 @@ /* global define */ | ||
case 0: cb(); break | ||
case 1: cb(a[0], a[1]); break | ||
case 2: cb(a[0], a[1], a[2]); break | ||
case 3: cb(a[0], a[1], a[2], a[3]); break | ||
case 1: cb(a[0]); break | ||
case 2: cb(a[0], a[1]); break | ||
case 3: cb(a[0], a[1], a[2]); break | ||
case 4: cb(a[0], a[1], a[2], a[3]); break | ||
default: cb.apply(null, a) | ||
@@ -63,2 +64,9 @@ } | ||
cachedCallback.cache = function configureCache (cacher) { | ||
if (typeof cacher === 'undefined') cacher = true | ||
return function cachedCallbackWithCache (getter) { | ||
return cachedCallback(getter, cacher) | ||
} | ||
} | ||
if (typeof exports === 'object') { | ||
@@ -65,0 +73,0 @@ module.exports = cachedCallback |
{ | ||
"name": "cached-callback", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -43,2 +43,10 @@ # cached-callback | ||
The second `cache` argument is also exposed with `.cache()` | ||
```js | ||
var cached = require('cached-callback').cache() | ||
var get = cached(function (id, callback) { | ||
request('http://example.com/'+id, callback) | ||
} | ||
``` | ||
### Custom caching method | ||
@@ -45,0 +53,0 @@ Caches the result with a custom caching method. |
26
test.js
@@ -51,2 +51,14 @@ var assert = require('assert') | ||
// test debounce when no key is used | ||
var called = 0 | ||
var without = debounce(function (cb) { | ||
setTimeout(function () { cb(++called) }, 1) | ||
}) | ||
without(function (val) { assert.equal(val, 1) }) | ||
without(function (val) { assert.equal(val, 1) }) | ||
setTimeout(function () { | ||
without(function (args) { assert.equal(args, 2) }) | ||
}, 2) | ||
// | ||
@@ -72,4 +84,18 @@ // Check cache | ||
// test cache setter cachedCallback.cache(true) | ||
// test caching when no key is passed | ||
var cachedCallback = require('./').cache(true) | ||
var times = 0 | ||
var persistent = cachedCallback(function (cb) { | ||
setTimeout(function () { cb(++times) }, 1) | ||
}) | ||
persistent(function (val) { assert.equal(val, 1) }) | ||
persistent(function (val) { assert.equal(val, 1) }) | ||
setTimeout(function () { | ||
persistent(function (val) { assert.equal(val, 1) }) | ||
}, 2) | ||
process.on('exit', function (exitCode) { | ||
if (!exitCode) console.log('All tests succeeded.') | ||
}) |
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
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
7354
152
80