1- import { useState , useMemo , useCallback } from 'react' ;
1+ import { useState , useMemo , useCallback , useRef , useEffect } from 'react' ;
22import { CatalogItem } from '@console/dynamic-plugin-sdk/src/extensions/catalog' ;
33import { consoleFetch } from '@console/dynamic-plugin-sdk/src/lib-core' ;
44import { getConsoleRequestHeaders } from '@console/dynamic-plugin-sdk/src/utils/fetch' ;
@@ -18,6 +18,7 @@ const useCatalogItems: UseCatalogItems = () => {
1818 const [ loading , setLoading ] = useState ( true ) ;
1919 const [ error , setError ] = useState ( '' ) ;
2020 const [ lastModified , setLastModified ] = useState ( '' ) ;
21+ const abortControllerRef = useRef < AbortController > ( ) ;
2122
2223 const headers = useMemo ( ( ) => {
2324 const consoleHeaders = getConsoleRequestHeaders ( ) ;
@@ -28,9 +29,11 @@ const useCatalogItems: UseCatalogItems = () => {
2829 } ;
2930 } , [ lastModified ] ) ;
3031
31- // Fetch function that only updates state on 200 responses
3232 const fetchItems = useCallback ( ( ) => {
33- consoleFetch ( '/api/olm/catalog-items/' , { headers } )
33+ abortControllerRef . current ?. abort ( ) ;
34+ abortControllerRef . current = new AbortController ( ) ;
35+
36+ consoleFetch ( '/api/olm/catalog-items/' , { headers, signal : abortControllerRef . current . signal } )
3437 . then ( ( response ) => {
3538 if ( response . status === 304 ) {
3639 return null ;
@@ -50,11 +53,18 @@ const useCatalogItems: UseCatalogItems = () => {
5053 setLoading ( false ) ;
5154 } )
5255 . catch ( ( err ) => {
56+ if ( err . name === 'AbortError' ) return ;
5357 setError ( err . toString ( ) ) ;
5458 setLoading ( false ) ;
5559 } ) ;
5660 } , [ headers ] ) ;
5761
62+ useEffect ( ( ) => {
63+ return ( ) => {
64+ abortControllerRef . current ?. abort ( ) ;
65+ } ;
66+ } , [ ] ) ;
67+
5868 usePoll ( fetchItems , 30 * ONE_SECOND ) ;
5969
6070 const items = useMemo ( ( ) => olmCatalogItems . map ( normalizeCatalogItem ) , [ olmCatalogItems ] ) ;
0 commit comments