import { IComponent } from '../types' ; import { WatcherDependences } from './watch' ; import { injected , providersFormater , TProviders } from '@indiv/di' ; import { collectDependencesFromViewModel } from './utils' ; import { componentCompiler } from '../compile' ; import { ChangeDetectionStrategy } from './change-detection' ; export type TComponentOptions = { selector : string ; template ? : string ; templateUrl ? : string ; providers ? : TProviders ; changeDetection ? : ChangeDetectionStrategy ; } ; template Props template Vm param { TComponentOptions } options returns { ( _constructor : Function ) => void } export function Component ( options ) { return function ( _constructor ) : void { injected ( _constructor ) ; ( _constructor as any ) . isSingletonMode = false ; ( _constructor as any ) . nvType = 'nvComponent' ; ( _constructor as any ) . selector = options . selector ; const vm = _constructor . prototype ; if ( options . template ) vm . $template = options . template ; if ( options . templateUrl ) vm . $templateUrl = options . templateUrl ; if ( options . providers ) vm . $privateProviders = providersFormater ( options . providers ) ; if ( options . changeDetection === ChangeDetectionStrategy . Default ) { vm . $nvChangeDetection = options . changeDetection ; vm . $watchStatus = 'available' ; vm . $isWaitingRender = false ; } else { vm . $nvChangeDetection = options . changeDetection ; vm . $watchStatus = 'disable' ; vm . $isWaitingRender = false ; } vm . $declarationMap = new Map ( ) ; vm . $componentList = [ ] ; vm . $directiveList = [ ] ; vm . $pipeMap = new Map ( ) ; vm . watchData = function ( ) : void { ( this as IComponent ) . $indivInstance . templateChecker ( this as IComponent ) ; if ( ( this as IComponent ) . $nvChangeDetection === ChangeDetectionStrategy . OnPush ) return ; if ( ! ( this as IComponent ) . $dependencesList ) ( this as IComponent ) . $dependencesList = [ ] ; collectDependencesFromViewModel ( this ) ; ( this as IComponent ) . $dependencesList . forEach ( dependence => WatcherDependences ( this as IComponent , dependence ) ) ; } ; vm . render = async function ( ) : Promise < IComponent > { const nativeElement = ( this as IComponent ) . $nativeElement ; return Promise . resolve ( ) . then ( ( ) => { return ( this as IComponent ) . compiler ( nativeElement , this ) ; } ) ; } ; vm . compiler = componentCompiler ; } ; }