oceanprotocol/market

View on GitHub
src/@hooks/useAbortController.ts

Summary

Maintainability
A
0 mins
Test Coverage
F
0%
import { useRef, useEffect, useCallback } from 'react'
export const useAbortController = (): (() => AbortSignal) => {
  const axiosSource = useRef(null)
  const newAbortController = useCallback(() => {
    axiosSource.current = new AbortController()
    return axiosSource.current.signal
  }, [])

  useEffect(
    () => () => {
      if (axiosSource.current) axiosSource.current.abort()
    },
    []
  )

  return newAbortController
}