All files index.js

79.41% Statements 27/34
70.59% Branches 12/17
66.67% Functions 12/18
79.17% Lines 19/24

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      1x       1x 1x 1x         1x     2x   2x 2x 2x 2x 1x   1x   1x         1x   3x             1x   1x 1x     2x                        
import React from 'react'
 
export default function create(fn) {
  let state = {
    listeners: [],
    current: fn(
      merge => {
        Eif (typeof merge === 'function') merge = merge(state.current)
        state.current = Object.assign({}, state.current, merge)
        state.listeners.forEach(listener => listener(state.current))
      },
      () => state.current
    ),
  }
  return [
    // useStore
    (selector, dependencies) => {
      let selected = selector ? selector(state.current) : state.current
      // Using functional initial b/c selected itself could be a function
      const [slice, set] = React.useState(() => selected)
      const sliceRef = React.useRef()
      React.useEffect(() => void (sliceRef.current = slice), [slice])
      React.useEffect(() => {
        const ping = () => {
          // Get fresh selected state
          let selected = selector ? selector(state.current) : state.current
          // If state is not equal from the get go and not an atomic then shallow equal it
          Eif (
            sliceRef.current !== selected &&
            typeof selected === 'object' &&
            !Array.isArray(selected)
          ) {
            selected = Object.entries(selected).reduce(
              (acc, [key, value]) =>
                sliceRef.current[key] !== value
                  ? Object.assign({}, acc, { [key]: value })
                  : acc,
              sliceRef.current
            )
          }
          // Refresh local slice, functional initial b/c selected itself could be a function
          Eif (sliceRef.current !== selected) set(() => selected)
        }
        state.listeners.push(ping)
        return () => (state.listeners = state.listeners.filter(i => i !== ping))
      }, dependencies || [selector])
      // Returning the selected state slice
      return selected
    },
    {
      subscribe: fn => {
        state.listeners.push(fn)
        return () => (state.listeners = state.listeners.filter(i => i !== fn))
      },
      getState: () => state.current,
      destroy: () => ((state.listeners = []), (state.current = {})),
    },
  ]
}