import { notNull } from "@adpt/utils" ; import * as util from "util" ; import { DeployHelpers , GoalStatus , waiting } from "../deploy" ; import { Handle , isHandle } from "../handle" ; import { AdaptElement , childrenToArray , createElement , DeferredComponent , isElement , isMountedElement , SFCDeclProps , } from "../jsx" ; import { Children } from "../type_support" ; import { Group } from "./group" ; /**
 * Props for  Adapt . Sequence .
  export interface SequenceProps extends Children < Handle | AdaptElement | null > { } function toHandle ( val ) { return isElement ( val ) ? val . props . handle : val ; } function handleDependsOn ( h , dependency ) { const orig = h . mountedOrig ; if ( ! orig ) return ; orig . addDependency ( dependency ) ; } public export function Sequence ( props ) { const origChildren = childrenToArray ( props . children ) . filter ( notNull ) ; const newProps = { key : props . key , origChildren , } ; return createElement ( SequenceDeferred , newProps , origChildren ) ; } interface SequenceDeferredProps extends SequenceProps { origChildren : ( Handle | AdaptElement ) [ ] ; } function nextBuilt ( el , skip ? ) { const next = el . props . handle . nextMounted ( ( e ) => e !== skip && isMountedElement ( e ) && e . built ( ) ) ; return next === skip ? undefined : next ; } class SequenceDeferred extends DeferredComponent < SequenceDeferredProps > { deployedWhenIsTrivial = true ; build ( ) { const props = this . props ; const origKids = props . origChildren ; let prev ; for ( const k of origKids ) { if ( ! isHandle ( k ) && ! isElement ( k ) ) { throw new Error ( "Children of a Sequence component must be an " + "Element or Handle. Invalid child: " + util . inspect ( k ) ) ; } const kHandle = toHandle ( k ) ; if ( prev && isElement ( k ) ) { for ( let el = nextBuilt ( k ) ; el != null ; el = nextBuilt ( el , el ) ) { handleDependsOn ( el . props . handle , prev ) ; } } prev = kHandle ; } const finalKids = childrenToArray ( props . children ) . filter ( isElement ) ; if ( finalKids . length === 0 ) return null ; return createElement ( Group , { key : props . key } , finalKids ) ; } deployedWhen = ( _goalStatus , helpers ) => { const unready = childrenToArray ( this . props . children ) . map ( ( k ) => isElement ( k ) ? k . props . handle : isHandle ( k ) ? k : null ) . filter ( notNull ) . filter ( ( h ) => ! helpers . isDeployed ( h ) ) ; return unready . length === 0 ? true : waiting ( `Waiting on ${ unready . length } child elements` , unready ) ; } ; }