mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
8ca159b4b9
PDF Detach GitOrigin-RevId: f69d8a87d1ba2115ad496a719106dfc7707a6ed5
33 lines
674 B
JavaScript
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 }
|
|
}
|