Contexts
Contexts are data available through a whole track. Unlike parameters, they inherently exists and are accessible by all treatements requiring them, as long as the source of the track they are in provide it.
The calling treatments don't have to explicitly pass any context to their inner called treatements.
Contexts have special naming convention, starting with @
.
Context providing and requirement
Sources provide contexts, and treatments connected afterwards this source can require context to access it.
Requiring a context means the treatment can only be used in a track coming from a source providing such context.
use http/server::HttpServer
use http/server::connection
use http/method::|get as |methodGet
use http/server::@HttpRequest
use std/data::|get
treatment myApp[http_server: HttpServer]() {
connection(method=|methodGet(), route="/user/:user")
actionGetUser()
connection.data -> actionGetUser.data,result -> connection.data
}
treatment actionGetUser()
require @HttpRequest
input data: Stream<byte>
output result: Stream<byte>
{
getUser(user_id = |get<string>(@HttpRequest[parameters], "user"))
Self.data -> getUser.data,result -> Self.result
}
treatment getUser(user_id: Option<string>)
input data : Stream<byte>
output result: Stream<byte>
{
// Do some stuff…
}
Reference for HttpServer, connection, |get (http method), @HttpRequest,|get (map access)
myApp
actionGetUser
As contexts comes at track creation, they are inherently variable data.