New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

awesome-debounce-promise

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

awesome-debounce-promise - npm Package Compare versions

Comparing version

to
2.1.0

14

dist/index.d.ts

@@ -1,14 +0,8 @@

import * as DebouncePromise from 'debounce-promise';
declare type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never;
export declare const debouncePromise: typeof DebouncePromise;
import DebouncePromise from 'debounce-promise';
export declare type AwesomeDebounceOptions = {
key: (...args: any[]) => string;
key: (...args: any[]) => string | null | undefined;
onlyResolvesLast: boolean;
} & DebouncePromise.DebounceOptions;
export declare class DebounceCache {
debounceCache: object;
constructor();
getDebouncedFunction<T extends (...args: any[]) => any>(func: T, wait: number, options: AwesomeDebounceOptions, args: ArgumentsType<T>): any;
}
declare function AwesomeDebouncePromise<T extends (...args: any[]) => any>(func: T, wait: number, options?: Partial<AwesomeDebounceOptions>): (...args: ArgumentsType<T>) => any;
declare type ReturnedFunction<Fun> = Fun & {};
declare function AwesomeDebouncePromise<Fun extends (...args: any[]) => any>(func: Fun, wait: number, options?: Partial<AwesomeDebounceOptions>): ReturnedFunction<Fun>;
export default AwesomeDebouncePromise;

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

import DebouncePromise from 'debounce-promise';
import { onlyResolvesLast } from 'awesome-only-resolves-last-promise';
/*! *****************************************************************************

@@ -27,144 +30,2 @@ Copyright (c) Microsoft Corporation. All rights reserved.

function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/* global setTimeout, clearTimeout */
var dist = function debounce(fn) {
var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var lastCallAt = void 0;
var deferred = void 0;
var timer = void 0;
var pendingArgs = [];
return function debounced() {
var currentWait = getWait(wait);
var currentTime = new Date().getTime();
var isCold = !lastCallAt || currentTime - lastCallAt > currentWait;
lastCallAt = currentTime;
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (isCold && options.leading) {
return options.accumulate ? Promise.resolve(fn.call(this, [args])).then(function (result) {
return result[0];
}) : Promise.resolve(fn.call.apply(fn, [this].concat(args)));
}
if (deferred) {
clearTimeout(timer);
} else {
deferred = defer();
}
pendingArgs.push(args);
timer = setTimeout(flush.bind(this), currentWait);
if (options.accumulate) {
var _ret = function () {
var argsIndex = pendingArgs.length - 1;
return {
v: deferred.promise.then(function (results) {
return results[argsIndex];
})
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
return deferred.promise;
};
function flush() {
var thisDeferred = deferred;
clearTimeout(timer);
Promise.resolve(options.accumulate ? fn.call(this, pendingArgs) : fn.apply(this, pendingArgs[pendingArgs.length - 1])).then(thisDeferred.resolve, thisDeferred.reject);
pendingArgs = [];
deferred = null;
}
};
function getWait(wait) {
return typeof wait === 'function' ? wait() : wait;
}
function defer() {
var deferred = {};
deferred.promise = new Promise(function (resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
return deferred;
}
var DebouncePromise = /*#__PURE__*/Object.freeze({
default: dist,
__moduleExports: dist
});
function createImperativePromise(promiseArg) {
var resolve = null;
var reject = null;
var wrappedPromise = new Promise(function (_resolve, _reject) {
resolve = _resolve;
reject = _reject;
});
promiseArg && promiseArg.then(function (val) {
resolve && resolve(val);
}, function (error) {
reject && reject(error);
});
return {
promise: wrappedPromise,
resolve: function (value) {
resolve && resolve(value);
},
reject: function (reason) {
reject && reject(reason);
},
cancel: function () {
resolve = null;
reject = null;
}
};
}
// see https://stackoverflow.com/a/54825370/82609
function onlyResolvesLast(asyncFunction) {
var cancelPrevious = null;
var wrappedFunction = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
cancelPrevious && cancelPrevious();
var initialPromise = asyncFunction.apply(void 0, args);
var _a = createImperativePromise(initialPromise), promise = _a.promise, cancel = _a.cancel;
cancelPrevious = cancel;
return promise;
};
return wrappedFunction; // TODO fix TS
}
// We use DebouncePromise as a dependency as it does a great low-level job
// The behavior of the lib is to return the same promise for all function calls
var debouncePromise = DebouncePromise;
var DefaultKey = 'DEFAULT_AWESOME_DEBOUNCE_PROMISE_KEY';
var DefaultOptions = {

@@ -179,3 +40,3 @@ // One distinct debounced function is created per key and added to an internal cache

}
return DefaultKey;
return null;
},

@@ -192,17 +53,31 @@ // By default, a debounced function will only resolve

var DebounceCache = /** @class */ (function () {
function DebounceCache() {
this.debounceCache = {};
function DebounceCache(config) {
this.config = config;
this.debounceSingleton = null;
this.debounceCache = {}; // when key feature is used
}
DebounceCache.prototype.getDebouncedFunction = function (func, wait, options, args) {
var keyOptions = options.key, onlyResolvesLastOption = options.onlyResolvesLast, otherOptions = __rest(options, ["key", "onlyResolvesLast"]);
var key = keyOptions.apply(void 0, args);
// If the debounced function does not exist for this key, we create one on the fly and return it
if (!this.debounceCache[key]) {
var debouncedFunc = debouncePromise(func, wait, otherOptions);
if (onlyResolvesLastOption) {
debouncedFunc = onlyResolvesLast(debouncedFunc); // TODO fix TS
DebounceCache.prototype._createDebouncedFunction = function () {
var debouncedFunc = DebouncePromise(this.config.func, this.config.wait, this.config.options); // TODO TS
if (this.config.options.onlyResolvesLast) {
debouncedFunc = onlyResolvesLast(debouncedFunc);
}
return {
func: debouncedFunc,
};
};
DebounceCache.prototype.getDebouncedFunction = function (args) {
var _a;
var key = (_a = this.config.options).key.apply(_a, args);
if (key === null || typeof key === 'undefined') {
if (!this.debounceSingleton) {
this.debounceSingleton = this._createDebouncedFunction();
}
this.debounceCache[key] = debouncedFunc;
return this.debounceSingleton;
}
return this.debounceCache[key];
else {
if (!this.debounceCache[key]) {
this.debounceCache[key] = this._createDebouncedFunction();
}
return this.debounceCache[key];
}
};

@@ -213,4 +88,8 @@ return DebounceCache;

var finalOptions = __assign({}, DefaultOptions, options);
var debounceCache = new DebounceCache();
return function AwesomeDebouncePromiseWrapper() {
var debounceCache = new DebounceCache({
func: func,
wait: wait,
options: finalOptions,
});
var AwesomeDebouncePromiseWrapper = (function () {
var args = [];

@@ -220,9 +99,14 @@ for (var _i = 0; _i < arguments.length; _i++) {

}
var debouncedFn = debounceCache.getDebouncedFunction(func, wait, finalOptions, args);
var debouncedFn = debounceCache.getDebouncedFunction(args).func;
return debouncedFn.apply(void 0, args);
}); // TODO fix TS
/*
AwesomeDebouncePromiseWrapper.cancel = (key?: string) => {
};
*/
return AwesomeDebouncePromiseWrapper;
}
export default AwesomeDebouncePromise;
export { debouncePromise, DebounceCache };
//# sourceMappingURL=index.es.js.map

@@ -5,2 +5,7 @@ 'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var DebouncePromise = _interopDefault(require('debounce-promise'));
var awesomeOnlyResolvesLastPromise = require('awesome-only-resolves-last-promise');
/*! *****************************************************************************

@@ -32,144 +37,2 @@ Copyright (c) Microsoft Corporation. All rights reserved.

function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
t[p[i]] = s[p[i]];
return t;
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/* global setTimeout, clearTimeout */
var dist = function debounce(fn) {
var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var lastCallAt = void 0;
var deferred = void 0;
var timer = void 0;
var pendingArgs = [];
return function debounced() {
var currentWait = getWait(wait);
var currentTime = new Date().getTime();
var isCold = !lastCallAt || currentTime - lastCallAt > currentWait;
lastCallAt = currentTime;
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (isCold && options.leading) {
return options.accumulate ? Promise.resolve(fn.call(this, [args])).then(function (result) {
return result[0];
}) : Promise.resolve(fn.call.apply(fn, [this].concat(args)));
}
if (deferred) {
clearTimeout(timer);
} else {
deferred = defer();
}
pendingArgs.push(args);
timer = setTimeout(flush.bind(this), currentWait);
if (options.accumulate) {
var _ret = function () {
var argsIndex = pendingArgs.length - 1;
return {
v: deferred.promise.then(function (results) {
return results[argsIndex];
})
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
return deferred.promise;
};
function flush() {
var thisDeferred = deferred;
clearTimeout(timer);
Promise.resolve(options.accumulate ? fn.call(this, pendingArgs) : fn.apply(this, pendingArgs[pendingArgs.length - 1])).then(thisDeferred.resolve, thisDeferred.reject);
pendingArgs = [];
deferred = null;
}
};
function getWait(wait) {
return typeof wait === 'function' ? wait() : wait;
}
function defer() {
var deferred = {};
deferred.promise = new Promise(function (resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
return deferred;
}
var DebouncePromise = /*#__PURE__*/Object.freeze({
default: dist,
__moduleExports: dist
});
function createImperativePromise(promiseArg) {
var resolve = null;
var reject = null;
var wrappedPromise = new Promise(function (_resolve, _reject) {
resolve = _resolve;
reject = _reject;
});
promiseArg && promiseArg.then(function (val) {
resolve && resolve(val);
}, function (error) {
reject && reject(error);
});
return {
promise: wrappedPromise,
resolve: function (value) {
resolve && resolve(value);
},
reject: function (reason) {
reject && reject(reason);
},
cancel: function () {
resolve = null;
reject = null;
}
};
}
// see https://stackoverflow.com/a/54825370/82609
function onlyResolvesLast(asyncFunction) {
var cancelPrevious = null;
var wrappedFunction = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
cancelPrevious && cancelPrevious();
var initialPromise = asyncFunction.apply(void 0, args);
var _a = createImperativePromise(initialPromise), promise = _a.promise, cancel = _a.cancel;
cancelPrevious = cancel;
return promise;
};
return wrappedFunction; // TODO fix TS
}
// We use DebouncePromise as a dependency as it does a great low-level job
// The behavior of the lib is to return the same promise for all function calls
var debouncePromise = DebouncePromise;
var DefaultKey = 'DEFAULT_AWESOME_DEBOUNCE_PROMISE_KEY';
var DefaultOptions = {

@@ -184,3 +47,3 @@ // One distinct debounced function is created per key and added to an internal cache

}
return DefaultKey;
return null;
},

@@ -197,17 +60,31 @@ // By default, a debounced function will only resolve

var DebounceCache = /** @class */ (function () {
function DebounceCache() {
this.debounceCache = {};
function DebounceCache(config) {
this.config = config;
this.debounceSingleton = null;
this.debounceCache = {}; // when key feature is used
}
DebounceCache.prototype.getDebouncedFunction = function (func, wait, options, args) {
var keyOptions = options.key, onlyResolvesLastOption = options.onlyResolvesLast, otherOptions = __rest(options, ["key", "onlyResolvesLast"]);
var key = keyOptions.apply(void 0, args);
// If the debounced function does not exist for this key, we create one on the fly and return it
if (!this.debounceCache[key]) {
var debouncedFunc = debouncePromise(func, wait, otherOptions);
if (onlyResolvesLastOption) {
debouncedFunc = onlyResolvesLast(debouncedFunc); // TODO fix TS
DebounceCache.prototype._createDebouncedFunction = function () {
var debouncedFunc = DebouncePromise(this.config.func, this.config.wait, this.config.options); // TODO TS
if (this.config.options.onlyResolvesLast) {
debouncedFunc = awesomeOnlyResolvesLastPromise.onlyResolvesLast(debouncedFunc);
}
return {
func: debouncedFunc,
};
};
DebounceCache.prototype.getDebouncedFunction = function (args) {
var _a;
var key = (_a = this.config.options).key.apply(_a, args);
if (key === null || typeof key === 'undefined') {
if (!this.debounceSingleton) {
this.debounceSingleton = this._createDebouncedFunction();
}
this.debounceCache[key] = debouncedFunc;
return this.debounceSingleton;
}
return this.debounceCache[key];
else {
if (!this.debounceCache[key]) {
this.debounceCache[key] = this._createDebouncedFunction();
}
return this.debounceCache[key];
}
};

@@ -218,4 +95,8 @@ return DebounceCache;

var finalOptions = __assign({}, DefaultOptions, options);
var debounceCache = new DebounceCache();
return function AwesomeDebouncePromiseWrapper() {
var debounceCache = new DebounceCache({
func: func,
wait: wait,
options: finalOptions,
});
var AwesomeDebouncePromiseWrapper = (function () {
var args = [];

@@ -225,10 +106,14 @@ for (var _i = 0; _i < arguments.length; _i++) {

}
var debouncedFn = debounceCache.getDebouncedFunction(func, wait, finalOptions, args);
var debouncedFn = debounceCache.getDebouncedFunction(args).func;
return debouncedFn.apply(void 0, args);
}); // TODO fix TS
/*
AwesomeDebouncePromiseWrapper.cancel = (key?: string) => {
};
*/
return AwesomeDebouncePromiseWrapper;
}
exports.debouncePromise = debouncePromise;
exports.DebounceCache = DebounceCache;
exports.default = AwesomeDebouncePromise;
//# sourceMappingURL=index.js.map
{
"name": "awesome-debounce-promise",
"version": "2.0.1",
"version": "2.1.0",
"description": "Debounce your async calls",

@@ -28,9 +28,9 @@ "author": "slorber",

"@types/debounce-promise": "^3.1.1",
"awesome-only-resolves-last-promise": "^1.0.2",
"awesome-imperative-promise": "^1.0.1",
"awesome-only-resolves-last-promise": "^1.0.3",
"debounce-promise": "^3.1.0"
},
"devDependencies": {
"@svgr/rollup": "^4.1.0",
"@types/jest": "^24.0.6",
"cross-env": "^5.2.0",
"gh-pages": "^2.0.1",
"prettier": "^1.16.4",

@@ -42,5 +42,3 @@ "react-scripts-ts": "^3.1.0",

"rollup-plugin-peer-deps-external": "^2.2.0",
"rollup-plugin-postcss": "^2.0.3",
"rollup-plugin-typescript2": "^0.19.2",
"rollup-plugin-url": "^2.2.0",
"typescript": "^3.3.3333"

@@ -47,0 +45,0 @@ },

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet