Skip to content

Commit 4eede0e

Browse files
huiliangShenbanruo
and
banruo
authoredMay 27, 2024··
feat(useTimeout): target support reactivity (#3923)
Co-authored-by: banruo <shl@dataqin.com>
1 parent 4a88231 commit 4eede0e

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed
 

‎packages/shared/useTimeout/index.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2+
import { ref } from 'vue-demi'
23
import { useTimeout } from '.'
34

45
describe('useTimeout', () => {
@@ -13,4 +14,18 @@ describe('useTimeout', () => {
1314
expect(ready.value).toEqual(false)
1415
setTimeout(() => expect(ready.value).toEqual(true), 10)
1516
})
17+
18+
it('works with ref target', () => {
19+
const interval = ref(10)
20+
const ready = useTimeout(interval)
21+
expect(ready.value).toEqual(false)
22+
setTimeout(() => expect(ready.value).toEqual(true), 10)
23+
})
24+
25+
it('works with controls and ref target', () => {
26+
const interval = ref(10)
27+
const { ready } = useTimeout(interval, { controls: true })
28+
expect(ready.value).toEqual(false)
29+
setTimeout(() => expect(ready.value).toEqual(true), 10)
30+
})
1631
})

‎packages/shared/useTimeout/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ComputedRef } from 'vue-demi'
22
import { computed } from 'vue-demi'
33
import type { UseTimeoutFnOptions } from '../useTimeoutFn'
44
import { useTimeoutFn } from '../useTimeoutFn'
5-
import type { Fn, Stoppable } from '../utils'
5+
import type { Fn, MaybeRefOrGetter, Stoppable } from '../utils'
66
import { noop } from '../utils'
77

88
export interface UseTimeoutOptions<Controls extends boolean> extends UseTimeoutFnOptions {
@@ -25,9 +25,9 @@ export interface UseTimeoutOptions<Controls extends boolean> extends UseTimeoutF
2525
* @param interval
2626
* @param options
2727
*/
28-
export function useTimeout(interval?: number, options?: UseTimeoutOptions<false>): ComputedRef<boolean>
29-
export function useTimeout(interval: number, options: UseTimeoutOptions<true>): { ready: ComputedRef<boolean> } & Stoppable
30-
export function useTimeout(interval = 1000, options: UseTimeoutOptions<boolean> = {}) {
28+
export function useTimeout(interval?: MaybeRefOrGetter<number>, options?: UseTimeoutOptions<false>): ComputedRef<boolean>
29+
export function useTimeout(interval: MaybeRefOrGetter<number>, options: UseTimeoutOptions<true>): { ready: ComputedRef<boolean> } & Stoppable
30+
export function useTimeout(interval: MaybeRefOrGetter<number> = 1000, options: UseTimeoutOptions<boolean> = {}) {
3131
const {
3232
controls: exposeControls = false,
3333
callback,

0 commit comments

Comments
 (0)
Please sign in to comment.