feat: stop extension status polling when a status is resolved

This commit is contained in:
Andrew Bastin
2021-12-17 15:11:48 +05:30
parent 655e6dc2a4
commit 156011f2cd
3 changed files with 40 additions and 18 deletions

View File

@@ -164,16 +164,29 @@ export function useColorMode() {
export function usePolled<T>(
pollDurationMS: number,
pollFunc: () => T
pollFunc: (stopPolling: () => void) => T
): Ref<T> {
const result = shallowRef(pollFunc())
let polling = true
let handle: ReturnType<typeof setInterval> | undefined
const handle = setInterval(() => {
result.value = pollFunc()
}, pollDurationMS)
const stopPolling = () => {
if (handle) {
clearInterval(handle)
handle = undefined
polling = false
}
}
const result = shallowRef(pollFunc(stopPolling))
if (polling) {
handle = setInterval(() => {
result.value = pollFunc(stopPolling)
}, pollDurationMS)
}
onBeforeUnmount(() => {
clearInterval(handle)
if (polling) stopPolling()
})
return result