Children: Content<T>
Components on the server — design
Status: implemented are markup as HTML, components of the server, their results,
Responsegoing up,Content<T>, component values with a result, decorators with guards, wrappers that take the body and the request context, routes, androoklang/server; the server of the site,site/server/main.rk, is written with them, and the guide has a section on them. The decisions are collected at the end of the document, together with what is not decided yet. Server markup, components androoklang/serverremain experimental in 1.x and may change between releases.
This document builds on components.md: components, decorators, context, components as values. It describes only what is different on the server.
A component that wraps others, a middleware, decides whether they run at all. So the children of a component of the server are lazy: they are rendered only when the component asks for them.
comp Logger<T>(children Content<T>) T {
const started = Date.now()
defer console.log(`${@session.url.pathname}: ${Date.now() - started} ms`)
return await children()
}
Content<T>is a function without parameters that renders the children and gives their result,T. Calling it runs them; not calling it skips them, with all they would load.{children}in markup isawait children(): the children go where it stands.Contentwithout an argument isContent<Html>, so the components of the browser that takechildren Contentmean the same on the server. A use without children givesnullwhen they are called.- The children of
Contentare markup, also a single component:<Layout><Article /></Layout>givesHtml, and aResponseofArticlegoes up whenLayoutcalls them, also withawait children(). - Type parameters of a component,
comp Logger<T>(...), are new: they are written as for a function (see "Generics" in spec.md) and are inferred from the properties and the children of the tag, as those of a call are from its arguments:<List items={[1, 2]} render={n => String(n)} />ofcomp List<T>(items T[], render func(T) string)takesTfromitems, andnis a number. The type of<Logger><Users /></Logger>isUser[], that of<Logger><Profile /></Logger>isHtml | Response, that of<Logger><p>...</p></Logger>isHtml. One component among the children ofContent<T>gives its own value; other children are markup. - The children are checked against
Content<T>of a type:<Names><Users /></Names>ofcomp Names(children Content<string[]>)is an error,the children of <Names> give User[], but it takes Content<string[]>. - Calling the children twice renders them twice; that is allowed, as for any function.