Skip to content

Commit 0e34040

Browse files
committed
remove legacy undocumented useAsyncFetchMore
1 parent a521b83 commit 0e34040

File tree

1 file changed

+0
-81
lines changed

1 file changed

+0
-81
lines changed

src/index.ts

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -393,84 +393,3 @@ export const useAsyncCallback = <
393393
}
394394
);
395395
};
396-
397-
export const useAsyncFetchMore = <R, Args extends any[]>({
398-
value,
399-
fetchMore,
400-
merge,
401-
isEnd: isEndFn,
402-
}: {
403-
value: UseAsyncReturn<R, Args>;
404-
fetchMore: (result: R) => Promise<R>;
405-
merge: (result: R, moreResult: R) => R;
406-
isEnd: (moreResult: R) => boolean;
407-
}) => {
408-
const getAsyncValue = useGetter(value);
409-
const [isEnd, setIsEnd] = useState(false);
410-
411-
// TODO not really fan of this id thing, we should find a way to support cancellation!
412-
const fetchMoreId = useRef(0);
413-
414-
const fetchMoreAsync = useAsyncCallback(async () => {
415-
const freshAsyncValue = getAsyncValue();
416-
if (freshAsyncValue.status !== 'success') {
417-
throw new Error(
418-
"Can't fetch more if the original fetch is not a success"
419-
);
420-
}
421-
if (fetchMoreAsync.status === 'loading') {
422-
throw new Error(
423-
"Can't fetch more, because we are already fetching more!"
424-
);
425-
}
426-
427-
fetchMoreId.current = fetchMoreId.current + 1;
428-
const currentId = fetchMoreId.current;
429-
const moreResult = await fetchMore(freshAsyncValue.result!);
430-
431-
// TODO not satisfied with this, we should just use "freshAsyncValue === getAsyncValue()" but asyncValue is not "stable"
432-
const isStillSameValue =
433-
freshAsyncValue.status === getAsyncValue().status &&
434-
freshAsyncValue.result === getAsyncValue().result;
435-
436-
const isStillSameId = fetchMoreId.current === currentId;
437-
438-
// Handle race conditions: we only merge the fetchMore result if the initial async value is the same
439-
const canMerge = isStillSameValue && isStillSameId;
440-
if (canMerge) {
441-
value.merge({
442-
result: merge(value.result!, moreResult),
443-
});
444-
if (isEndFn(moreResult)) {
445-
setIsEnd(true);
446-
}
447-
}
448-
449-
// return is useful for chaining, like fetchMore().then(result => {});
450-
return moreResult;
451-
});
452-
453-
const reset = () => {
454-
fetchMoreAsync.reset();
455-
setIsEnd(false);
456-
};
457-
458-
// We only allow to fetch more on a stable async value
459-
// If that value change for whatever reason, we reset the fetchmore too (which will make current pending requests to be ignored)
460-
// TODO value is not stable, we could just reset on value change otherwise
461-
const shouldReset = value.status !== 'success';
462-
useEffect(() => {
463-
if (shouldReset) {
464-
reset();
465-
}
466-
}, [shouldReset]);
467-
468-
return {
469-
canFetchMore:
470-
value.status === 'success' && fetchMoreAsync.status !== 'loading',
471-
loading: fetchMoreAsync.loading,
472-
status: fetchMoreAsync.status,
473-
fetchMore: fetchMoreAsync.execute,
474-
isEnd,
475-
};
476-
};

0 commit comments

Comments
 (0)