import { useIntervalFn } from '@vueuse/core'

/**
 * 轮询 composable
 * 封装 useIntervalFn，自动在组件卸载时清理
 */
export function usePolling(fn: () => void | Promise<void>, interval = 5000, immediate = true) {
  const { pause, resume } = useIntervalFn(fn, interval, { immediate })

  function stop() {
    pause()
  }

  function start() {
    if (immediate) fn()
    resume()
  }

  return { start, stop, pause, resume }
}
