import { Action, RefreshChannelOptions, UseSubscription } from '@prefecthq/vue-compositions' import { MaybeRefOrGetter, computed, reactive, toValue } from 'vue' type ExtractAction[]> = { [K in keyof T]: T[K] extends UseSubscription ? V : never } export type UseSubscriptions = { subscriptions: Omit, 'promise' | 'response' | 'error'> & { responses: UseSubscription['response'][], errors: UseSubscription['error'][], }, } export function useSubscriptions[]>(subscriptions: MaybeRefOrGetter): UseSubscriptions[number]> { const source = computed(() => toValue(subscriptions)) const loading = computed(() => source.value.some(subscription => subscription.loading)) const errored = computed(() => source.value.some(subscription => subscription.errored)) const errors = computed(() => source.value.map(subscription => subscription.error)) const executed = computed(() => source.value.length > 0 && source.value.every(subscription => subscription.executed)) const responses = computed(() => source.value.map(subscription => subscription.response)) const paused = computed(() => source.value.some(subscription => subscription.paused)) const late = computed(() => source.value.some(subscription => subscription.late)) const unsubscribe = (): void => { source.value.forEach(subscription => subscription.unsubscribe()) } const refresh = async (options?: RefreshChannelOptions): Promise => { const promises = source.value.map(subscription => subscription.refresh(options)) await Promise.all(promises) } const isSubscribed = (): boolean => { return source.value.every(subscription => subscription.isSubscribed()) } const response: UseSubscriptions[number]>['subscriptions'] = reactive({ loading, errored, errors, executed, paused, late, responses, unsubscribe, refresh, isSubscribed, }) return { subscriptions: response } }