import { toArray } from "@adpt/utils" ; import { flatten } from "lodash" ; import { Handle , isHandle } from "../handle" ; import { isMountedElement } from "../jsx" ; import { Dependency , DeployHelpers , Relation , RelationExt , Waiting , WaitStatus , } from "./deploy_types" ; const isWaiting = ( val ) : val is Waiting | Waiting [ ] => val !== true ; export const relationIsReadyStatus = ( rels ) => { const status = ( r ) => r . ready ( r . relatesTo || [ ] ) ; rels = toArray ( rels ) ; const notReady = flatten ( rels . map ( status ) . filter ( isWaiting ) ) ; return notReady . length === 0 ? true : notReady . length === 1 ? notReady [ 0 ] : notReady ; } ; export const relationIsReady = ( r ) => relationIsReadyStatus ( r ) === true ; export const relationInverse = ( r ) : Relation => { const relatesTo = r . relatesTo ? r . relatesTo . map ( ( a ) => relationInverse ( a ) ) : [ ] ; if ( r . inverse ) return r . inverse ( relatesTo ) ; return { ... r , relatesTo } ; } ; export const relationToString = ( r , indent = "" ) : string => { if ( Object . prototype . hasOwnProperty . call ( r , "toString" ) && r . toString ) { return r . toString ( indent ) ; } const relatesTo = r . relatesTo || [ ] ; const args = relatesTo . length === 0 ? "" : `\n${ relatesTo . map ( ( a ) => relationToString ( a , indent + "  " ) ) . join ( ",\n" ) }\n${ indent }` ; return `${ indent }${ r . description }(${ args })` ; } ; export const waitStatusToString = ( s ) => s === true ? "Ready" : ! Array . isArray ( s ) ? s . status : [ "Waiting for:" , ... s . map ( ( w ) => w . status ) ] . join ( "\n" ) ; export function depName ( d ) { if ( isHandle ( d ) ) { const target = d . target ; if ( target ) { let id = isMountedElement ( target ) ? target . id : target . componentName ; if ( d . name ) id += ` (${ d . name })` ; return id ; } if ( d . name ) return `Handle(${ d . name })` ; return d . toString ( ) ; } return `Dep(${ d . description })` ; } export const toRelation = ( h , d ) => isHandle ( d ) ? h . dependsOn ( d ) : d ; export function relatedHandles ( rel ) { const handles = new Set < Handle > ( ) ; const deps = ( r ) => { toDependencies ( r ) . forEach ( ( d ) => { if ( isHandle ( d ) ) handles . add ( d ) ; else deps ( d ) ; } ) ; } ; deps ( rel ) ; return [ ... handles ] ; } export const toDependencies = ( r ) => r . toDependencies ? r . toDependencies ( ) : r . relatesTo || [ ] ; export const nDepends = ( count ) => count === 1 ? `1 dependency` : `${ count } dependencies` ;