import * as React from 'react' ; import reim , { Reim , Filter , Actions , isReim , ReimOptions } from 'reim' ; import State from './components/state' ; export { default as State } from './components/state' ; export { default as Partial } from './components/partial' ; export function withReim < TR extends Reim < any > > ( store , { filter = s => s , actions = { } } ) { return ( Children ) => ( p ) => ( < State < typeof store , any , typeof filter , typeof actions > store = { store } filter = { filter } actions = { actions } > { ( selected , actions ) => < Children { ... selected } { ... actions } { ... p } />} < /State>); } export function useReim < TS > ( initial , { filter = s => s , actions = { } } = { } , dependencies = [ ] ) { if ( ! React . useState ) { throw new Error ( 'At least React@16.7-alpha.2 is required to use Hooks' ) ; } const { useState , useEffect , useRef } = React ; const mountRef = useRef ( false ) ; const storeRef = useRef ( isReim ( initial ) ? initial : reim ( initial ) ) ; const [ state , set ] = useState ( storeRef . current . filter ( filter ) ) ; useEffect ( ( ) => { if ( mountRef . current ) { set ( ( ) => storeRef . current . filter ( filter ) ) ; } else { mountRef . current = true ; } } , dependencies ) ; useEffect ( ( ) => storeRef . current . subscribe ( s => set ( s ) , { filter , immediate : true } ) . unsubscribe , [ ] ) ; return [ state , storeRef . current . actions ( actions ) ] ; }