-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify and rebalance caching/refetching
- Loading branch information
1 parent
6fe99ca
commit 8ccf8f9
Showing
11 changed files
with
42 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,22 @@ | ||
import { | ||
QueryClient, | ||
QueryObserverResult, | ||
useQuery, | ||
useQueryClient | ||
} from "@tanstack/react-query"; | ||
import { QueryObserverResult } from "@tanstack/react-query"; | ||
import { Server } from "../../models/server"; | ||
import Well from "../../models/well"; | ||
import WellService from "../../services/wellService"; | ||
import { QUERY_KEY_WELL } from "./queryKeys"; | ||
import { QueryOptions } from "./queryOptions"; | ||
import { getWellsQueryKey } from "./useGetWells"; | ||
import { useGetWells } from "./useGetWells"; | ||
|
||
export const getWellQueryKey = (serverUrl: string, wellUid: string) => { | ||
return [QUERY_KEY_WELL, serverUrl?.toLowerCase(), wellUid?.toLowerCase()]; | ||
}; | ||
|
||
const updatePartialWells = ( | ||
queryClient: QueryClient, | ||
server: Server, | ||
well: Well | ||
) => { | ||
const existingWells = queryClient.getQueryData<Well[]>( | ||
getWellsQueryKey(server?.url) | ||
); | ||
if (existingWells) { | ||
const existingWellIndex = existingWells.findIndex( | ||
(w) => w.uid === well.uid | ||
); | ||
existingWells[existingWellIndex] = well; | ||
queryClient.setQueryData<Well[]>( | ||
getWellsQueryKey(server?.url), | ||
existingWells | ||
); | ||
} | ||
}; | ||
|
||
export const wellQuery = ( | ||
queryClient: QueryClient, | ||
server: Server, | ||
wellUid: string, | ||
options?: QueryOptions | ||
) => ({ | ||
queryKey: getWellQueryKey(server?.url, wellUid), | ||
queryFn: async () => { | ||
const well = await WellService.getWell(wellUid, null, server); | ||
updatePartialWells(queryClient, server, well); | ||
return well; | ||
}, | ||
...options, | ||
enabled: !!server && !!wellUid && !(options?.enabled === false), | ||
gcTime: Infinity // We don't want to garbage collect wells | ||
}); | ||
|
||
type WellQueryResult = Omit<QueryObserverResult<Well, unknown>, "data"> & { | ||
well: Well; | ||
type WellQueryResult = Omit< | ||
QueryObserverResult<Well, unknown>, | ||
"data" | "refetch" | ||
> & { | ||
well: Well | undefined; | ||
}; | ||
|
||
export const useGetWell = ( | ||
server: Server, | ||
wellUid: string, | ||
options?: QueryOptions | ||
): WellQueryResult => { | ||
const queryClient = useQueryClient(); | ||
const { data, ...state } = useQuery<Well>( | ||
wellQuery(queryClient, server, wellUid, options) | ||
); | ||
return { well: data, ...state }; | ||
const { wells, ...state } = useGetWells(server, options); | ||
const well = wells?.find((w) => w.uid === wellUid); | ||
return { well, ...state }; | ||
}; |
Oops, something went wrong.