import Model from '../' ; import SubscriberManager , { SubscribeAction } from './subscribe-manager' ; type TCallback = ( ... states : any ) => void ; export interface IWatchAction { state ( callback ) : SubscribeAction ; store ( callback ) : SubscribeAction ; localStoreFetch ( callback ) : SubscribeAction ; } export default class Watch { private _m : Model ; private _stateSubscriberManager : SubscriberManager ; private _storeSubscriberManager : SubscriberManager ; private _localStoreFetchedSubscriberManager : SubscriberManager ; private _hasLocalStoreBeenFetched : boolean = false ; constructor ( m ) { this . _m = m ; this . _stateSubscriberManager = new SubscriberManager ( m , ( m ) => m . watch ( ) . state ) ; this . _storeSubscriberManager = new SubscriberManager ( m , null ) ; this . _localStoreFetchedSubscriberManager = new SubscriberManager ( m , null ) ; } private _model = ( ) : Model => this . _m ; private _stateSubscriber = ( ) : SubscriberManager => this . _stateSubscriberManager ; private _storeSubscriber = ( ) : SubscriberManager => this . _storeSubscriberManager ; private _localStoreFetchedSubscriber = ( ) : SubscriberManager => this . _localStoreFetchedSubscriberManager ; public hasLocalStoreBeenPulled = ( ) => this . _hasLocalStoreBeenFetched ; public onStateChanged = ( ... props ) : number => this . _stateSubscriber ( ) . notify ( ... props ) ; public onStoreChanged = ( ... props ) => this . _storeSubscriber ( ) . notify ( ... props ) ; public onLocalStoreFetched = ( ) => { this . _hasLocalStoreBeenFetched = true ; return this . _localStoreFetchedSubscriber ( ) . notify ( ) ; } ; public state = ( callback ) : SubscribeAction => this . _stateSubscriber ( ) . add ( callback ) ; public store = ( callback ) : SubscribeAction => this . _storeSubscriber ( ) . add ( callback ) ; public localStoreFetch = ( callback ) : SubscribeAction => this . _localStoreFetchedSubscriber ( ) . add ( callback ) ; public action = ( ) : IWatchAction => { return { state : this . state , store : this . store , localStoreFetch : this . localStoreFetch } ; } ; }