Request context
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.
The request, the user who is signed in, the connection to the database for this request: deep
components need them, and passing them through every property is what React's context and Go's
context.Context exist for. In Rook it is a decorator that a component above applies and the
components below read its public members.
dec session(request Request) {
public const url = new URL(request.url)
public const user = await userOf(request)
}
@session(request)
comp App(request Request) {
return <Logger><Router routes={ROUTES} /></Logger>
}
comp Header() {
return <header>{@session.user?.name ?? "Guest"}</header>
}
- Requirements are inferred from a read such as
@session.userin a component or decorator. Named component calls pass the requirement up to the provider. The nearest instance is selected. - Known trees are checked at compile time. If the tree starts with
<Header />and has no provider, the compiler reports that it requires@session. Dynamic wrappers can declare the requirement explicitly. - Checked at runtime where the tree is not known: through a component value or children given
by a component of another module. There a missing decorator is an error of the request:
Header needs @session from a component above it. - One instance per request. The body of
@sessionruns once, where it is applied; everything below reads the same object. Nothing is shared between requests, unlike the state of a module.