-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add progressive refetch interval and repeat invalidation (#22)
- Loading branch information
1 parent
1f13f2e
commit c472fae
Showing
17 changed files
with
357 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface InvalidateRepeatOptions { | ||
interval: number; | ||
/** | ||
* Number of repeated calls, not counting the first one | ||
*/ | ||
count: number; | ||
} | ||
|
||
export interface InvalidateOptions { | ||
repeat?: InvalidateRepeatOptions; | ||
} |
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,63 @@ | ||
import React from 'react'; | ||
|
||
import type {Query, QueryFunction, QueryFunctionContext, SkipToken} from '@tanstack/react-query'; | ||
|
||
import type {DataSourceError, DataSourceKey, DataSourceResponse} from '../../core'; | ||
import type {AnyQueryDataSource, RefetchInterval} from '../types'; | ||
|
||
export const useRefetchInterval = <TDataSource extends AnyQueryDataSource, TQueryData, TPageParams>( | ||
refetchIntervalOption?: RefetchInterval< | ||
DataSourceResponse<TDataSource>, | ||
DataSourceError<TDataSource>, | ||
TQueryData, | ||
DataSourceKey | ||
>, | ||
queryFnOption?: | ||
| QueryFunction<DataSourceResponse<TDataSource>, DataSourceKey, TPageParams> | ||
| SkipToken, | ||
): { | ||
refetchInterval?: | ||
| number | ||
| false | ||
| (( | ||
query: Query< | ||
DataSourceResponse<TDataSource>, | ||
DataSourceError<TDataSource>, | ||
TQueryData, | ||
DataSourceKey | ||
>, | ||
) => number | false | undefined); | ||
queryFn?: | ||
| QueryFunction<DataSourceResponse<TDataSource>, DataSourceKey, TPageParams> | ||
| SkipToken; | ||
} => { | ||
const count = React.useRef<number>(0); | ||
|
||
const queryFn = React.useMemo(() => { | ||
if (typeof queryFnOption === 'function') { | ||
return (context: QueryFunctionContext<DataSourceKey, TPageParams>) => { | ||
count.current++; | ||
return queryFnOption(context); | ||
}; | ||
} | ||
return undefined; | ||
}, [queryFnOption]); | ||
|
||
const refetchInterval = React.useMemo(() => { | ||
if (typeof refetchIntervalOption === 'function') { | ||
return ( | ||
query: Query< | ||
DataSourceResponse<TDataSource>, | ||
DataSourceError<TDataSource>, | ||
TQueryData, | ||
DataSourceKey | ||
>, | ||
) => { | ||
return refetchIntervalOption(query, count.current); | ||
}; | ||
} | ||
return refetchIntervalOption; | ||
}, [refetchIntervalOption]); | ||
|
||
return {refetchInterval, queryFn}; | ||
}; |
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
Oops, something went wrong.