aboutsummaryrefslogtreecommitdiff
path: root/src/services/fetcher/fetcher.js
blob: 95b8c9d32d79292f3282a62477be45ea4c812d4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

export const makeFetcher = (call, interval) => {
  let stopped = false
  let timeout = null
  let func = () => {}

  func = () => {
    call().finally(() => {
      if (stopped) return
      timeout = window.setTimeout(func, interval)
    })
  }

  const stopFetcher = () => {
    stopped = true
    window.clearTimeout(timeout)
  }

  func()

  return stopFetcher
}