Skip to content

Commit 33afb36

Browse files
committed
feat: use-debounceFn add cancel flush
1 parent ecafa2c commit 33afb36

File tree

1 file changed

+13
-11
lines changed
  • packages/hooks/src/useDebounceFn

1 file changed

+13
-11
lines changed
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import debounce from "lodash/debounce";
1+
import debounce from 'lodash/debounce'
22

33
export interface DebounceOptions {
4-
wait?: number;
5-
leading?: boolean;
6-
trailing?: boolean;
7-
maxWait?: number;
4+
wait?: number
5+
leading?: boolean
6+
trailing?: boolean
7+
maxWait?: number
88
}
99

10-
type noop = (...args: any) => any;
10+
type noop = (...args: any) => any
1111

1212
function useDebounceFn<T extends noop>(fn: T, options?: DebounceOptions) {
13-
const wait = options?.wait ?? 1000;
14-
const run = debounce(fn, wait, options);
13+
const wait = options?.wait ?? 1000
14+
const debounced = debounce(fn, wait, options)
1515
return {
16-
run,
17-
};
16+
run: debounced,
17+
cancel: debounced.cancel,
18+
flush: debounced.flush,
19+
}
1820
}
1921

20-
export default useDebounceFn;
22+
export default useDebounceFn

0 commit comments

Comments
 (0)