overleaf/services/web/frontend/js/shared/hooks/use-callback-handlers.js

22 lines
519 B
JavaScript
Raw Normal View History

import { useCallback, useRef } from 'react'
export default function useCallbackHandlers() {
const handlersRef = useRef(new Set())
const addHandler = useCallback(handler => {
handlersRef.current.add(handler)
}, [])
const deleteHandler = useCallback(handler => {
handlersRef.current.delete(handler)
}, [])
const callHandlers = useCallback((...args) => {
for (const handler of handlersRef.current) {
handler(...args)
}
}, [])
return { addHandler, deleteHandler, callHandlers }
}