overleaf/services/web/frontend/js/shared/hooks/use-callback-handlers.ts
ilkin-overleaf d959dbc236 Merge pull request #11105 from overleaf/ii-shared-hooks-js-to-ts-conversion
[web] Shared React hooks JS to TS conversion

GitOrigin-RevId: 0ccdebff236c7424b1a73cd7d6646a9d01a20eb1
2023-01-10 09:05:52 +00:00

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 }
}