omnimessage_lustre

Types

Holds decode and encode functions for omnimessage messages. Decode errors will be called back for you to handle, while Encode errors are interpreted as “skip this message” – no error will be raised for them and they won’t be sent over.

Since an EncoderDecoder is expected to receive the whole message type of an application, but usually will ignore messages that aren’t shared, it’s best to define it as a thin wrapper around shared encoders/decoders:

// Holds shared message types, encoders and decoders
import shared

let encoder_decoder =
  EncoderDecoder(
    fn(msg) {
      case msg {
        // Messages must be encodable
        ClientMessage(message) -> Ok(shared.encode_client_message(message))
        // Return Error(Nil) for messages you don't want to send out
        _ -> Error(Nil)
      }
    },
    fn(encoded_msg) {
      // Unsupported messages will cause TransportError(DecodeError(error))
      shared.decode_server_message(encoded_msg)
      |> result.map(ServerMessage)
    },
  )
pub type EncoderDecoder(msg, encoding, decode_error) {
  EncoderDecoder(
    encode: fn(msg) -> Result(encoding, Nil),
    decode: fn(encoding) -> Result(msg, decode_error),
  )
}

Constructors

  • EncoderDecoder(
      encode: fn(msg) -> Result(encoding, Nil),
      decode: fn(encoding) -> Result(msg, decode_error),
    )

Functions

pub fn application(
  init: fn(a) -> #(b, Effect(c)),
  update: fn(d, c) -> #(b, Effect(c)),
  view: fn(b) -> Element(e),
  encoder_decoder: EncoderDecoder(c, f, g),
  transport: Transport(f, g),
  transport_wrapper: fn(TransportState(g)) -> c,
) -> App(a, b, e)

Creates an omnimessage_lustre application. The extra parameters are:

  • encoder_decoder encodes and decodes messages
  • transport will transfer and recieve encoded messages. see omnimessage_lustre/transports for available ones
  • transport_wrapper a wrapper for your Msg type for transport status
pub fn component(
  init: fn(a) -> #(b, Effect(c)),
  update: fn(d, c) -> #(b, Effect(c)),
  view: fn(b) -> Element(e),
  on_attribute_change: Dict(
    String,
    fn(Dynamic) -> Result(e, List(DecodeError)),
  ),
  encoder_decoder: EncoderDecoder(c, f, g),
  transport: Transport(f, g),
  transport_wrapper: fn(TransportState(g)) -> c,
) -> App(a, b, e)

Creates an omnimessage_lustre Lustre component. The extra parameters are:

  • encoder_decoder encodes and decodes messages
  • transport will transfer and recieve encoded messages. see omnimessage_lustre/transports for available ones
  • transport_wrapper a wrapper for your Msg type for transport status
Search Document