import { Observable } from 'rxjs' ; import { Emitter } from './emitter' ; import { mapValues } from './utils' ; export type Undux < State extends object > = { [ K in keyof State ] : { key : K ; previousValue : State [ K ] ; value : State [ K ] ; } ; } ; /**
 * Public Store interface. When you want to reference the Store type,
 * this is the type to use.
 */ export interface Store < State extends object > { get < K extends keyof State > ( key ) : State [ K ] ; set < K extends keyof State > ( key ) : ( value : State [ K ] ) => void ; setFrom_EXPERIMENTAL ( f ) : void ; on < K extends keyof State > ( key ) : Observable < State [ K ] > ; onAll ( ) : Observable < Undux < State > [ keyof State ] > ; getState ( ) : Readonly < State > ; } /**
 * Immutable snapshot of the current store state. One StoreSnapshot per
 * StoreDefinition is usually alive at a time.
 */ export class StoreSnapshot < State extends object > implements Store < State > { constructor ( private state , private storeDefinition ) { } get < K extends keyof State > ( key ) { return this . state [ key ] ; } set < K extends keyof State > ( key ) { return this . storeDefinition . set ( key ) ; } setFrom_EXPERIMENTAL ( f ) { return this . storeDefinition . setFrom_EXPERIMENTAL ( f ) ; } on < K extends keyof State > ( key ) { return this . storeDefinition . on ( key ) ; } onAll ( ) { return this . storeDefinition . onAll ( ) ; } getState ( ) { return Object . freeze ( this . state ) ; } } export type Options = { isDevMode : boolean ; } ; let DEFAULT_OPTIONS = { isDevMode : false } ; /**
 * We create a single instance of this per <Container />.
 */ export class StoreDefinition < State extends object > implements Store < State > { private storeSnapshot : StoreSnapshot < State > ; private alls : Emitter < Undux < State > > ; private emitter : Emitter < State > ; private setters : { readonly [ K in keyof State ] : ( value : State [ K ] ) => void ; } ; constructor ( state , options ) { this . alls = new Emitter ( options . isDevMode ) ; this . emitter = new Emitter ( options . isDevMode ) ; this . storeSnapshot = new StoreSnapshot ( state , this ) ; this . setters = mapValues ( state , ( v , key ) => ( value ) => { let previousValue = this . storeSnapshot . get ( key ) ; this . storeSnapshot = new StoreSnapshot ( Object . assign ( { } , this . storeSnapshot . getState ( ) , { [ key ] : value } ) , this ) ; this . emitter . emit ( key , value ) ; this . alls . emit ( key , { key , previousValue , value } ) ; } ) ; } on < K extends keyof State > ( key ) { return this . emitter . on ( key ) ; } onAll ( ) { return this . alls . all ( ) ; } get < K extends keyof State > ( key ) { return this . storeSnapshot . get ( key ) ; } set < K extends keyof State > ( key ) { return this . setters [ key ] ; } setFrom_EXPERIMENTAL ( f ) { return f ( this . storeSnapshot ) ; } getCurrentSnapshot ( ) { return this . storeSnapshot ; } toStore ( ) { return this . storeSnapshot ; } getState ( ) { return this . storeSnapshot . getState ( ) ; } } deprecated export function createStore < State extends object > ( initialState , options = DEFAULT_OPTIONS ) { return new StoreDefinition < State > ( initialState , options ) ; } export type Effects < State extends object > = ( store : StoreDefinition < State > ) => StoreDefinition < State > ; export type EffectsAs < States extends { [ alias ] : any ; } > = ( stores : { [ K in keyof States ] : StoreDefinition < States [ K ] > ; } ) => { [ K in keyof States ] : StoreDefinition < States [ K ] > ; } ; deprecated export type Plugin < State extends object > = ( store : StoreDefinition < State > ) => StoreDefinition < State > ; export * from './plugins/withLogger' ; export * from './plugins/withReduxDevtools' ; export * from './react' ; export { CreateConnectedStore } from './react/createConnectedStore' ; export { CreateConnectedStoreAs } from './react/createConnectedStoreAs' ;