chore: add abort controller

This commit is contained in:
kawamataryo
2024-10-19 23:09:24 +09:00
parent aaf28b0c95
commit 90d991b76e
3 changed files with 32 additions and 2 deletions

View File

@@ -151,12 +151,20 @@ export const useRetrieveBskyUsers = () => {
[],
);
const abortControllerRef = React.useRef<AbortController | null>(null);
const startRetrieveLoop = React.useCallback(
async (queryParam: string) => {
abortControllerRef.current = new AbortController();
const signal = abortControllerRef.current.signal;
let isBottomReached = false;
let index = 0;
while (!isBottomReached) {
if (signal.aborted) {
break;
}
const data = detectXUsers(queryParam).filter((u) => {
return !detectedXUsers.some(
(t) => t.twAccountName === u.twAccountName,
@@ -191,6 +199,12 @@ export const useRetrieveBskyUsers = () => {
[retrieveBskyUsers, detectedXUsers],
);
const stopRetrieveLoop = () => {
if (abortControllerRef.current) {
abortControllerRef.current.abort();
}
};
const initialize = React.useCallback(
async ({
identifier,
@@ -283,5 +297,6 @@ export const useRetrieveBskyUsers = () => {
matchTypeFilter,
changeMatchTypeFilter,
filteredUsers,
stopRetrieveLoop,
};
};