import sinon from "sinon" ; import Adapt , { AdaptMountedElement , BuiltinProps , domDiffElements , handle , Handle , PrimitiveComponent , useImperativeMethods , WithChildren , } from "../../src" ; import { ChangeType , DependsOn , DeployHelpers , DeployStatus , GoalStatus , WaitStatus , } from "../../src/deploy/deploy_types" ; import { DependencyType , EPDependencies , EPDependency , ExecutionPlanImpl } from "../../src/deploy/execution_plan" ; import { GenericInstance } from "../../src/jsx" ; export interface IdProps { id : number ; } export class Prim extends PrimitiveComponent < IdProps > { } export interface DependProps extends WithChildren { id : number ; dep ? : ( id : number , goalStatus : GoalStatus , h : DeployHelpers ) => DependsOn | undefined ; when ? : ( id : number , goalStatus : GoalStatus , comp : DependPrim ) => WaitStatus | Promise < WaitStatus > ; } export class DependPrim < P extends DependProps = DependProps , S extends object = { } > extends PrimitiveComponent < P , S > { static defaultProps = { id : 0 } ; constructor ( props ) { super ( props ) ; const when = props . when ; if ( when ) this . deployedWhen = ( gs ) => when ( props . id , gs , this ) ; } dependsOn = ( goalStatus , h ) => this . props . dep && this . props . dep ( this . props . id , goalStatus , h ) ; } export function MakePrim ( props ) { return < Prim id = { props . id } />; } export interface MakeDependProps extends DependProps { id : number ; dep ? : ( id : number , goalStatus : GoalStatus , h : DeployHelpers ) => DependsOn | undefined ; when ? : ( id : number , goalStatus : GoalStatus ) => WaitStatus | Promise < WaitStatus > ; primProps : DependProps & Partial < BuiltinProps > ; } export function MakeDependPrim ( props ) { const { dep , id , when } = props ; const methods = { } ; if ( dep ) methods . dependsOn = ( gs , h ) => dep ( id , gs , h ) ; if ( when ) methods . deployedWhen = ( gs , h ) => when ( id , gs ) ; useImperativeMethods ( ( ) => methods ) ; return < DependPrim { ... props . primProps } / > ; } MakeDependPrim . defaultProps = { id : 0 , primProps : { id : 0 } , } ; export function spyArgs ( spy ) ; export function spyArgs ( spy , argNum ) ; export function spyArgs ( spy , argNum ? ) { const args = spy . getCalls ( ) . map ( ( call ) => call . args ) ; if ( argNum === undefined ) return args ; return args . map ( ( a ) => a [ argNum ] ) ; } export function makeHandles ( count , namePrefix = "" ) { const ret = [ ] ; for ( let i = 0 ; i < count ; i ++ ) { ret . push ( handle ( namePrefix + i ) ) ; } return ret ; } export const toChangeType = ( goal ) => goal === DeployStatus . Deployed ? ChangeType . create : ChangeType . delete ; export const toDiff = ( els , goal ) => goal === DeployStatus . Deployed ? domDiffElements ( [ ] , els ) : domDiffElements ( els , [ ] ) ; export interface StringDependencies { [ id ] : string [ ] ; } export interface DependenciesOptions { key ? : "id" | "detail" ; removeEmpty ? : boolean ; type ? : DependencyType ; } const defaultDepOptions = { key : "detail" , removeEmpty : false , type : DependencyType . FinishStart , } ; export function dependencies ( plan , options = { } ) { const { key , removeEmpty , type } = { ... defaultDepOptions , ... options } ; const ret = { } ; const epDeps = plan . toDependencies ( type ) ; const getKey = ( ep ) => key === "detail" || ! ep . elementId ? ep . detail : ep . elementId ; const toKey = ( d ) => getKey ( epDeps [ d . id ] ) ; Object . keys ( epDeps ) . map ( ( id ) => { const ep = epDeps [ id ] ; if ( removeEmpty && ep . deps . length === 0 ) return ; ret [ getKey ( ep ) ] = ep . deps . map ( toKey ) . sort ( ) ; } ) ; return ret ; }