-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implement polling while asynchronous tool refresh is unresolved
- force refresh happens asynchronously from response. Must implement polling to ensure data displayed is up to date - removes the redundant `ToolReferenceService.getToolReferencesCategoryMap` method by extracting unique code and applying it as needed in components
- Loading branch information
1 parent
79c8cd6
commit c18c017
Showing
8 changed files
with
134 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useEffect, useState } from "react"; | ||
import useSWR from "swr"; | ||
|
||
import { ToolReferenceService } from "~/lib/service/api/toolreferenceService"; | ||
|
||
export function usePollSingleTool(toolId: string) { | ||
const [isPolling, setIsPolling] = useState(false); | ||
|
||
const { mutate: updateTools } = useSWR( | ||
isPolling ? ToolReferenceService.getToolReferences.key("tool") : null, | ||
({ type }) => ToolReferenceService.getToolReferences(type), | ||
{ fallbackData: [], revalidateIfStale: false } | ||
); | ||
|
||
const getTool = useSWR( | ||
isPolling ? ToolReferenceService.getToolReferenceById.key(toolId) : null, | ||
({ toolReferenceId }) => | ||
ToolReferenceService.getToolReferenceById(toolReferenceId), | ||
{ refreshInterval: 1000 } | ||
); | ||
|
||
useEffect(() => { | ||
if (!getTool.data) return; | ||
|
||
setIsPolling(!getTool.data.resolved); | ||
|
||
// resolved means async update is complete | ||
if (getTool.data.resolved) { | ||
updateTools( | ||
(tools) => { | ||
if (!getTool.data) return tools; | ||
if (!tools) return [getTool.data]; | ||
|
||
const index = tools.findIndex((tool) => tool.id === toolId); | ||
|
||
const copy = [...tools]; | ||
copy[index] = getTool.data; | ||
return copy; | ||
}, | ||
{ revalidate: false } | ||
); | ||
} | ||
}, [getTool.data, updateTools, toolId]); | ||
|
||
return { | ||
startPolling: () => setIsPolling(true), | ||
isPolling, | ||
}; | ||
} |
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