Skip to content

Commit 3afbc40

Browse files
Implements dependencies options (#1)
* Implement dependencies option * ICachedFetchDefaultOptions with no optional properties
1 parent 12712ad commit 3afbc40

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/cachedFetch.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import {
55
useReducer,
66
useState
77
} from "react";
8-
import { CachedFetchContext, ICachedFetchOptions } from "./cachedFetchProvider";
8+
import {
9+
CachedFetchContext,
10+
ICachedFetchDefaultOptions,
11+
ICachedFetchOptions
12+
} from "./cachedFetchProvider";
913

1014
interface ICachedFetchReducerState {
1115
isLoading: boolean;
@@ -48,17 +52,39 @@ export const useCachedFetch = (
4852
const { cache, updateCache, globalOptions }: any = useContext(
4953
CachedFetchContext
5054
);
51-
const unifiedOptions = { ...globalOptions, ...options };
52-
const [headers] = useState(unifiedOptions.headers);
55+
const unifiedOptions: ICachedFetchDefaultOptions = {
56+
...globalOptions,
57+
...options
58+
};
59+
const [headers] = useState<Headers>(unifiedOptions.headers);
5360
const [state, dispatch] = useReducer(cachedFetchReducer, {
5461
isLoading: false,
5562
hasError: false
5663
});
5764
const [shouldRefresh, setShouldRefresh] = useState(false);
65+
const [isWaitingForDependencies, setIsWaitingForDependencies] = useState(
66+
true
67+
);
5868

5969
const fetchCallback = useCallback(unifiedOptions.fetcher, []);
6070

6171
useEffect(() => {
72+
if (
73+
unifiedOptions.dependencies &&
74+
unifiedOptions.dependencies.some((dependency: boolean) => !dependency)
75+
) {
76+
setIsWaitingForDependencies(true);
77+
return;
78+
}
79+
80+
setIsWaitingForDependencies(false);
81+
}, [unifiedOptions.dependencies]);
82+
83+
useEffect(() => {
84+
if (isWaitingForDependencies) {
85+
return;
86+
}
87+
6288
let mounted = true;
6389

6490
const fetchData = async () => {
@@ -84,7 +110,15 @@ export const useCachedFetch = (
84110
return () => {
85111
mounted = false;
86112
};
87-
}, [route, dispatch, updateCache, fetchCallback, shouldRefresh, headers]);
113+
}, [
114+
route,
115+
dispatch,
116+
updateCache,
117+
fetchCallback,
118+
shouldRefresh,
119+
headers,
120+
isWaitingForDependencies
121+
]);
88122

89123
const refresh = () => {
90124
setShouldRefresh((current: Boolean) => !current);

src/cachedFetchProvider.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ export interface ICachedFetchOptions {
99
headers?: Headers;
1010
fetcher?: (route: string, headers: Headers) => any;
1111
initialValue?: any;
12+
dependencies?: boolean[];
13+
}
14+
15+
export interface ICachedFetchDefaultOptions {
16+
headers: Headers;
17+
fetcher: (route: string, headers: Headers) => any;
18+
initialValue: any;
19+
dependencies: boolean[];
1220
}
1321

1422
interface ICachedFetchProviderProps {
@@ -26,7 +34,8 @@ const defaultOptions = {
2634
headers: {
2735
method: "GET"
2836
},
29-
initialValue: undefined
37+
initialValue: undefined,
38+
dependencies: []
3039
};
3140

3241
export const CachedFetchProvider: FunctionComponent<ICachedFetchProviderProps> = ({

0 commit comments

Comments
 (0)