Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optional callback for useInterval, useTimeout & useTimestamp #2240

Merged
merged 1 commit into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/core/useTimestamp/index.ts
Expand Up @@ -32,6 +32,10 @@ export interface UseTimestampOptions<Controls extends boolean> {
* @default requestAnimationFrame
*/
interval?: 'requestAnimationFrame' | number
/**
* Callback on each update
*/
callback?: (timestamp: number) => void
}

/**
Expand All @@ -48,15 +52,22 @@ export function useTimestamp(options: UseTimestampOptions<boolean> = {}) {
offset = 0,
immediate = true,
interval = 'requestAnimationFrame',
callback,
} = options

const ts = ref(timestamp() + offset)

const update = () => ts.value = timestamp() + offset
const cb = callback
? () => {
update()
callback(ts.value)
}
: update

const controls: Pausable = interval === 'requestAnimationFrame'
? useRafFn(update, { immediate })
: useIntervalFn(update, interval, { immediate })
? useRafFn(cb, { immediate })
: useIntervalFn(cb, interval, { immediate })

if (exposeControls) {
return {
Expand Down
13 changes: 12 additions & 1 deletion packages/shared/useInterval/index.ts
Expand Up @@ -17,6 +17,10 @@ export interface UseIntervalOptions<Controls extends boolean> {
* @default true
*/
immediate?: boolean
/**
* Callback on every interval
*/
callback?: (count: number) => void
}

/**
Expand All @@ -32,10 +36,17 @@ export function useInterval(interval: MaybeComputedRef<number> = 1000, options:
const {
controls: exposeControls = false,
immediate = true,
callback,
} = options

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

if (exposeControls) {
return {
Expand Down
9 changes: 7 additions & 2 deletions packages/shared/useTimeout/index.ts
Expand Up @@ -2,7 +2,7 @@ import type { ComputedRef } from 'vue-demi'
import { computed } from 'vue-demi'
import type { UseTimeoutFnOptions } from '../useTimeoutFn'
import { useTimeoutFn } from '../useTimeoutFn'
import type { Stoppable } from '../utils'
import type { Fn, Stoppable } from '../utils'
import { noop } from '../utils'

export interface UseTimeoutOptions<Controls extends boolean> extends UseTimeoutFnOptions {
Expand All @@ -12,6 +12,10 @@ export interface UseTimeoutOptions<Controls extends boolean> extends UseTimeoutF
* @default false
*/
controls?: Controls
/**
* Callback on timeout
*/
callback?: Fn
}

/**
Expand All @@ -26,10 +30,11 @@ export function useTimeout(interval: number, options: UseTimeoutOptions<true>):
export function useTimeout(interval = 1000, options: UseTimeoutOptions<boolean> = {}) {
const {
controls: exposeControls = false,
callback,
} = options

const controls = useTimeoutFn(
noop,
callback ?? noop,
interval,
options,
)
Expand Down