import { IComponentState , IComponentProps , IViewModel , IComponentContext } from './interfaces/index' ; import { disposeModel } from './disposeModel' ; export function createModel ( state , props , ctx , ViewModel ) { const { resolver = ( ViewModelClass ) => new ViewModelClass ( ) } = ctx as IComponentContext ; const { modelRef = ( ) => true } = props ; if ( props . model && state . model !== props . model ) { modelRef ( props . model ) ; disposeModel ( state ) ; return { model : props . model , shouldDispose : false } ; } if ( ! state . model ) { const model = resolver ( ViewModel ) ; modelRef ( model ) ; return { model , shouldDispose : true } ; } return state ; }