mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-03 22:48:03 -05:00
34 lines
674 B
JavaScript
34 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 }
|
||
|
}
|