sprocket_mist

Types

pub type CSRFValidator =
  fn(String) -> Result(Nil, Nil)

Values

pub fn component(
  req: request.Request(//internal),
  component: fn(//internal, p) -> #(//internal, //internal),
  initialize_props: fn(option.Option(dynamic.Dynamic)) -> p,
  validate_csrf: fn(String) -> Result(Nil, Nil),
) -> response.Response(mist.ResponseData)

Component

Renders a component for a given request. This function is used to instantiate a server component with specified props and render it to the client.

This function is useful for rendering a standalone server component on a page.

Rendering a component is a two-step process:

  1. Upon the initial request, the server will statically render the component with the specified props using the initialize_props function. This function will be called with None during this phase, in which the server can decide what props to pass to the component. The rendered HTML will be sent to the client.

  2. The client will then connect to the server via a websocket connection by appending /connect to the request path. Once the connection is established, the client will send a join message to the server with the CSRF token and initial props. The server will then start the sprocket runtime with the component and initialize props using the initialize_props function with Some(Dynamic). Once the runtime is started, the server will render the component with the initial props and send the full rendered DOM to the client. The client will then be able to send messages to the server and receive patch updates from the server.

This function also takes a validate_csrf function that will be called with the CSRF token sent by the client. This function should return Ok if the CSRF token is valid, and Error if the CSRF token is invalid. If the CSRF token is invalid, the server will log an error and close the connection.

pub fn view(
  req: request.Request(//internal),
  layout: fn(//internal) -> //internal,
  el: //internal,
  validate_csrf: fn(String) -> Result(Nil, Nil),
) -> response.Response(mist.ResponseData)

View

Renders a view for a given request. This function is used to render a entire view with a layout and an element. A layout is a function that takes an element and returns a new element which contains the element wrapped in a layout. A layout is only rendered once and will not be re-rendered when the server sends patches.

This function behaves similarly to the component function, but it does not initialize a component with props and any props sent by the client will be ignored. This function is useful for rendering an entire page with a component embedded in it.

Search Document