(options: UseLifecycleEffectOptions)
| 25 | } |
| 26 | |
| 27 | export default function useLifecycleEffect(options: UseLifecycleEffectOptions): void { |
| 28 | const { addFailure, controls, emitEvent, previousState, props, state, step, store } = options; |
| 29 | const { action, index, lifecycle, positioned, scrolling, size, status } = state; |
| 30 | |
| 31 | const previousStep = usePrevious(step) ?? null; |
| 32 | |
| 33 | const lastAction = useRef<Actions | null>(null); |
| 34 | const propsRef = useRef(props); |
| 35 | const stateRef = useRef(state); |
| 36 | const previousStateRef = useRef(previousState); |
| 37 | const stepRef = useRef(step); |
| 38 | const previousStepRef = useRef(previousStep); |
| 39 | const controlsRef = useRef(controls); |
| 40 | const pollingRef = useRef<ReturnType<typeof setInterval> | null>(null); |
| 41 | const pollingTargetRef = useRef<StepTarget | null>(null); |
| 42 | const beforeRef = useRef<{ cancel: () => void } | null>(null); |
| 43 | |
| 44 | propsRef.current = props; |
| 45 | stateRef.current = state; |
| 46 | previousStateRef.current = previousState; |
| 47 | stepRef.current = step; |
| 48 | previousStepRef.current = previousStep; |
| 49 | controlsRef.current = controls; |
| 50 | |
| 51 | const cleanup = () => { |
| 52 | if (pollingRef.current) { |
| 53 | clearInterval(pollingRef.current); |
| 54 | pollingRef.current = null; |
| 55 | } |
| 56 | |
| 57 | pollingTargetRef.current = null; |
| 58 | |
| 59 | if (beforeRef.current) { |
| 60 | beforeRef.current.cancel(); |
| 61 | beforeRef.current = null; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | // Effect 1: Action tracking |
| 66 | useEffect(() => { |
| 67 | if (!previousStateRef.current) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | const { hasChangedTo } = treeChanges(stateRef.current, previousStateRef.current); |
| 72 | |
| 73 | const isAfterAction = hasChangedTo('action', [ |
| 74 | ACTIONS.NEXT, |
| 75 | ACTIONS.PREV, |
| 76 | ACTIONS.SKIP, |
| 77 | ACTIONS.CLOSE, |
| 78 | ACTIONS.REPLAY, |
| 79 | ]); |
| 80 | |
| 81 | const isStaleAfterStart = |
| 82 | action === ACTIONS.START && |
| 83 | (lastAction.current === ACTIONS.CLOSE || lastAction.current === ACTIONS.REPLAY); |
| 84 |
no test coverage detected
searching dependent graphs…