All files / atom/videoPlayer/src/hooks/hls useTimeLimitCheck.js

47.82% Statements 11/23
8.33% Branches 1/12
66.66% Functions 4/6
47.61% Lines 10/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43    1x   1x 5x   5x 5x         5x                               5x 5x 5x       5x              
import {useCallback, useEffect, useRef} from 'react'
 
const ONE_SECOND = 1000
 
const useTimeLimitCheck = ({playerRef, timeLimit, timeOffset}) => {
  const checkTimeLimitInterval = useRef(null)
 
  const stopTimeLimitInterval = useCallback(() => {
    Eif (checkTimeLimitInterval.current === null) return
    clearInterval(checkTimeLimitInterval.current)
    checkTimeLimitInterval.current = null
  }, [checkTimeLimitInterval])
 
  const startTimeLimitInterval = useCallback(() => {
    stopTimeLimitInterval()
    if (timeLimit === undefined) return
 
    checkTimeLimitInterval.current = setInterval(() => {
      const currentTime = playerRef.current?.currentTime
      const isPlaying = !playerRef.current?.paused
      const isTimeLimitReached = currentTime >= timeLimit && timeLimit !== undefined
 
      if (isPlaying && isTimeLimitReached) {
        playerRef.current?.pause()
        playerRef.current.currentTime = timeOffset || 0
      }
    }, ONE_SECOND)
  }, [checkTimeLimitInterval, playerRef, stopTimeLimitInterval, timeLimit, timeOffset])
 
  useEffect(() => {
    return () => {
      stopTimeLimitInterval()
    }
  }, [stopTimeLimitInterval])
 
  return {
    startTimeLimitInterval,
    stopTimeLimitInterval
  }
}
 
export default useTimeLimitCheck