mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-14 20:40:17 -05:00
d959dbc236
[web] Shared React hooks JS to TS conversion GitOrigin-RevId: 0ccdebff236c7424b1a73cd7d6646a9d01a20eb1
21 lines
623 B
TypeScript
21 lines
623 B
TypeScript
import { useCallback, useRef } from 'react'
|
|
|
|
export default function useCallbackHandlers() {
|
|
const handlersRef = useRef(new Set<(...arg: unknown[]) => void>())
|
|
|
|
const addHandler = useCallback((handler: (...args: unknown[]) => void) => {
|
|
handlersRef.current.add(handler)
|
|
}, [])
|
|
|
|
const deleteHandler = useCallback((handler: (...args: unknown[]) => void) => {
|
|
handlersRef.current.delete(handler)
|
|
}, [])
|
|
|
|
const callHandlers = useCallback((...args: unknown[]) => {
|
|
for (const handler of handlersRef.current) {
|
|
handler(...args)
|
|
}
|
|
}, [])
|
|
|
|
return { addHandler, deleteHandler, callHandlers }
|
|
}
|