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

@jcoreio/async-throttle

Package Overview
Dependencies
Maintainers
8
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jcoreio/async-throttle - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

.eslintcache

59

lib/index.js

@@ -1,16 +0,16 @@

"use strict";
'use strict';
var _regenerator = require("babel-runtime/regenerator");
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator");
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _toConsumableArray2 = require("babel-runtime/helpers/toConsumableArray");
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _promise = require("babel-runtime/core-js/promise");
var _promise = require('babel-runtime/core-js/promise');

@@ -28,6 +28,5 @@ var _promise2 = _interopRequireDefault(_promise);

module.exports = function throttle(fn, wait) {
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
getNextArgs = _ref.getNextArgs;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (!getNextArgs) getNextArgs = function getNextArgs(prev, next) {
var getNextArgs = options.getNextArgs || function (prev, next) {
return next;

@@ -45,2 +44,3 @@ };

var args = nextArgs;
if (!args) throw new Error('unexpected error: nextArgs is null');
nextArgs = null;

@@ -50,21 +50,30 @@ return lastPromise = fn.apply(undefined, (0, _toConsumableArray3.default)(args));

return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() {
var _args = arguments;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
nextArgs = nextArgs ? getNextArgs(nextArgs, [].concat(Array.prototype.slice.call(_args))) : [].concat(Array.prototype.slice.call(_args));
if (!nextPromise) {
nextPromise = _promise2.default.all([lastPromise, delay(lastInvokeTime + wait - Date.now())]).then(invoke, invoke);
}
return _context.abrupt("return", nextPromise);
return function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
case 3:
case "end":
return _context.stop();
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
nextArgs = nextArgs ? getNextArgs(nextArgs, args) : args;
if (!nextPromise) {
nextPromise = _promise2.default.all([lastPromise, delay(lastInvokeTime + wait - Date.now())]).then(invoke, invoke);
}
return _context.abrupt('return', nextPromise);
case 3:
case 'end':
return _context.stop();
}
}
}
}, _callee, this);
}));
}, _callee, this);
}));
return function () {
return _ref.apply(this, arguments);
};
}();
};
{
"name": "@jcoreio/async-throttle",
"version": "1.1.1",
"version": "1.1.2",
"description": "throttle async and promise-returning functions. Other packages don't do it right.",
"main": "lib/index.js",
"scripts": {
"lint": "eslint src test",
"lint:fix": "eslint --fix src test",
"lint:watch": "esw --watch src test",
"lint": "eslint src test --cache",
"lint:fix": "eslint --fix src test --cache",
"lint:watch": "esw --watch src test --cache",
"flow": "flow",
"flow:coverage": "for file in src/**.js test/**.js; do echo $file; flow coverage $file; done",
"flow:watch": "flow-watch -e js,js.flow,flowconfig --ignore lib/ --ignore node_modules/ --watch .flowconfig --watch src/ --watch test/",
"gen-flow-files": "flow gen-flow-files src/ --out-dir lib",
"copy-flow-files": "cd src; copy *.js.flow **/*.js.flow ../lib",
"build": "rimraf lib && babel src --out-dir lib",
"build": "rimraf lib && babel src --out-dir lib && flow-copy-source -v src/ lib",
"test": "NODE_ENV=production BABEL_ENV=test nyc --reporter=lcov --reporter=text mocha $npm_package_config_mocha",
"test:watch": "mocha --watch $npm_package_config_mocha",
"commitmsg": "validate-commit-msg",
"codecov": "nyc report --reporter=text-lcov > coverage.lcov; codecov",
"commitmsg": "commitlint -e $GIT_PARAMS",
"precommit": "npm run lint && flow",
"prepush": "npm test",
"prepublish": "npm run lint && flow && npm test && npm run build && npm run copy-flow-files",
"prepublishOnly": "npm run lint && flow && npm test && npm run build",
"open:coverage": "open coverage/lcov-report/index.html",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
"semantic-release": "semantic-release",
"travis-deploy-once": "travis-deploy-once"
},
"config": {
"mocha": "--compilers js:babel-core/register ./test/**/*.js",
"mocha": "-r babel-register ./test/**/*.js",
"commitizen": {

@@ -58,32 +58,35 @@ "path": "cz-conventional-changelog"

"devDependencies": {
"@jedwards1211/eslint-config": "^1.0.0",
"@commitlint/cli": "^6.0.2",
"@commitlint/config-conventional": "^6.0.2",
"@jedwards1211/commitlint-config": "^1.0.0",
"@jedwards1211/eslint-config": "^2.0.0",
"@jedwards1211/eslint-config-flow": "^1.0.0",
"babel-cli": "^6.22.2",
"babel-core": "^6.22.1",
"babel-cli": "^6.23.0",
"babel-core": "^6.23.1",
"babel-eslint": "^7.1.1",
"babel-plugin-istanbul": "^4.0.0",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-flow": "^6.23.0",
"babel-preset-stage-1": "^6.22.0",
"babel-register": "^6.22.0",
"babel-runtime": "^6.22.0",
"bluebird": "^3.5.0",
"chai": "^3.5.0",
"babel-register": "^6.23.0",
"babel-runtime": "^6.23.0",
"chai": "^4.1.2",
"codecov": "^3.0.0",
"copy": "^0.3.0",
"coveralls": "^2.11.16",
"eslint": "^3.13.1",
"eslint": "^3.15.0",
"eslint-plugin-flowtype": "^2.30.0",
"eslint-watch": "^3.0.0",
"flow-bin": "^0.56.0",
"flow-bin": "^0.72.0",
"flow-copy-source": "^1.2.1",
"flow-watch": "^1.1.0",
"husky": "^0.13.1",
"husky": "^0.14.3",
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"nyc": "^10.1.2",
"rimraf": "^2.5.4",
"semantic-release": "^6.3.6",
"sinon": "^2.1.0",
"validate-commit-msg": "^2.8.2"
"mocha": "^4.1.0",
"nyc": "^11.4.1",
"rimraf": "^2.6.0",
"semantic-release": "^15.1.4",
"sinon": "^5.0.7",
"travis-deploy-once": "^4.3.1"
}
}
}
# async-throttle
[![Build Status](https://travis-ci.org/jcoreio/async-throttle.svg?branch=master)](https://travis-ci.org/jcoreio/async-throttle)
[![Coverage Status](https://coveralls.io/repos/github/jcoreio/async-throttle/badge.svg?branch=master)](https://coveralls.io/github/jcoreio/async-throttle?branch=master)
-[![Coverage Status](https://codecov.io/gh/jcoreio/async-throttle/branch/master/graph/badge.svg)](https://codecov.io/gh/jcoreio/async-throttle)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

@@ -6,0 +6,0 @@ [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

@@ -1,15 +0,24 @@

const delay = ms => new Promise(resolve => setTimeout(resolve, Math.max(ms, 0) || 0))
// @flow
module.exports = function throttle(fn, wait, {getNextArgs} = {}) {
if (!getNextArgs) getNextArgs = (prev, next) => next
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, Math.max(ms, 0) || 0))
let nextArgs
let lastPromise = delay(0)
let nextPromise = null
let lastInvokeTime = NaN
module.exports = function throttle<Args: Array<any>, Value>(
fn: (...args: Args) => Promise<Value>,
wait: ?number,
options: {
getNextArgs?: (args0: Args, args1: Args) => Args,
} = {}
): (...args: Args) => Promise<Value> {
const getNextArgs = options.getNextArgs || ((prev, next) => next)
function invoke() {
let nextArgs: ?Args
let lastPromise: Promise<any> = delay(0)
let nextPromise: ?Promise<Value> = null
let lastInvokeTime: number = NaN
function invoke(): Promise<Value> {
lastInvokeTime = Date.now()
nextPromise = null
const args = nextArgs
if (!args) throw new Error('unexpected error: nextArgs is null')
nextArgs = null

@@ -19,6 +28,6 @@ return lastPromise = fn(...args)

return async function () {
return async function (...args: Args): Promise<Value> {
nextArgs = nextArgs
? getNextArgs(nextArgs, [...arguments])
: [...arguments]
? getNextArgs(nextArgs, args)
: args
if (!nextPromise) {

@@ -25,0 +34,0 @@ nextPromise = Promise.all([

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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