Skip to content

Commit

Permalink
feat(curry): add isPending method to debounce function (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnKnoWn-Consortium authored and aleclarson committed Jun 24, 2024
1 parent 873367f commit fd86975
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,15 @@ const debounce = ({ delay }, func) => {
clearTimeout(timer);
timer = setTimeout(() => {
active && func(...args);
timer = void 0;
}, delay);
} else {
func(...args);
}
};
debounced.isPending = () => {
return timer !== void 0;
};
debounced.cancel = () => {
active = false;
};
Expand Down
4 changes: 4 additions & 0 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,15 @@ var radash = (function (exports) {
clearTimeout(timer);
timer = setTimeout(() => {
active && func(...args);
timer = void 0;
}, delay);
} else {
func(...args);
}
};
debounced.isPending = () => {
return timer !== void 0;
};
debounced.cancel = () => {
active = false;
};
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions docs/curry/debounce.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ const makeSearchRequest = (event) => {
input.addEventListener('change', debounce({ delay: 100 }, makeSearchRequest))
```

## Timming
## Timing

A visual of the debounce behavior when `delay` is `100`. The debounce function
returned by `debounce` can be called every millisecond but it will only call
the given callback after `delay` milliseconds have passed.

```sh
Time: 0ms - - - - 100ms - - - - 200ms - - - - 300ms - - - - 400ms - - - -
Time: 0ms - - - - 100ms - - - - 200ms - - - - 300ms - - - - 400ms - - - -
debounce Invocations: x x x x - - - - - - - - x x x x x x x x x x - - - - - - - - - - - -
Source Invocations: - - - - - - - - - - x - - - - - - - - - - - - - - - - - x - - - - -
```

### Cancel

The function returned by `debounce` has a `cancel` property, a function that when called will permanently stop the source function from being debounced.
The function returned by `debounce` has a `cancel` method that when called will permanently stop the source function from being debounced.

```ts
const debounced = debounce({ delay: 100 }, api.feed.refresh)
Expand All @@ -47,7 +47,7 @@ debounced.cancel()

### Flush

The function returned by `debounce` has a `flush` property, a function that when called will directly invoke the source function.
The function returned by `debounce` has a `flush` method that when called will directly invoke the source function.

```ts
const debounced = debounce({ delay: 100 }, api.feed.refresh)
Expand All @@ -57,3 +57,15 @@ const debounced = debounce({ delay: 100 }, api.feed.refresh)
debounced.flush(event)
```

### isPending

The function returned by `debounce` has a `isPending` method that when called will return if there is any pending invocation the source function.

```ts
const debounced = debounce({ delay: 100 }, api.feed.refresh)

// ... sometime later

debounced.isPending()
```

8 changes: 8 additions & 0 deletions src/curry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export type DebounceFunction<TArgs extends any[]> = {
* Cancels the debounced function
*/
cancel(): void
/**
* Checks if there is any invocation debounced
*/
isPending(): boolean
/**
* Runs the debounced function immediately
*/
Expand Down Expand Up @@ -136,11 +140,15 @@ export const debounce = <TArgs extends any[]>(
clearTimeout(timer)
timer = setTimeout(() => {
active && func(...args)
timer = undefined
}, delay)
} else {
func(...args)
}
}
debounced.isPending = () => {
return timer !== undefined
}
debounced.cancel = () => {
active = false
}
Expand Down
18 changes: 16 additions & 2 deletions src/tests/curry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('curry module', () => {
expect(mockFunc).toHaveBeenCalledTimes(6)
})

test('when we call the flush method it should execute the function immediately', () => {
test('executes the function immediately when the flush method is called', () => {
func.flush()
expect(mockFunc).toHaveBeenCalledTimes(1)
})
Expand All @@ -208,7 +208,21 @@ describe('curry module', () => {
expect(mockFunc).toHaveBeenCalledTimes(3)
})

test('cancels all pending invocations when cancel is called', async () => {
test('cancels all pending invocations when the cancel method is called', async () => {
const results: boolean[] = []
func()
results.push(func.isPending())
results.push(func.isPending())
await _.sleep(610)
results.push(func.isPending())
func()
results.push(func.isPending())
await _.sleep(610)
results.push(func.isPending())
assert.deepEqual(results, [true, true, false, true, false])
})

test('returns if there is any pending invocation when the pending method is called', async () => {
func()
func.cancel()
await _.sleep(610)
Expand Down

0 comments on commit fd86975

Please sign in to comment.