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(useTimeout): target support reactivity #3923

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 15 additions & 0 deletions packages/shared/useTimeout/index.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it } from 'vitest'
import { ref } from 'vue-demi'
import { useTimeout } from '.'

describe('useTimeout', () => {
Expand All @@ -13,4 +14,18 @@ describe('useTimeout', () => {
expect(ready.value).toEqual(false)
setTimeout(() => expect(ready.value).toEqual(true), 10)
})

it('works with ref target', () => {
const interval = ref(10)
const ready = useTimeout(interval)
expect(ready.value).toEqual(false)
setTimeout(() => expect(ready.value).toEqual(true), 10)
})

it('works with controls and ref target', () => {
const interval = ref(10)
const { ready } = useTimeout(interval, { controls: true })
expect(ready.value).toEqual(false)
setTimeout(() => expect(ready.value).toEqual(true), 10)
})
})
8 changes: 4 additions & 4 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 { Fn, Stoppable } from '../utils'
import type { Fn, MaybeRefOrGetter, Stoppable } from '../utils'
import { noop } from '../utils'

export interface UseTimeoutOptions<Controls extends boolean> extends UseTimeoutFnOptions {
Expand All @@ -25,9 +25,9 @@ export interface UseTimeoutOptions<Controls extends boolean> extends UseTimeoutF
* @param interval
* @param options
*/
export function useTimeout(interval?: number, options?: UseTimeoutOptions<false>): ComputedRef<boolean>
export function useTimeout(interval: number, options: UseTimeoutOptions<true>): { ready: ComputedRef<boolean> } & Stoppable
export function useTimeout(interval = 1000, options: UseTimeoutOptions<boolean> = {}) {
export function useTimeout(interval?: MaybeRefOrGetter<number>, options?: UseTimeoutOptions<false>): ComputedRef<boolean>
export function useTimeout(interval: MaybeRefOrGetter<number>, options: UseTimeoutOptions<true>): { ready: ComputedRef<boolean> } & Stoppable
export function useTimeout(interval: MaybeRefOrGetter<number> = 1000, options: UseTimeoutOptions<boolean> = {}) {
const {
controls: exposeControls = false,
callback,
Expand Down