import { useRef } from 'react' import { event, mutable } from './index' import type { MutableArgs, RefEvent } from './types' export * from './index' export const useOnce = (fun: () => T) => { const memo = useRef(void 0 as T) return memo.current ?? (memo.current = fun()) } export const useMutable = (...args: MutableArgs) => { const memo = useOnce(() => mutable()) return memo(...args) } export const useEvent = (...args: MutableArgs) => { const memo = useMutable(...args) as T return useOnce(() => event(memo)) } export const useRefEvent = (...args: MutableArgs) => { const memo = useMutable(...args) as T return useOnce(() => { const self = event({ ...memo, ref: (el: Element) => { if (el) { self.mount?.((self.target = el)) } else self.clean?.(null) }, }) as RefEvent return self }) } export default useEvent