import { IComponent } from '../types' ; import { WatcherDependences } from './watch' ; import { ChangeDetectionStrategy } from './change-detection' ; import { utils } from '../utils' ; export type SetState = ( newState : any ) => void ; export param { * } newState returns { void } export function setState ( newState ) { let _newState = null ; if ( newState && utils . isFunction ( newState ) ) _newState = newState ( ) ; if ( newState && newState instanceof Object ) _newState = newState ; const saveWatchStatus = ( this as IComponent ) . $watchStatus ; if ( saveWatchStatus === 'available' ) ( this as IComponent ) . $watchStatus = 'pending' ; for ( const key in _newState ) { this [ key ] = _newState [ key ] ; if ( ( this as IComponent ) . $nvChangeDetection !== ChangeDetectionStrategy . OnPush ) WatcherDependences ( this as IComponent , key ) ; } if ( saveWatchStatus === 'available' ) { ( this as IComponent ) . $watchStatus = 'available' ; if ( ( this as IComponent ) . nvDoCheck ) ( this as IComponent ) . nvDoCheck ( ) ; ( this as IComponent ) . render ( ) ; } } export returns { ( target : IComponent , propertyName : string ) => any } export function StateSetter ( ) { return function ( target , propertyName ) : any { target [ propertyName ] = setState ; return target [ propertyName ] ; } ; }