All files / core init.ts

58.33% Statements 7/12
42.86% Branches 6/14
33.33% Functions 1/3
70% Lines 7/10
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                            20x 20x   20x   20x       20x         20x   20x      
import Immutable from './immutable'
 
import { State } from './state'
import { Action } from './action'
 
export interface InitOptions {
  hotLoad?: boolean
  cache?: string
  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.cache != undefined) {
    throw new Error('statex:initialize: Option cache is not supported. Import initialize from statex/electron to use cache option')
  }
 
  Iif (options.hotLoad === true && typeof localStorage !== 'undefined') {
    // for dev builds in browser
    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))
  }
 
}