2020-09-02 17:40:47 +00:00
|
|
|
|
2020-09-02 18:08:06 +00:00
|
|
|
// makeFetcher - replacement for setInterval for fetching, starts counting
|
|
|
|
// the interval only after a request is done instead of immediately.
|
2020-09-02 19:12:50 +00: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 18:08:06 +00:00
|
|
|
|
|
|
|
export const makeFetcher = (promiseCall, interval) => {
|
2020-09-02 17:40:47 +00:00
|
|
|
let stopped = false
|
|
|
|
let timeout = null
|
|
|
|
let func = () => {}
|
|
|
|
|
|
|
|
func = () => {
|
2020-09-02 18:08:06 +00:00
|
|
|
promiseCall().finally(() => {
|
2020-09-02 17:40:47 +00:00
|
|
|
if (stopped) return
|
|
|
|
timeout = window.setTimeout(func, interval)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const stopFetcher = () => {
|
|
|
|
stopped = true
|
2020-09-02 18:01:31 +00:00
|
|
|
window.clearTimeout(timeout)
|
2020-09-02 17:40:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:12:50 +00:00
|
|
|
timeout = window.setTimeout(func, interval)
|
2020-09-02 17:40:47 +00:00
|
|
|
|
|
|
|
return stopFetcher
|
|
|
|
}
|