|
| 1 | +import { computed } from "vue"; |
| 2 | +import type { ComputedRef, Ref } from "vue"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Response returned by `useQuery()` / `useSubscription()`. |
| 6 | + * |
| 7 | + * `T` is the data type, the equivalent to the first argument of `UseQueryResponse` and |
| 8 | + * `UseSubscriptionResponse`. |
| 9 | + */ |
| 10 | +interface Response<T> { |
| 11 | + data: Ref<T | undefined>; |
| 12 | + error: Ref<Error | undefined>; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Options for the `useMappedWithFallback` composable. |
| 17 | + * |
| 18 | + * The interface includes two responses and two mapping functions. The two responses are |
| 19 | + * primary and secondary, which are typically returned by `useSubscription()` and |
| 20 | + * `useQuery()`, respectively. The two mapping functions convert the respective response |
| 21 | + * data into a common target type `T`. The secondary response is used as a fallback when |
| 22 | + * the primary response data is `undefined`. |
| 23 | + * |
| 24 | + * @template T The target type that both mappers should produce |
| 25 | + * @template D1 The data type of Response<D1>["data"] |
| 26 | + * @template D2 The data type of Response<D2>["data"] |
| 27 | + * @template R1 The primary response type, typically `UseSubscriptionResponse<D1>` |
| 28 | + * @template R2 The fallback response type, typically `UseQueryResponse<D2>` |
| 29 | + * |
| 30 | + */ |
| 31 | +export interface UseMappedWithFallbackOptions< |
| 32 | + T, |
| 33 | + D1, |
| 34 | + D2, |
| 35 | + R1 extends Response<D1>, |
| 36 | + R2 extends Response<D2>, |
| 37 | +> { |
| 38 | + /** The primary response source, typically returned by `useSubscription()` */ |
| 39 | + response1: R1; |
| 40 | + |
| 41 | + /** The fallback response source, typically returned by `useQuery()` */ |
| 42 | + response2: R2; |
| 43 | + |
| 44 | + /** Mapping function for the primary response data. `d` is `response1.data` */ |
| 45 | + map1: (d: Ref<D1 | undefined>) => T | undefined; |
| 46 | + |
| 47 | + /** Mapping function for the fallback response data. `d` is `response2.data` */ |
| 48 | + map2: (d: Ref<D2 | undefined>) => T | undefined; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Return type of `useMappedWithFallback()`. |
| 53 | + * |
| 54 | + * The returned object contains two reactive properties: `data` and `error`. |
| 55 | + */ |
| 56 | +interface UseMappedWithFallbackReturn<T> { |
| 57 | + /** |
| 58 | + * The reactive value mapped primarily from `response1` with fallback to `response2`. |
| 59 | + * `undefined` if either response has an error. |
| 60 | + */ |
| 61 | + data: ComputedRef<T | undefined>; |
| 62 | + |
| 63 | + /** |
| 64 | + * The error from `response1` otherwise from `response2` else `undefined` |
| 65 | + */ |
| 66 | + error: ComputedRef<Error | undefined>; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * A reactive mapped value from a primary response with secondary fallback. |
| 71 | + * |
| 72 | + * @param options {@link UseMappedWithFallbackOptions} |
| 73 | + * @returns {@link UseMappedWithFallbackReturn} |
| 74 | + * @example |
| 75 | + * ```typescript |
| 76 | + * const subscription = useSubscription<D1>({ query: SUBSCRIPTION_QUERY }); |
| 77 | + * const query = useQuery<D2>({ query: QUERY }); |
| 78 | + * |
| 79 | + * const { data, error } = useMappedWithFallback({ |
| 80 | + * response1: subscription, // primary: real-time updates |
| 81 | + * response2: query, // fallback: initial data |
| 82 | + * map1: (d) => d.value?.ctrlState, |
| 83 | + * map2: (d) => d.value?.ctrl.state, |
| 84 | + * }); |
| 85 | + * ``` |
| 86 | + */ |
| 87 | +export function useMappedWithFallback< |
| 88 | + T, |
| 89 | + D1, |
| 90 | + D2, |
| 91 | + R1 extends Response<D1>, |
| 92 | + R2 extends Response<D2>, |
| 93 | +>( |
| 94 | + options: UseMappedWithFallbackOptions<T, D1, D2, R1, R2>, |
| 95 | +): UseMappedWithFallbackReturn<T> { |
| 96 | + const error = computed( |
| 97 | + () => options.response1.error?.value ?? options.response2.error?.value, |
| 98 | + ); |
| 99 | + |
| 100 | + const data = computed(() => |
| 101 | + error.value |
| 102 | + ? undefined |
| 103 | + : (options.map1(options.response1.data) ?? options.map2(options.response2.data)), |
| 104 | + ); |
| 105 | + |
| 106 | + return { data, error }; |
| 107 | +} |
0 commit comments