Skip to content

Commit

Permalink
Require Node.js 6, add TypeScript definition (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 12, 2019
1 parent 70c0c3f commit 126eb89
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
3 changes: 2 additions & 1 deletion .travis.yml
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
21 changes: 21 additions & 0 deletions index.d.ts
@@ -0,0 +1,21 @@
export interface Options {
/**
* Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1). Meaning immediately, instead of waiting for `wait` milliseconds.
*
* @default false
*/
readonly leading?: boolean;
}

/**
* [Debounce](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions.
*
* @param fn - Promise-returning/async function to debounce.
* @param wait - Milliseconds to wait before calling `fn`.
* @returns Returns a function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called.
*/
export default function pDebounce<ArgumentsType extends unknown[], ReturnType>(
fn: (...arguments: ArgumentsType) => PromiseLike<ReturnType> | ReturnType,
wait: number,
options?: Options
): (...arguments: ArgumentsType) => Promise<ReturnType>;
9 changes: 6 additions & 3 deletions index.js
@@ -1,5 +1,6 @@
'use strict';
module.exports = (fn, wait, opts) => {

const pDebounce = (fn, wait, opts) => {
if (!Number.isFinite(wait)) {
throw new TypeError('Expected `wait` to be a finite number');
}
Expand All @@ -10,9 +11,8 @@ module.exports = (fn, wait, opts) => {
let timer;
let resolveList = [];

return function () {
return function (...args) {
const ctx = this;
const args = arguments;

return new Promise(resolve => {
const runImmediately = opts.leading && !timer;
Expand Down Expand Up @@ -40,3 +40,6 @@ module.exports = (fn, wait, opts) => {
});
};
};

module.exports = pDebounce;
module.exports.default = pDebounce;
9 changes: 9 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,9 @@
import {expectType} from 'tsd-check';
import pDebounce from '.';

const expensiveCall = (input: number) => Promise.resolve(input);

expectType<(input: number) => Promise<number>>(pDebounce(expensiveCall, 200));
expectType<(input: number) => Promise<number>>(
pDebounce(expensiveCall, 200, {leading: true})
);
102 changes: 52 additions & 50 deletions package.json
@@ -1,52 +1,54 @@
{
"name": "p-debounce",
"version": "1.0.0",
"description": "Debounce promise-returning & async functions",
"license": "MIT",
"repository": "sindresorhus/p-debounce",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"promise",
"debounce",
"debounced",
"limit",
"limited",
"concurrency",
"throttle",
"throat",
"limited",
"interval",
"rate",
"batch",
"ratelimit",
"task",
"queue",
"async",
"await",
"promises",
"bluebird"
],
"devDependencies": {
"ava": "*",
"delay": "^1.3.1",
"in-range": "^1.0.0",
"time-span": "^1.0.0",
"xo": "*"
},
"xo": {
"esnext": true
}
"name": "p-debounce",
"version": "1.0.0",
"description": "Debounce promise-returning & async functions",
"license": "MIT",
"repository": "sindresorhus/p-debounce",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd-check"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"promise",
"debounce",
"debounced",
"limit",
"limited",
"concurrency",
"throttle",
"throat",
"limited",
"interval",
"rate",
"batch",
"ratelimit",
"task",
"queue",
"async",
"await",
"promises",
"bluebird"
],
"devDependencies": {
"ava": "^1.3.1",
"delay": "^4.1.0",
"in-range": "^1.0.0",
"time-span": "^2.0.0",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
},
"xo": {
"esnext": true
}
}
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -38,7 +38,7 @@ Returns a function that delays calling `fn` until after `wait` milliseconds have

Type: `Function`

Promise-returning/async function.
Promise-returning/async function to debounce.

#### wait

Expand Down
10 changes: 5 additions & 5 deletions test.js
Expand Up @@ -2,20 +2,20 @@ import test from 'ava';
import delay from 'delay';
import inRange from 'in-range';
import timeSpan from 'time-span';
import m from './';
import pDebounce from '.';

const fixture = Symbol('fixture');

test('single call', async t => {
const debounced = m(async val => val, 100);
const debounced = pDebounce(async val => val, 100);
t.is(await debounced(fixture), fixture);
});

test('multiple calls', async t => {
let count = 0;
const end = timeSpan();

const debounced = m(async val => {
const debounced = pDebounce(async val => {
count++;
await delay(50);
return val;
Expand All @@ -34,7 +34,7 @@ test('multiple calls', async t => {
test('leading option', async t => {
let count = 0;

const debounced = m(async val => {
const debounced = pDebounce(async val => {
count++;
await delay(50);
return val;
Expand All @@ -53,7 +53,7 @@ test('leading option', async t => {
test('leading option - does not call input function after timeout', async t => {
let count = 0;

const debounced = m(async () => {
const debounced = pDebounce(async () => {
count++;
}, 100, {leading: true});

Expand Down

0 comments on commit 126eb89

Please sign in to comment.