2022-03-31 07:22:36 -04:00
|
|
|
import { useCallback, useRef } from 'react'
|
2021-11-15 11:33:57 -05:00
|
|
|
|
|
|
|
export default function useCallbackHandlers() {
|
2022-03-31 07:22:36 -04:00
|
|
|
const handlersRef = useRef(new Set())
|
2021-11-15 11:33:57 -05:00
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const addHandler = useCallback(handler => {
|
|
|
|
handlersRef.current.add(handler)
|
|
|
|
}, [])
|
2021-11-15 11:33:57 -05:00
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const deleteHandler = useCallback(handler => {
|
|
|
|
handlersRef.current.delete(handler)
|
|
|
|
}, [])
|
2021-11-15 11:33:57 -05:00
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
const callHandlers = useCallback((...args) => {
|
|
|
|
for (const handler of handlersRef.current) {
|
|
|
|
handler(...args)
|
|
|
|
}
|
|
|
|
}, [])
|
2021-11-15 11:33:57 -05:00
|
|
|
|
|
|
|
return { addHandler, deleteHandler, callHandlers }
|
|
|
|
}
|