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 3.0.0 to 3.1.0

tests/examples/smartcache.spec.js

4

callback/dedupe.js
var _dedupe = require('../src/dedupe');
var wrapper = require('../src/noop');
function dedupe(delay, getKey, logger) {
return _dedupe(wrapper, delay, getKey, logger);
function dedupe(getKey, logger) {
return _dedupe(wrapper, getKey, logger);
}
module.exports = dedupe;
{
"name": "async-deco",
"version": "3.0.0",
"version": "3.1.0",
"description": "A collection of decorators for adding features to asynchronous functions (callback or promise based).",

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

"husky": "^0.10.2",
"memoize-cache": "0.0.1",
"memoize-cache": "0.0.2",
"mocha": "^2.1.0",

@@ -35,3 +35,2 @@ "npm-release": "^1.0.0"

"es6-promisify": "^3.0.0",
"lodash": "^4.5.1",
"require-all": "^2.0.0",

@@ -38,0 +37,0 @@ "setimmediate": "^1.0.4"

var _dedupe = require('../src/dedupe');
var wrapper = require('../src/promise-translator');
function dedupe(delay, getKey, logger) {
return _dedupe(wrapper, delay, getKey, logger);
function dedupe(getKey, logger) {
return _dedupe(wrapper, getKey, logger);
}
module.exports = dedupe;

@@ -204,3 +204,4 @@ async-deco

------
It throttles the execution of the function. After collecting the output it dispatches it to all callbacks.
It executes the original function, while is waiting for the output it doesn't call the function anymore but instead it collects the callbacks.
After getting the result, it dispatches the same to all callbacks.
It may use the "getKey" function to group the callbacks into queues.

@@ -210,7 +211,6 @@ ```js

var dedupe = dedupeDecorator(100, getKey, getLogger);
var dedupe = dedupeDecorator(getKey, getLogger);
var myfunc = dedupe(function (..., cb) { .... });
```
The arguments:
* delay [optional, default 0] the number of milliseconds to throttle invocations to.
* getKey function [optional]: it runs against the original arguments and returns the key used for creating different queues of execution. If it is missing there will be only one execution queue.

@@ -268,5 +268,5 @@ * logger function (logs "deduping" whenever is calling more than one callback with the same results)

Smart memoize
-------------
Using memoize on an asynchronous function has a conceptual flaw. Let's say for example I have a function with 100ms latency. I call this function every 10 ms. I memoize for 100ms.
Smart memoize/cache
-------------------
Using memoize(or cache) on an asynchronous function has a conceptual flaw. Let's say for example I have a function with 100ms latency. I call this function every 10 ms:
```

@@ -277,8 +277,6 @@ executed ⬇⬇⬇⬇⬇⬇⬇⬇⬇⬇

```
What happen is that while I am still waiting for the first result (to memoize) I regularly execute other 9 functions.
What happen is that while I am still waiting for the first result (to cache) I regularly execute other 9 functions.
What if I compose memoize with dedupe?
```js
var decorator = compose(
memoizeDecorator(),
dedupeDecorator(100));
var decorator = compose(dedupeDecorator(), memoizeDecorator());

@@ -285,0 +283,0 @@ var newfunc = decorator(function (..., cb) { .... });

@@ -23,3 +23,2 @@ var noopLogger = require('./noop-logger');

};
cache.query(args, function (err, cacheQuery) {

@@ -26,0 +25,0 @@ if (!err && cacheQuery.cached === true) {

@@ -1,6 +0,4 @@

var throttle = require('lodash/throttle');
var noopLogger = require('./noop-logger');
function dedupeDecorator(wrapper, delay, getKey, getlogger) {
delay = delay || 0;
function dedupeDecorator(wrapper, getKey, getlogger) {
getKey = getKey || function () { return '_default'; };

@@ -11,3 +9,2 @@ getlogger = getlogger || noopLogger;

var callback_queues = {};
var functions = {};

@@ -23,3 +20,5 @@ return function () {

var len = cacheKey in callback_queues ? callback_queues[cacheKey].length : 0;
logger('deduping', {len: len});
if (len > 1) {
logger('deduping', {len: len, key: cacheKey});
}
for (var i = 0; i < len; i++) {

@@ -29,6 +28,5 @@ callback_queues[cacheKey][i](err, dep);

delete callback_queues[cacheKey];
delete functions[cacheKey];
}
if (!(cacheKey in functions)) {
if (!(cacheKey in callback_queues)) {
// creating callback

@@ -40,17 +38,8 @@ args[args.length - 1] = (function (cacheKey) {

}(cacheKey));
callback_queues[cacheKey] = [];
functions[cacheKey] = throttle((function (cacheKey) {
return function () {
var len = cacheKey in callback_queues ? callback_queues[cacheKey].length : 0;
if (len) { // callbacks already run, abort!
func.apply(context, args);
}
};
}(cacheKey)), delay);
callback_queues[cacheKey] = [cb];
func.apply(context, args);
}
callback_queues[cacheKey].push(cb);
functions[cacheKey]();
else {
callback_queues[cacheKey].push(cb);
}
};

@@ -57,0 +46,0 @@ });

@@ -16,4 +16,4 @@ var assert = require('chai').assert;

dedupe = dedupeDecorator(50, undefined, logger);
dedupeKey = dedupeDecorator(50, function (n) { return n % 2 === 0 ? 'even' : 'odd'; }, logger);
dedupe = dedupeDecorator(undefined, logger);
dedupeKey = dedupeDecorator(function (n) { return n % 2 === 0 ? 'even' : 'odd'; }, logger);
});

@@ -20,0 +20,0 @@

@@ -16,4 +16,4 @@ var assert = require('chai').assert;

dedupe = dedupeDecorator(50, undefined, logger);
dedupeKey = dedupeDecorator(50, function (n) { return n % 2 === 0 ? 'even' : 'odd'; }, logger);
dedupe = dedupeDecorator(undefined, logger);
dedupeKey = dedupeDecorator(function (n) { return n % 2 === 0 ? 'even' : 'odd'; }, logger);
});

@@ -20,0 +20,0 @@

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