|
| 1 | +import { gql, useMutation } from '@urql/vue' |
| 2 | +import { Ref, computed, watch } from 'vue' |
| 3 | +import { useDebounce } from '@vueuse/core' |
| 4 | +import { CloudData_RefetchDocument, SpecsListFragment } from '../generated/graphql' |
| 5 | + |
| 6 | +gql` |
| 7 | +mutation CloudData_Refetch ($ids: [ID!]!) { |
| 8 | + loadRemoteFetchables(ids: $ids){ |
| 9 | + id |
| 10 | + fetchingStatus |
| 11 | + } |
| 12 | +} |
| 13 | +` |
| 14 | + |
| 15 | +type NonNullCloudSpec = Exclude<SpecsListFragment['cloudSpec'], undefined | null> |
| 16 | + |
| 17 | +/** |
| 18 | + * Set up watchers & subscriptions to clear/refetch cloud data based on project state relative to latest data in the cloud |
| 19 | + * @param isProjectDisconnected Whether project has a valid connection to the cloud |
| 20 | + * @param isOffline Whether app is currently offline (network state) |
| 21 | + * @param projectId Cloud project ID (slug) |
| 22 | + * @param mostRecentUpdate Date/time of the latest project update in the cloud |
| 23 | + * @param displayedSpecs Spec entries currently displayed in the app |
| 24 | + * @param allSpecs All project spec entries |
| 25 | + * @returns Trigger functions |
| 26 | + */ |
| 27 | +export function useCloudSpecData ( |
| 28 | + isProjectDisconnected: Ref<boolean>, |
| 29 | + isOffline: Ref<boolean>, |
| 30 | + projectId: string | null | undefined, |
| 31 | + mostRecentUpdate: Ref<string | null>, |
| 32 | + displayedSpecs: Ref<(SpecsListFragment | undefined)[]>, |
| 33 | + allSpecs: (SpecsListFragment | undefined)[], |
| 34 | +) { |
| 35 | + const refetchMutation = useMutation(CloudData_RefetchDocument) |
| 36 | + |
| 37 | + const isCloudSpecOlderThan = (item: NonNullCloudSpec, comparisonDttm: string | null) => { |
| 38 | + if (item.data?.__typename !== 'CloudProjectSpec' && item.data?.__typename !== 'CloudProjectSpecNotFound') { |
| 39 | + return false |
| 40 | + } |
| 41 | + |
| 42 | + if (!item.data.retrievedAt || !comparisonDttm) { |
| 43 | + return false |
| 44 | + } |
| 45 | + |
| 46 | + return new Date(comparisonDttm).getTime() > new Date(item.data.retrievedAt).getTime() |
| 47 | + } |
| 48 | + |
| 49 | + const shouldRefetch = (item: NonNullCloudSpec) => { |
| 50 | + if (isOffline.value) { |
| 51 | + // Offline, no need to refetch |
| 52 | + return false |
| 53 | + } |
| 54 | + |
| 55 | + if (item.fetchingStatus === 'NOT_FETCHED' || item.fetchingStatus === undefined) { |
| 56 | + // Not yet fetched |
| 57 | + return true |
| 58 | + } |
| 59 | + |
| 60 | + if (isCloudSpecOlderThan(item, mostRecentUpdate.value)) { |
| 61 | + // outdated |
| 62 | + return true |
| 63 | + } |
| 64 | + |
| 65 | + // nothing new, no need to refetch |
| 66 | + return false |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Refetch any displayed RemoteFetchable entries that are older than the latest cloudProject update |
| 71 | + * or that have not yet been fetched |
| 72 | + */ |
| 73 | + const fetchDisplayedCloudData = async () => { |
| 74 | + const cloudSpecIdsToRefetch = displayedSpecs.value |
| 75 | + .map((spec) => spec?.cloudSpec) |
| 76 | + .filter((cloudSpec): cloudSpec is NonNullCloudSpec => Boolean(cloudSpec && shouldRefetch(cloudSpec))) |
| 77 | + .map((cloudSpec) => cloudSpec.id) |
| 78 | + ?? [] |
| 79 | + |
| 80 | + if (!isProjectDisconnected.value && !refetchMutation.fetching.value && cloudSpecIdsToRefetch.length > 0) { |
| 81 | + await refetchMutation.executeMutation({ ids: cloudSpecIdsToRefetch }) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Refetch any cloudSpec entries that are in an Error state |
| 87 | + */ |
| 88 | + const refetchFailedCloudData = async () => { |
| 89 | + const latestRunsIds = allSpecs |
| 90 | + .map((s) => s?.cloudSpec) |
| 91 | + .filter((cloudSpec): cloudSpec is NonNullCloudSpec => Boolean(cloudSpec?.fetchingStatus === 'ERRORED')) |
| 92 | + .map((cloudSpec) => cloudSpec.id) ?? [] |
| 93 | + |
| 94 | + await refetchMutation.executeMutation({ ids: [...latestRunsIds] }) |
| 95 | + } |
| 96 | + |
| 97 | + const displayedSpecIds = computed(() => displayedSpecs.value.map((v) => v?.cloudSpec?.id).filter((id) => !!id).join('|')) |
| 98 | + const debouncedDisplayedSpecIds = useDebounce(displayedSpecIds, 200) |
| 99 | + |
| 100 | + /* |
| 101 | + Automatically trigger refresh & purge on the following: |
| 102 | + - Set of displayed specs changes (scroll thru virtualized list) |
| 103 | + - Network connectivity state changes |
| 104 | + - Project connectivity state changes |
| 105 | + - Latest update timestamp for cloud project changes |
| 106 | + */ |
| 107 | + watch( |
| 108 | + [debouncedDisplayedSpecIds, isOffline, isProjectDisconnected, mostRecentUpdate], |
| 109 | + () => { |
| 110 | + fetchDisplayedCloudData() |
| 111 | + }, |
| 112 | + { flush: 'post' }, |
| 113 | + ) |
| 114 | + |
| 115 | + return { |
| 116 | + fetchDisplayedCloudData, |
| 117 | + refetchFailedCloudData, |
| 118 | + } |
| 119 | +} |
0 commit comments