All files / react createGlobalState.ts

100% Statements 43/43
91.67% Branches 11/12
100% Functions 8/8
100% Lines 41/41

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                                                                                  21x 5x 5x 16x 4x 4x   12x 12x     21x           21x 28x     21x 25x 2x   25x 25x 25x 25x         21x 5x 1x   5x       5x 5x 5x             21x 20x             21x 1x 1x   21x 21x 44x 42x 42x             21x 21x 21x 21x 21x 21x    
import { Dispatch, SetStateAction, useEffect, useState } from 'react'
import { EventBus } from '../utils'
 
export type CreateGlobalStateState = any
 
export type CreateGlobalStateCustomResult<
  S extends CreateGlobalStateState | undefined,
  R = never
> = (payload: {
  state: CreateGlobalStateResultResult<S, never>[0]
  setState: CreateGlobalStateResultResult<S, never>[1]
}) => R
 
export type CreateGlobalStateResultResult<
  S extends CreateGlobalStateState | undefined,
  R = never
> = [R] extends [never] ? readonly [S, Dispatch<SetStateAction<S>>] : R
 
export interface CreateGlobalStateResult<
  S extends CreateGlobalStateState | undefined,
  R = never
> {
  (): CreateGlobalStateResultResult<S, R>
  getState(): S
  setState(nextState: SetStateAction<S>): void
  setStatePartial(nextState: Partial<S> | ((prevState: S) => Partial<S>)): void
  watchState(callback: (nextState: S, prevState: S) => any): () => void
  watchStateImmediate(callback: (nextState: S, prevState: S) => any): () => void
}
 
export function createGlobalState<S extends CreateGlobalStateState, R = never>(
  initialState: S,
): CreateGlobalStateResult<S, R>
 
export function createGlobalState<S extends CreateGlobalStateState, R = never>(
  customResult?: CreateGlobalStateCustomResult<S, R>,
): CreateGlobalStateResult<S | undefined, R>
 
export function createGlobalState<S extends CreateGlobalStateState, R = never>(
  initialState: S,
  customResult?: CreateGlobalStateCustomResult<S, R>,
): CreateGlobalStateResult<S, R>
 
export function createGlobalState<S extends CreateGlobalStateState, R = never>(
  _initialState?: S,
  _customResult?: CreateGlobalStateCustomResult<S | undefined, R>,
): CreateGlobalStateResult<S | undefined, R> {
  let initialState: S | undefined
  let customResult: CreateGlobalStateCustomResult<S | undefined, R> | undefined
  if (typeof _customResult === 'function') {
    initialState = _initialState
    customResult = _customResult
  } else if (typeof _initialState === 'function') {
    initialState = undefined
    customResult = _initialState as any
  } else {
    initialState = _initialState
    customResult = undefined
  }
 
  const bus = new EventBus<{
    setGlobalState: (
      nextGlobalState: S | undefined,
      prevGlobalState: S | undefined,
    ) => void
  }>()
  let currentGlobalState: S | undefined = initialState
  const getGlobalState: () => S | undefined = () => currentGlobalState
  const setGlobalState: Dispatch<
    SetStateAction<S | undefined>
  > = nextGlobalState => {
    if (typeof nextGlobalState === 'function') {
      nextGlobalState = (nextGlobalState as any)(currentGlobalState)
    }
    if (nextGlobalState !== currentGlobalState) {
      const prevGlobalState = currentGlobalState
      currentGlobalState = nextGlobalState as any
      bus.emit('setGlobalState', nextGlobalState as any, prevGlobalState)
    }
  }
  const setGlobalStatePartial: Dispatch<
    SetStateAction<Partial<S> | undefined>
  > = nextGlobalState => {
    if (typeof nextGlobalState === 'function') {
      nextGlobalState = (nextGlobalState as any)(currentGlobalState)
    }
    nextGlobalState = {
      ...(currentGlobalState as any),
      ...nextGlobalState,
    }
    const prevGlobalState = currentGlobalState
    currentGlobalState = nextGlobalState as any
    bus.emit('setGlobalState', nextGlobalState as any, prevGlobalState)
  }
  const watchGlobalState: (
    callback: (
      nextGlobalState: S | undefined,
      prevGlobalState: S | undefined,
    ) => any,
  ) => () => void = callback => {
    return bus.on('setGlobalState', callback)
  }
  const watchGlobalStateImmediate: (
    callback: (
      nextGlobalState: S | undefined,
      prevGlobalState: S | undefined,
    ) => any,
  ) => () => void = callback => {
    callback(initialState, undefined)
    return bus.on('setGlobalState', callback)
  }
  const useCustomResult = typeof customResult === 'function'
  const useGlobalState: CreateGlobalStateResult<S | undefined, R> = (() => {
    const [state, setState] = useState(currentGlobalState)
    useEffect(() => watchGlobalState(setState), [])
    return useCustomResult
      ? customResult!({
          state: state,
          setState: setGlobalState,
        })
      : [state, setGlobalState]
  }) as any
  useGlobalState.getState = getGlobalState
  useGlobalState.setState = setGlobalState
  useGlobalState.setStatePartial = setGlobalStatePartial
  useGlobalState.watchState = watchGlobalState
  useGlobalState.watchStateImmediate = watchGlobalStateImmediate
  return useGlobalState
}