Skip to content

Commit

Permalink
feat(useTimeout): target support reactivity (#3923)
Browse files Browse the repository at this point in the history
Co-authored-by: banruo <shl@dataqin.com>
  • Loading branch information
huiliangShen and banruo authored May 27, 2024
1 parent 4a88231 commit 4eede0e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 15 additions & 0 deletions packages/shared/useTimeout/index.test.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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

0 comments on commit 4eede0e

Please sign in to comment.