Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-deco

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-deco - npm Package Compare versions

Comparing version 8.4.0 to 8.5.0

4

package.json
{
"name": "async-deco",
"version": "8.4.0",
"version": "8.5.0",
"description": "A collection of decorators for adding features to asynchronous functions (callback or promise based).",

@@ -41,3 +41,3 @@ "main": "index.js",

"es6-promisify": "^6.0.0",
"little-ds-toolkit": "^0.4.0",
"little-ds-toolkit": "^1.0.0",
"lodash": "^4.17.5",

@@ -44,0 +44,0 @@ "memoize-cache-utils": "^0.1.1",

@@ -183,16 +183,20 @@ async-deco

Memoize
-------
It caches the result. At any subsequent calls it will return the cached result.
This decorator implements an "in RAM" cache. That means that is possible having non-string cache keys and non-serializable values.
The cache is LRU, so it is advisable picking up a fixed cache length.
```js
var memoizeDecorator = require('async-deco/callback/memoize');
var simpleMemoize = memoizeDecorator(getKey);
var myfunc = simpleMemoize(function (..., cb) { .... });
var cached = memoizeDecorator({ error: ..., len: ..., ttl: ..., cacheKey: ....});
var myfunc = cached(function (..., cb) { .... });
```
It takes 1 argument:
* a getKey function [optional]: it runs against the original arguments and returns the key used for the caching. If it is missing, only one result will be memoized.
The "options" object may contains:
* an "error" attribute. This can be either an Error constructor function, or a function returning true if the result should be considered an error. The function takes as argument the output of the decorated function: error, result. If the result is an error the returned value is not cached.
* "len": the size of the cache
* "ttl": the number of ms to consider the cache entry valid. If a cached value is stale it remains cached until it exceeds the size of the cache
* "getKey": a function that returns a cacheKey, giving the same arguments of the decorated function
It logs "memoize-hit" with {key: cache key, result: cache result}
It logs:
* "memoize-hit" with {key: cache key, result: cache result}

@@ -199,0 +203,0 @@ Cache

var defaultLogger = require('../utils/default-logger')
var keyGetter = require('memoize-cache-utils/key-getter')
var LRUCache = require('little-ds-toolkit/lib/lru-cache')
var getErrorCondition = require('./get-error-condition')
function getMemoizeDecorator (wrapper, getKey) {
getKey = keyGetter(getKey || function () { return '_default' })
var cache = {}
function getMemoizeDecorator (wrapper, opts) {
opts = opts || {}
if (typeof opts === 'function') {
opts = { getKey: opts }
}
var getKey = opts.getKey || function () { return '_default' }
var defaultTTL = opts.ttl
var maxLen = opts.len
var condition = getErrorCondition(opts.error)
var cache = new LRUCache({ maxLen: maxLen, defaultTTL: defaultTTL })
return wrapper(function memoize (func) {
return function _memoize () {
var result
var context = this

@@ -15,11 +24,14 @@ var args = Array.prototype.slice.call(arguments, 0)

var cacheKey = getKey.apply(context, args)
args[args.length - 1] = function (err, dep) {
if (!err && cacheKey !== null) {
cache[cacheKey] = dep
args[args.length - 1] = function (err, res) {
if (cacheKey !== null && !condition(err, res)) {
cache.set(cacheKey, res)
}
cb(err, dep)
cb(err, res)
}
if (cacheKey !== null && cacheKey in cache) {
logger('memoize-hit', {key: cacheKey, result: cache[cacheKey]})
return cb(null, cache[cacheKey])
if (cacheKey !== null && cache.has(cacheKey)) {
result = cache.get(cacheKey)
logger('memoize-hit', {key: cacheKey, result: result})
return cb(null, result)
} else {

@@ -26,0 +38,0 @@ func.apply(context, args)

@@ -47,1 +47,45 @@ /* eslint-env node, mocha */

})
describe('memoize with parameters (callback)', function () {
var cached
beforeEach(function () {
cached = memoizeDecorator({
len: 1,
cacheKey: function (a, b, c) {
return a + b + c
}
})
})
it('must cache using different keys', function (done) {
var f = cached(function (a, b, c, next) {
next(undefined, a + b + c)
})
f(1, 2, 3, function (err, dep) {
assert.isFalse(!!err)
assert.equal(dep, 6)
f(3, 2, 1, function (err, dep) {
assert.isFalse(!!err)
assert.equal(dep, 6)
done()
})
})
})
it('must cache', function (done) {
var f = cached(function (a, b, c, next) {
next(undefined, Math.random())
})
f(1, 2, 3, function (err, res1) {
assert.isFalse(!!err)
f(3, 2, 1, function (err, res2) {
assert.isFalse(!!err)
assert.equal(res1, res2)
done()
})
})
})
})

@@ -47,1 +47,41 @@ /* eslint-env node, mocha */

})
describe('memoize with parameters (promise)', function () {
var cached
beforeEach(function () {
cached = memoizeDecorator({
len: 1,
cacheKey: function (a, b, c) {
return a + b + c
}
})
})
it('must cache using different keys', function (done) {
var f = cached(function (a, b, c) {
return Promise.resolve(a + b + c)
})
f(1, 2, 3).then(function (dep) {
assert.equal(dep, 6)
f(3, 2, 1).then(function (dep) {
assert.equal(dep, 6)
done()
})
})
})
it('must cache', function (done) {
var f = cached(function (a, b, c) {
Promise.resolve(Math.random())
})
f(1, 2, 3).then(function (res1) {
f(3, 2, 1).then(function (res2) {
assert.equal(res1, res2)
done()
})
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc