# All files / src/_hooks Timer.tsx

95.23% Statements 20/21
62.5% Branches 5/8
88.88% Functions 8/9
94.73% Lines 18/19

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
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    10162x

    62x
    4255x

    4255x

    382x

    81x
    81x

    62x
    1x

    1x

    1x

    186x
    1x

    62x

    62x
    1x

    62x
    2x

     import React from 'react'

    /* TYPES */

    interface TimerConfig {}

    interface Props {
      children: React.ReactNode
    }

    type TimerId = number | null

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface TimerApi {
      cancel: () => void
      reset: () => void
      setTimer: (callback: () => void, duration: number) => void
      timerId: TimerId
    }

    /* HOOK */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function useTimer(config: TimerConfig): TimerApi {
      const timer = React.useRef<TimerId>(null)

      return {
        cancel: () => {
          timer.current && clearTimeout(timer.current)
        },
        reset: () => {
          timer.current && clearTimeout(timer.current)
        },
        setTimer: (callback, duration) => {
          timer.current && clearTimeout(timer.current)
          timer.current = window.setTimeout(callback, duration)
        },
        timerId: timer.current,
      }
    }

    /* RENDER PROP */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function Timer({
      children,
    }: {
      children: (timer: TimerApi) => unknown
    }) {
      const timer = useTimer({})

      return typeof children === 'function' ? children(timer) : children
    }

    /* CONTEXT */

    const noop = (method: string) => () =>
      console.warn(
        `Could not call \`${method}\`, please wrap your component in a \`<TimerProvider>\``
      )

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const TimerContext = React.createContext<TimerApi>({
      cancel: noop('cancel'),
      reset: noop('reset'),
      setTimer: noop('setTimer'),
      timerId: null,
    })

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function TimerProvider({ children }: Props) {
      return (
        <TimerContext.Provider value={useTimer({})}>
          {children}
        </TimerContext.Provider>
      )
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function useTimerContext() {
      return React.useContext(TimerContext)
    }