Skip to content

Commit

Permalink
feat(useQuery): rewrite to use auto-tracked state to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Aug 15, 2024
1 parent 972264a commit 72adce9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/use-query/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export function App() {
function Demo1() {
const [count, actions] = useCounter(0)
const { run, data, loading, error } = useQuery(wait, { refreshDependencies: [count] })

return (
<>
<KeyValue label="Data" value={error ? 'Error!' : loading ? 'Loading...' : data ?? 'Not loaded'} />
<Zone>
<Button mono disabled={loading} onClick={() => run()}>
{data}
<Button mono onClick={() => run()}>
run()
</Button>
<Button mono onClick={() => actions.inc()}>
Expand Down
4 changes: 2 additions & 2 deletions src/use-query/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ export function useQuery<T extends AnyFunc, D = Awaited<ReturnType<T>>, E = any>

const service = useLoadingSlowFn<T, D, E>(
useRetryFn<T, E>(
(async (...args) => {
((...args) => {
const prePromise = cacheActions.getPromiseCache()
if (prePromise) return prePromise
const promise = latest.current.fetcher(...args)
cacheActions.setPromiseCache(promise)
return await promise
return promise
}) as T,
{
count: options.errorRetryCount,
Expand Down

0 comments on commit 72adce9

Please sign in to comment.