cached-callback
Advanced tools
Comparing version 2.1.1 to 3.0.0
34
index.js
/* global define */ | ||
(function (root) { | ||
function cachedCallback (getter, cacher) { | ||
var map = {} | ||
var cache = (typeof cacher === 'object') ? cacher : cacher ? memoryCache() : false | ||
const map = {} | ||
const cache = (typeof cacher === 'object' && cacher) || (cacher && memoryCache()) || false | ||
return function collect (id, __argsAndCallback) { | ||
var args = toArray(arguments) | ||
var callback = args.pop() | ||
const args = toArray(arguments) | ||
const callback = args.pop() | ||
if (typeof callback !== 'function') throw new Error('The last argument has to be a callback.') | ||
var cached | ||
let cached | ||
if (cache && (cached = cache.get(id))) return invoke(callback, cached, cached.length) | ||
var callbacks = map[id] | ||
let callbacks = map[id] | ||
if (callbacks && callbacks.length) return callbacks.push(callback) | ||
@@ -22,4 +22,4 @@ callbacks = map[id] = [callback] | ||
delete map[id] | ||
var argumentLength = arguments.length | ||
for (var i = 0; i < callbacks.length; i++) { | ||
const argumentLength = arguments.length | ||
for (let i = 0; i < callbacks.length; i++) { | ||
invoke(callbacks[i], arguments, argumentLength) | ||
@@ -34,4 +34,4 @@ } | ||
function toArray (arrayLike) { | ||
var args = [] | ||
for (var i = 0, len = arrayLike.length; i < len; i++) { | ||
const args = [] | ||
for (let i = 0, len = arrayLike.length; i < len; i++) { | ||
args.push(arrayLike[i]) | ||
@@ -44,8 +44,8 @@ } | ||
switch (length) { | ||
case 0: cb(); 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) | ||
case 0: return cb() | ||
case 1: return cb(a[0]) | ||
case 2: return cb(a[0], a[1]) | ||
case 3: return cb(a[0], a[1], a[2]) | ||
case 4: return cb(a[0], a[1], a[2], a[3]) | ||
default: return cb.apply(null, a) | ||
} | ||
@@ -55,3 +55,3 @@ } | ||
function memoryCache () { | ||
var values = {} | ||
const values = {} | ||
return { | ||
@@ -58,0 +58,0 @@ get: function (key) { |
{ | ||
"name": "cached-callback", | ||
"version": "2.1.1", | ||
"version": "3.0.0", | ||
"description": "cached-callback caches arguments returned from an earlier execution and passes them to a callback passed in", | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"keywords": [ | ||
@@ -15,3 +18,3 @@ "callback", | ||
"scripts": { | ||
"test": "standard && node test", | ||
"test": "node test", | ||
"preversion": "npm test", | ||
@@ -22,5 +25,2 @@ "afterversion": "npm publish" | ||
"license": "ISC", | ||
"devDependencies": { | ||
"standard": "^5.3.1" | ||
}, | ||
"repository": { | ||
@@ -33,3 +33,17 @@ "type": "git", | ||
}, | ||
"homepage": "https://github.com/marcbachmann/cached-callback#readme" | ||
"homepage": "https://github.com/marcbachmann/cached-callback#readme", | ||
"release": { | ||
"extends": "@livingdocs/semantic-release-presets/npm-github" | ||
}, | ||
"ci": { | ||
"isCi": true, | ||
"name": "Drone", | ||
"service": "drone", | ||
"commit": "17785fae8831d04fa598a1117c2597d5a5800ccc", | ||
"build": "6", | ||
"branch": "master", | ||
"isPr": false, | ||
"slug": "marcbachmann/cached-callback", | ||
"date": "2019-03-17T12:06:08.897Z" | ||
} | ||
} |
# cached-callback | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/marcbachmann/cached-callback.svg)](https://greenkeeper.io/) | ||
cached-callback caches arguments returned from an earlier execution and passes them to a callback passed in | ||
@@ -4,0 +6,0 @@ |
30
test.js
@@ -1,3 +0,3 @@ | ||
var assert = require('assert') | ||
var debounce = require('./') | ||
const assert = require('assert') | ||
const debounce = require('./') | ||
@@ -7,4 +7,4 @@ // A getter only gets executed once | ||
// after the first callback got executed | ||
var executions = 0 | ||
var get = debounce(otherGetter) | ||
let executions = 0 | ||
const get = debounce(otherGetter) | ||
function otherGetter (id, callback) { | ||
@@ -15,3 +15,3 @@ executions += 1 | ||
var multi = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
const multi = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
multi.forEach(function () { | ||
@@ -38,3 +38,3 @@ get(1, function (arg1, arg2) { | ||
// All arguments get passed to the getter | ||
var second = debounce(function (arg1, arg2, arg3, callback) { | ||
const second = debounce(function (arg1, arg2, arg3, callback) { | ||
assert.equal(arg1, 'a') | ||
@@ -50,3 +50,3 @@ assert.equal(arg2, 'b') | ||
// The last argument has to be a callback | ||
var third = debounce(function (a, cb) {}) | ||
const third = debounce(function (a, cb) {}) | ||
assert.throws(function () { third('a', 'b') }, /The last argument has to be a callback/) | ||
@@ -56,4 +56,4 @@ assert.doesNotThrow(function () { third('a', callback) }) | ||
// test debounce when no key is used | ||
var called = 0 | ||
var without = debounce(function (cb) { | ||
let called = 0 | ||
const without = debounce(function (cb) { | ||
setTimeout(function () { cb(++called) }, 1) | ||
@@ -70,4 +70,4 @@ }) | ||
// Check cache | ||
var i = 0 | ||
var cached = debounce(function (a, cb) { | ||
let i = 0 | ||
const cached = debounce(function (a, cb) { | ||
cb(null, i++) | ||
@@ -91,5 +91,5 @@ }, true) | ||
// test caching when no key is passed | ||
var cachedCallback = require('./').cache(true) | ||
var times = 0 | ||
var persistent = cachedCallback(function (cb) { | ||
const cachedCallback = require('./').cache(true) | ||
let times = 0 | ||
const persistent = cachedCallback(function (cb) { | ||
setTimeout(function () { cb(++times) }, 1) | ||
@@ -105,3 +105,3 @@ }) | ||
process.on('exit', function (exitCode) { | ||
if (!exitCode) console.log('All tests succeeded.') | ||
if (!exitCode) console.log('All tests succeeded.') // eslint-disable-line no-console | ||
}) |
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
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
15087
0
6
347
85
1