Skip to content

Commit

Permalink
feat(useInterval): support ref as interval (vitest-dev#1215)
Browse files Browse the repository at this point in the history
  • Loading branch information
okxiaoliang4 committed Feb 8, 2022
1 parent dcc010d commit a2b2b0d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/shared/useInterval/index.ts
@@ -1,6 +1,6 @@
import type { Ref } from 'vue-demi'
import { ref } from 'vue-demi'
import type { Pausable } from '../utils'
import type { MaybeRef, Pausable } from '../utils'
import { useIntervalFn } from '../useIntervalFn'

export interface IntervalOptions<Controls extends boolean> {
Expand All @@ -19,9 +19,9 @@ export interface IntervalOptions<Controls extends boolean> {
immediate?: boolean
}

export function useInterval(interval?: number, options?: IntervalOptions<false>): Ref<number>
export function useInterval(interval: number, options: IntervalOptions<true>): { counter: Ref<number> } & Pausable
export function useInterval(interval = 1000, options: IntervalOptions<boolean> = {}) {
export function useInterval(interval?: MaybeRef<number>, options?: IntervalOptions<false>): Ref<number>
export function useInterval(interval: MaybeRef<number>, options: IntervalOptions<true>): { counter: Ref<number> } & Pausable
export function useInterval(interval: MaybeRef<number> = 1000, options: IntervalOptions<boolean> = {}) {
const {
controls: exposeControls = false,
immediate = true,
Expand Down
7 changes: 6 additions & 1 deletion packages/shared/useIntervalFn/demo.vue
Expand Up @@ -4,14 +4,19 @@ import { useIntervalFn } from '.'
const greetings = ['Hello', 'Hi', 'Yo!', 'Hey', 'Hola', 'こんにちは', 'Bonjour', 'Salut!', '你好']
const word = ref('Hello')
const interval = ref(500)
const { pause, resume, isActive } = useIntervalFn(() => {
word.value = greetings[Math.round(Math.random() * (greetings.length - 1))]
}, 500)
}, interval)
</script>

<template>
<p>{{ word }}</p>
<p>
interval:
<input v-model="interval" type="number" placeholder="interval">
</p>
<button v-if="isActive" class="orange" @click="pause">
Pause
</button>
Expand Down
16 changes: 12 additions & 4 deletions packages/shared/useIntervalFn/index.ts
@@ -1,6 +1,6 @@
import { ref } from 'vue-demi'
import { isRef, ref, unref, watch } from 'vue-demi'
import { tryOnScopeDispose } from '../tryOnScopeDispose'
import type { Fn, Pausable } from '../utils'
import type { Fn, MaybeRef, Pausable } from '../utils'
import { isClient } from '../utils'

export interface IntervalFnOptions {
Expand All @@ -26,7 +26,7 @@ export interface IntervalFnOptions {
* @param interval
* @param options
*/
export function useIntervalFn(cb: Fn, interval = 1000, options: IntervalFnOptions = {}): Pausable {
export function useIntervalFn(cb: Fn, interval: MaybeRef<number> = 1000, options: IntervalFnOptions = {}): Pausable {
const {
immediate = true,
immediateCallback = false,
Expand Down Expand Up @@ -54,12 +54,20 @@ export function useIntervalFn(cb: Fn, interval = 1000, options: IntervalFnOption
if (immediateCallback)
cb()
clean()
timer = setInterval(cb, interval)
timer = setInterval(cb, unref(interval))
}

if (immediate && isClient)
resume()

if (isRef(interval)) {
const stopWatch = watch(interval, () => {
if (immediate && isClient)
resume()
})
tryOnScopeDispose(stopWatch)
}

tryOnScopeDispose(pause)

return {
Expand Down

0 comments on commit a2b2b0d

Please sign in to comment.