import { useState , useEffect , useLayoutEffect } from 'react' ; import { Storage } from './Storage' ; import { Store } from './Store' ; import { Dispatch , Action , keyType } from './types' ; import { emitStoreInit } from './emitter' ; const isBrowser = typeof window === 'object' && typeof document === 'object' && document . nodeType === 9 ; const useSafeLayoutEffect = isBrowser ? useLayoutEffect : useEffect ; param key param initialValue see https export function useStore < S = any , K = string > ( key , initialValue ? ) { const storageStore = Storage . get ( key ) ; const initialState = storageStore ? storageStore . state : initialValue ; Storage . set ( key , new Store < S > ( initialState ) ) ; const newStore = Storage . get ( key ) ; const [ state , set ] = useState ( initialState ) ; const { setters } = newStore ; /**
     * push setState sync
     */ useSafeLayoutEffect ( ( ) => { setters . push ( set ) ; emitStoreInit ( key ) ; } , [ ] ) ; useEffect ( ( ) => { return ( ) => { setters . splice ( setters . indexOf ( set ) , 1 ) ; } ; } , [ ] ) ; function act ( key ) { return ( value ) => { return newStore . setState ( key , state , value ) ; } ; } return [ state , act ( key ) ] ; }