Skip to content

Commit

Permalink
Rename leading to before (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Richienb and sindresorhus authored Apr 20, 2021
1 parent 2eee952 commit 84a463b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare namespace pDebounce {
@default false
*/
readonly leading?: boolean;
readonly before?: boolean;
}
}

Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ const pDebounce = (fn, wait, options = {}) => {
}

let leadingValue;
let timer;
let timeout;
let resolveList = [];

return function (...arguments_) {
return new Promise(resolve => {
const runImmediately = options.leading && !timer;
const shouldCallNow = options.before && !timeout;

clearTimeout(timer);
clearTimeout(timeout);

timer = setTimeout(() => {
timer = null;
timeout = setTimeout(() => {
timeout = null;

const result = options.leading ? leadingValue : fn.apply(this, arguments_);
const result = options.before ? leadingValue : fn.apply(this, arguments_);

for (resolve of resolveList) {
resolve(result);
Expand All @@ -25,7 +25,7 @@ const pDebounce = (fn, wait, options = {}) => {
resolveList = [];
}, wait);

if (runImmediately) {
if (shouldCallNow) {
leadingValue = fn.apply(this, arguments_);
resolve(leadingValue);
} else {
Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const expensiveCall = async (input: number) => input;

expectType<(input: number) => Promise<number>>(pDebounce(expensiveCall, 200));
expectType<(input: number) => Promise<number>>(
pDebounce(expensiveCall, 200, {leading: true})
pDebounce(expensiveCall, 200, {before: true})
);
expectType<(input: number) => Promise<number>>(
pDebounce.promise(expensiveCall)
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Milliseconds to wait before calling `fn`.

Type: `object`

##### leading
##### before

Type: `boolean`\
Default: `false`
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ test('.promise()', async t => {
t.is(await debounced(), 2);
});

test('leading option', async t => {
test('before option', async t => {
let count = 0;

const debounced = pDebounce(async value => {
count++;
await delay(50);
return value;
}, 100, {leading: true});
}, 100, {before: true});

const results = await Promise.all([1, 2, 3, 4].map(value => debounced(value)));

Expand All @@ -67,12 +67,12 @@ test('leading option', async t => {
t.is(await debounced(6), 5);
});

test('leading option - does not call input function after timeout', async t => {
test('before option - does not call input function after timeout', async t => {
let count = 0;

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

await delay(300);
await debounced();
Expand Down

0 comments on commit 84a463b

Please # to comment.