Comparing version 3.0.0 to 4.0.0
@@ -1,6 +0,15 @@ | ||
import { Config, Result } from './types'; | ||
import { Config, ConstructKey, Key, Result } from './types'; | ||
/** | ||
* | ||
*/ | ||
export declare function bunch<T extends any, R extends any>(fn: (args: (T | null)[]) => R | PromiseLike<R>, { debounce, maxTimeout, maxCount, onNewBunch, onBunchExecute, }?: Config<T>): (arg?: T | undefined) => Promise<Result<T, R>>; | ||
export declare function keyed<T extends any, R extends any>(fn: (args: (T | null)[]) => R | PromiseLike<R>, config?: Config<T>): (key: string | string[], arg?: T | undefined) => Promise<Result<T, R>>; | ||
export declare function bunch<T extends any[], R extends any>(fn: (args: T[]) => R | PromiseLike<R>, { debounce, maxTimeout, maxCount, onNewBunch, onBunchExecute, }?: Config<T>): (...args: T) => Promise<Result<T, R>>; | ||
export declare function keyed<T extends any[], R extends any>(fn: (args: T[]) => R | PromiseLike<R>, config?: Config<T>): (key: Key | ConstructKey<T>, ...args: T) => Promise<{ | ||
result: R; | ||
index: number; | ||
arguments: T; | ||
bunch: { | ||
size: number; | ||
arguments: T[]; | ||
}; | ||
key: string[]; | ||
}>; |
@@ -24,4 +24,4 @@ "use strict"; | ||
reset(); | ||
return (arg) => { | ||
argBunch.push(arg || null); | ||
return (...args) => { | ||
argBunch.push(args); | ||
const promise = new Promise((resolve, reject) => deferred.push({ resolve, reject })); | ||
@@ -64,3 +64,3 @@ if (!invocationCount) | ||
const result = yield fn(snapshot.argBunch); | ||
snapshot.argBunch.forEach((argument, index) => { | ||
snapshot.argBunch.forEach((args, index) => { | ||
const defer = snapshot.deferred[index]; | ||
@@ -70,3 +70,3 @@ defer.resolve({ | ||
index, | ||
argument, | ||
arguments: args, | ||
bunch: { | ||
@@ -91,18 +91,18 @@ size: snapshot.invocationCount, | ||
const map = {}; | ||
return (key, arg) => { | ||
// console.log('KEYS', Object.keys(map).length); | ||
// console.log(Object.keys(map)); | ||
return (key, ...args) => __awaiter(this, void 0, void 0, function* () { | ||
if (!key) | ||
throw new Error(`bunchie keyed must be provided a key.`); | ||
key = Array.isArray(key) ? key : [key]; | ||
if (!key.length) | ||
const constructedKey = typeof key === 'function' ? key(...args) : key; | ||
const keyAsArray = Array.isArray(constructedKey) ? constructedKey : [constructedKey]; | ||
if (!keyAsArray.length) | ||
throw new Error(`bunchie keyed cannot be provided an empty array as a key.`); | ||
const joined = key.join(','); | ||
const joined = keyAsArray.join(','); | ||
if (!map[joined]) { | ||
map[joined] = bunch(fn, Object.assign(Object.assign({}, config), { onBunchExecute: () => delete map[joined] })); | ||
} | ||
return map[joined](arg); | ||
}; | ||
const result = yield map[joined](...args); | ||
return Object.assign({ key: keyAsArray }, result); | ||
}); | ||
} | ||
exports.keyed = keyed; | ||
//# sourceMappingURL=index.js.map |
@@ -14,4 +14,6 @@ /// <reference types="node" /> | ||
onNewBunch?: () => void; | ||
onBunchExecute?: (args: (T | null)[]) => void; | ||
onBunchExecute?: (args: T[]) => void; | ||
}; | ||
export declare type Key = string | string[]; | ||
export declare type ConstructKey<T extends any[]> = (...args: T) => Key; | ||
export declare type Defer<R> = { | ||
@@ -24,7 +26,7 @@ resolve: (response: R) => void; | ||
index: number; | ||
argument: T | null; | ||
arguments: T; | ||
bunch: { | ||
size: number; | ||
arguments: (T | null)[]; | ||
arguments: T[]; | ||
}; | ||
}; |
{ | ||
"name": "bunchie", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "A keyed debouncer.", | ||
@@ -26,3 +26,3 @@ "keywords": [ | ||
"start": "npm run build", | ||
"test": "npm run build && mocha -r ts-node/register tests/**/*.test.ts", | ||
"test": "npm run build && mocha --bail -r ts-node/register tests/**/*.test.ts", | ||
"prepublish": "npm run build", | ||
@@ -29,0 +29,0 @@ "prettify": "npx prettier --write '**/*.ts'" |
Sorry, the diff of this file is not supported yet
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
16392
173
0