import { AppProps , App , CmdType , Context , normalize , resolveCmdResults } from './../index' ; import Cmd from './../cmd' ; import { get } from '../utils' ; export type Options = { renderToString : ( view : any ) => string ; } ; export default function withSSR < State , Actions > ( options ) { return ( app ) => ( props ) => { let initCmd = Cmd . none ; const ctx = app ( { ... props , init ( ) { let result = normalize ( props . init ( ) ) ; initCmd = result . cmd ; result . cmd = Cmd . none ; return result ; } , onRender ( ) { return ; } } ) ; ctx . render = async ( state ? ) => { let results = initCmd . map ( sub => sub ( ctx . actions ) ) ; await resolveCmdResults ( results ) ; state = state || ctx . state ; const view = ctx . view ( state , ctx . actions ) ; return options . renderToString ( view ) ; } ; let newCtx = new Proxy ( ctx , { get ( t , p , r ) { if ( p === 'state' ) { const s = t . state ; if ( 'lazyComps' in s ) { return { ... s as any , lazyComps : { } } ; } return s ; } return t [ p ] ; } } ) ; return newCtx ; } ; }