All files / src update.js

87.85% Statements 94/107
74.07% Branches 20/27
87.5% Functions 14/16
88.06% Lines 59/67

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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171  1x 1x   1x 1x 1x   1x   1x 6x               1x 5x         5x 1x     4x 4x 4x 2x           2x         5x 5x 5x   5x         1x             8x         1x 1x 1x     1x         1x 1x 1x                     2x 2x     2x 2x     2x 2x           3x 3x       3x           1x                   1x 1x 1x 1x 1x             1x 1x 1x 2x 2x       1x 1x 1x                 1x 1x 1x 1x 1x                   1x      
 
import { safe, compress } from 'raid-addons'
import { isNull } from 'lodash/fp'
 
import { actions } from './actions'
import { getHistory } from './history'
import { getRouteIndex, getCurrentRoute } from './utils'
 
export const DEFAULT_KEY = 'navigation'
 
const getStorage = storage => {
  return isNull(storage)
    ? null
    : storage || (window && window.sessionStorage)
}
 
/**
 * Initial state root
 */
const getInitialState = (storage, history) => {
  let initial = {
    stack: [getCurrentRoute(null, history)],
    index: 0
  }
 
  if (!storage) {
    return initial
  }
 
  try {
    let stored = storage.getItem(DEFAULT_KEY)
    if (stored) {
      return JSON.parse(stored)
    }
  } catch (err) {
    return initial
  }
 
  return initial
}
 
// const initialState = (storage, history) =>
 
export const setInitial = ({ key, storage, history }) => {
  storage = getStorage(storage)
  key = key || DEFAULT_KEY
 
  return {
    [key]: getInitialState(storage, history)
  }
}
 
export const initial = setInitial({
  key: DEFAULT_KEY
})
 
/**
 * Get state root selector function
 */
export const selector = (key = DEFAULT_KEY) => state => state[key]
 
/**
 * Popper
 */
const pop = ({ key, get, state, location, history }) => {
  const { index, stack } = get(state)
  let routeIndex = getRouteIndex(location, stack)
 
  // If the route was found then use it
  Iif (routeIndex >= 0) {
    state[key].index = routeIndex
    return
  }
 
  Eif (index === 0) {
    state[key].stack.unshift(getCurrentRoute(location))
    return
  }
 
  if (index === stack.length - 1) {
    console.warn('Attempting to navigate forward but no route exists')
  }
}
 
/**
 * pusher
 */
const push = ({ key, get, state, location, history }) => {
  const { index, stack } = get(state)
 
  // If we're pushing in to the middle next nuke end of stack
  Eif (index < stack.length - 1) {
    state[key].stack = stack.slice(0, index + 1)
  }
 
  state[key].index = index + 1
  state[key].stack.push(getCurrentRoute(location))
}
 
/**
 * Storage
 */
const save = ({ storage, key, state, get }) => {
  Iif (!storage) {
    return
  }
 
  storage.setItem(key, JSON.stringify(get(state)))
}
 
/**
 * Setup
 */
const init = ({ state, storage, history, key, get }) => {
  return {
    ...state,
    ...setInitial({ key, storage, history })
  }
}
 
/**
 * Updates the state based on a navigation pop event
 */
const createUpdatePop = ({ key, history, storage }) => {
  const get = selector(key)
  return safe((state, payload) => {
    pop({ key, get, state, location: payload.location, history })
    save({ storage, key, get, state })
  })
}
 
/**
 * Updates the state based on a navigation push event
 */
const createUpdatePush = ({ key, history, storage }) => {
  const get = selector(key)
  return safe((state, payload) => {
    push({ key, get, state, location: payload.location, history })
    save({ storage, key, get, state })
  })
}
 
const createInit = ({ key, storage, history }) => {
  const get = selector(key)
  return safe((state, payload) => {
    return init({ key, storage, history, state, get })
  })
}
 
/**
 * Can be used to create an update function that refs a defined root and
 * can be supplied with a specific history instance
 */
export const createUpdate = ({ key, history, storage }) => {
  key = key || DEFAULT_KEY
  history = getHistory(history)
  storage = getStorage(storage)
  return compress({
    [actions.pop]: createUpdatePop({ key, history, storage }),
    [actions.push]: createUpdatePush({ key, history, storage }),
    [actions.init]: createInit({ key, history, storage })
  })
}
 
/**
 * Update function to register with raid
 */
export const update = createUpdate({
  key: DEFAULT_KEY
})