# All files / utils usePlotEnv.ts

86.67% Statements 13/15
100% Branches 2/2
50% Functions 1/2
84.62% Lines 11/13

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
    311x

    1x
    1x

    1x
    4x

    4x
    3x
    3x

    4x

    4x

    4x

     import {useState, useRef} from 'react'

    import {SizedConfig} from '../types'
    import {PlotEnv} from './PlotEnv'
    import {useMountedEffect} from './useMountedEffect'

    /*
      Get a `PlotEnv` for a plot.

      The `PlotEnv` will be instantiated if it doesn't already exist. If the passed
      `config` changes, it will be set on the `PlotEnv` and the component will
      rerender.
    */
    export const usePlotEnv = (config: SizedConfig): PlotEnv => {
      const envRef = useRef(null)

      if (envRef.current === null) {
        envRef.current = new PlotEnv()
        envRef.current.config = config
      }

      const [envRevision, setEnvRevision] = useState(0)

      useMountedEffect(() => {
        envRef.current.config = config
        setEnvRevision(envRevision + 1)
      }, [config])

      return envRef.current
    }