All files init.ts

71.43% Statements 10/14
40% Branches 4/10
33.33% Functions 1/3
71.43% Lines 10/14
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 351x   1x 1x               1x   11x 11x   11x   11x                     11x   11x      
import * as Immutable from 'seamless-immutable'
 
import { State } from './state'
import { Action } from './action'
 
export interface InitOptions {
  hotLoad?: boolean
  showError?: boolean
  domain?: string
}
 
export function initialize(initialState: any, options?: InitOptions) {
 
  options = options || { domain: 'default' }
  Action.showError = options.showError
 
  const cacheKey = `statex-cache:${options.domain}`
 
  Iif (options.hotLoad) {
    // for dev builds
    State.next(Immutable.from(JSON.parse(
      localStorage.getItem(cacheKey) || 'null'
    ) || initialState))
 
    State.subscribe(
      state => localStorage.setItem(cacheKey, JSON.stringify(state)),
      error => console.error(error), undefined
    )
 
  } else Eif (initialState != undefined) {
    // for production
    State.next(Immutable.from(initialState))
  }
 
}