refactor(api): implement abort signal handling in API requests across multiple components to improve cancellation of ongoing requests and prevent memory leaks
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 2m12s
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 2m12s
This commit is contained in:
@@ -114,13 +114,23 @@ function IPRangesManager() {
|
||||
const [wsUrl, setWsUrl] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
(async () => { try { const r = await api.get('/ws/url'); setWsUrl(String(r.data?.url || '')); } catch {} })();
|
||||
const controller = new AbortController();
|
||||
(async () => {
|
||||
try {
|
||||
const r = await api.get('/ws/url', { signal: controller.signal });
|
||||
setWsUrl(String(r.data?.url || ''));
|
||||
} catch (e) {
|
||||
if (e?.name === 'CanceledError' || e?.name === 'AbortError' || e?.code === 'ERR_CANCELED') return;
|
||||
}
|
||||
})();
|
||||
return () => controller.abort();
|
||||
}, []);
|
||||
|
||||
const fetchItems = async () => {
|
||||
const fetchItems = async (abortSignal) => {
|
||||
setLoading(true);
|
||||
const opts = abortSignal ? { signal: abortSignal } : {};
|
||||
try {
|
||||
const response = await api.get(`/ip-ranges`, { params: { offset: 0, limit: 0, format: 'std' } });
|
||||
const response = await api.get(`/ip-ranges`, { params: { offset: 0, limit: 0, format: 'std' }, ...opts });
|
||||
const payload = Array.isArray(response.data?.items) ? response.data.items : [];
|
||||
const total = payload.length;
|
||||
setItems(payload);
|
||||
@@ -132,6 +142,7 @@ function IPRangesManager() {
|
||||
setContentLength(typeof lengthHeader !== 'undefined' ? Number(lengthHeader) : null);
|
||||
setError('');
|
||||
} catch (error) {
|
||||
if (error?.name === 'CanceledError' || error?.name === 'AbortError' || error?.code === 'ERR_CANCELED') return;
|
||||
console.error('Error fetching ip-ranges:', error);
|
||||
setError('Не удалось загрузить IP-диапазоны. Проверьте, запущен ли бэкенд.');
|
||||
} finally {
|
||||
@@ -139,7 +150,14 @@ function IPRangesManager() {
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => { if (!didInit.current) { didInit.current = true; fetchItems(); } }, []);
|
||||
useEffect(() => {
|
||||
if (!didInit.current) {
|
||||
didInit.current = true;
|
||||
const controller = new AbortController();
|
||||
fetchItems(controller.signal);
|
||||
return () => controller.abort();
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleAddItem = (item) => {
|
||||
const ipRange = item?.ipRange || newItem.ipRange;
|
||||
|
||||
Reference in New Issue
Block a user