2020-09-02 19:40:47 +02:00
|
|
|
|
2020-09-02 20:08:06 +02:00
|
|
|
// makeFetcher - replacement for setInterval for fetching, starts counting
|
|
|
|
// the interval only after a request is done instead of immediately.
|
2020-09-02 21:12:50 +02:00
|
|
|
// - promiseCall is a function that returns a promise, it's called the first
|
|
|
|
// time after the first interval.
|
|
|
|
// - interval is the interval delay in ms.
|
2020-09-02 20:08:06 +02:00
|
|
|
|
|
|
|
export const makeFetcher = (promiseCall, interval) => {
|
2020-09-02 19:40:47 +02:00
|
|
|
let stopped = false
|
|
|
|
let timeout = null
|
|
|
|
let func = () => {}
|
|
|
|
|
|
|
|
func = () => {
|
2020-09-02 20:08:06 +02:00
|
|
|
promiseCall().finally(() => {
|
2020-09-02 19:40:47 +02:00
|
|
|
if (stopped) return
|
|
|
|
timeout = window.setTimeout(func, interval)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const stopFetcher = () => {
|
|
|
|
stopped = true
|
2020-09-02 20:01:31 +02:00
|
|
|
window.clearTimeout(timeout)
|
2020-09-02 19:40:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 21:12:50 +02:00
|
|
|
timeout = window.setTimeout(func, interval)
|
2020-09-02 19:40:47 +02:00
|
|
|
|
|
|
|
return stopFetcher
|
|
|
|
}
|