( getEvent: () => LogEvent, options?: UseLogEventOnceOptions, )
| 38 | * ); |
| 39 | */ |
| 40 | const useLogEventOnce = ( |
| 41 | getEvent: () => LogEvent, |
| 42 | options?: UseLogEventOnceOptions, |
| 43 | ): void => { |
| 44 | const { logEvent } = useLogContext(); |
| 45 | const hasLoggedRef = useRef(false); |
| 46 | const getEventRef = useRef(getEvent); |
| 47 | |
| 48 | getEventRef.current = getEvent; |
| 49 | |
| 50 | const { condition = true } = options ?? {}; |
| 51 | |
| 52 | useEffect(() => { |
| 53 | if (condition && !hasLoggedRef.current) { |
| 54 | hasLoggedRef.current = true; |
| 55 | logEvent(getEventRef.current()); |
| 56 | } |
| 57 | }, [condition, logEvent]); |
| 58 | }; |
| 59 | |
| 60 | export default useLogEventOnce; |
no test coverage detected