Skip to content

Commit

Permalink
fix(useInterval): fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 18, 2023
1 parent b0ac4fa commit db66c02
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions packages/shared/useInterval/index.ts
Expand Up @@ -17,12 +17,18 @@ export interface UseIntervalOptions<Controls extends boolean> {
* @default true
*/
immediate?: boolean

/**
* Callback on every interval
*/
callback?: (count: number) => void
}

export interface UseIntervalControls {
counter: Ref<number>
reset: () => void
}

/**
* Reactive counter increases on every interval
*
Expand All @@ -31,7 +37,7 @@ export interface UseIntervalOptions<Controls extends boolean> {
* @param options
*/
export function useInterval(interval?: MaybeComputedRef<number>, options?: UseIntervalOptions<false>): Ref<number>
export function useInterval(interval: MaybeComputedRef<number>, options: UseIntervalOptions<true>): { counter: Ref<number> } & Pausable
export function useInterval(interval: MaybeComputedRef<number>, options: UseIntervalOptions<true>): UseIntervalControls & Pausable
export function useInterval(interval: MaybeComputedRef<number> = 1000, options: UseIntervalOptions<boolean> = {}) {
const {
controls: exposeControls = false,
Expand All @@ -41,13 +47,19 @@ export function useInterval(interval: MaybeComputedRef<number> = 1000, options:

const counter = ref(0)
const update = () => counter.value += 1
const reset = () => counter.value = 0
const controls = useIntervalFn(callback
? () => {
update()
callback(counter.value)
}
: update, interval, { immediate })
const reset = () => {
counter.value = 0
}
const controls = useIntervalFn(
callback
? () => {
update()
callback(counter.value)
}
: update,
interval,
{ immediate },
)

if (exposeControls) {
return {
Expand Down

0 comments on commit db66c02

Please sign in to comment.