-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuse-query-backed-subscription.ts
More file actions
36 lines (30 loc) · 1.18 KB
/
use-query-backed-subscription.ts
File metadata and controls
36 lines (30 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { computed } from "vue";
import type { ComputedRef, Ref } from "vue";
import { UseQueryResponse, UseSubscriptionResponse } from "@urql/vue";
import { onReady } from "@/utils/on-ready";
import type { OnReady } from "@/utils/on-ready";
import { useMappedWithFallback } from "./use-mapped-with-fallback";
interface UseQueryBackedSubscriptionOptions<T, Q, S> {
query: UseQueryResponse<Q>;
subscription: UseSubscriptionResponse<S>;
mapQueryData: (d: Ref<Q | undefined>) => T | undefined;
mapSubscriptionData: (d: Ref<S | undefined>) => T | undefined;
}
type UseQueryBackedSubscriptionReturn<T> = OnReady<{
data: ComputedRef<T | undefined>;
error: ComputedRef<Error | undefined>;
loading: ComputedRef<boolean>;
}>;
export function useQueryBackedSubscription<T, Q, S>(
options: UseQueryBackedSubscriptionOptions<T, Q, S>,
): UseQueryBackedSubscriptionReturn<T> {
const mapped = useMappedWithFallback({
response1: options.subscription,
response2: options.query,
map1: options.mapSubscriptionData,
map2: options.mapQueryData,
});
const loading = computed(() => options.query.fetching?.value);
const ret = { ...mapped, loading };
return onReady(ret, options.query);
}