mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-14 16:00:58 -05:00
0cde5be165
Convert React context providers to TypeScript [don't squash!] GitOrigin-RevId: d92a91798286978410956ab791d73c17c5086d86
21 lines
607 B
TypeScript
21 lines
607 B
TypeScript
import { useCallback, useRef } from 'react'
|
|
|
|
export default function useCallbackHandlers() {
|
|
const handlersRef = useRef(new Set<(...arg: any[]) => void>())
|
|
|
|
const addHandler = useCallback((handler: (...args: any[]) => void) => {
|
|
handlersRef.current.add(handler)
|
|
}, [])
|
|
|
|
const deleteHandler = useCallback((handler: (...args: any[]) => void) => {
|
|
handlersRef.current.delete(handler)
|
|
}, [])
|
|
|
|
const callHandlers = useCallback((...args: any[]) => {
|
|
for (const handler of handlersRef.current) {
|
|
handler(...args)
|
|
}
|
|
}, [])
|
|
|
|
return { addHandler, deleteHandler, callHandlers }
|
|
}
|