overleaf/services/web/frontend/js/shared/hooks/use-callback-handlers.js
Timothée Alby 8ca159b4b9 Merge pull request #5797 from overleaf/ta-pdf-detach
PDF Detach

GitOrigin-RevId: f69d8a87d1ba2115ad496a719106dfc7707a6ed5
2021-11-16 09:02:57 +00:00

33 lines
674 B
JavaScript

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