@prefecthq/prefect-ui-library
Version:
This library is the Vue and Typescript component library for [Prefect 2](https://github.com/PrefectHQ/prefect) and [Prefect Cloud 2](https://www.prefect.io/cloud/). _The components and utilities in this project are not meant to be used independently_.
33 lines (25 loc) • 1.16 kB
text/typescript
import { SubscriptionOptions } from '@prefecthq/vue-compositions'
import { ComputedRef, computed, ref } from 'vue'
import { MaybeRef, PrefectStateNames } from '@/types'
type StateResource = {
stateName: string | null,
}
const stateNamesThatShouldPoll: (string | null)[] = ['Pending', 'Running'] satisfies PrefectStateNames[]
export function useStatePolling(stateName: MaybeRef<string | null>, interval?: number): ComputedRef<SubscriptionOptions>
export function useStatePolling(resource: MaybeRef<StateResource>, interval?: number): ComputedRef<SubscriptionOptions>
export function useStatePolling(resourceOrStateName: MaybeRef<StateResource | string | null>, interval: number = 5000): ComputedRef<SubscriptionOptions> {
const argRef = ref(resourceOrStateName)
const shouldPoll = computed(() => {
const arg = argRef.value
if (arg === null) {
return false
}
if (typeof arg === 'object') {
return stateNamesThatShouldPoll.includes(arg.stateName)
}
return stateNamesThatShouldPoll.includes(arg)
})
return computed<SubscriptionOptions>(() => ({
interval: shouldPoll.value ? interval : undefined,
}))
}