feat: add utility composable for polling values

This commit is contained in:
Andrew Bastin
2021-12-17 14:42:13 +05:30
parent 65ee2ebcb2
commit a38c05febb

View File

@@ -5,6 +5,7 @@ import {
readonly, readonly,
Ref, Ref,
ref, ref,
shallowRef,
useContext, useContext,
watch, watch,
wrapProperty, wrapProperty,
@@ -154,3 +155,26 @@ export function useToast() {
const { $toast } = useContext() const { $toast } = useContext()
return $toast return $toast
} }
export function useColorMode() {
const { $colorMode } = useContext()
return $colorMode
}
export function usePolled<T>(
pollDurationMS: number,
pollFunc: () => T
): Ref<T> {
const result = shallowRef(pollFunc())
const handle = setInterval(() => {
result.value = pollFunc()
}, pollDurationMS)
onBeforeUnmount(() => {
clearInterval(handle)
})
return result
}