datastar_gleam/event

Types

Configuration for an ExecuteScript event.

This is a convenience wrapper around PatchElements that injects a <script> tag into the page and optionally removes it after execution.

pub type ExecuteScript {
  ExecuteScript(
    id: option.Option(String),
    retry: Int,
    script: String,
    auto_remove: option.Option(Bool),
    attributes: List(String),
  )
}

Constructors

  • ExecuteScript(
      id: option.Option(String),
      retry: Int,
      script: String,
      auto_remove: option.Option(Bool),
      attributes: List(String),
    )

Configuration for a PatchElements event.

Use new_elements or new_remove to create a value, then customise it with with_* functions and finally call patch_elements_to_datastar_event.

pub type PatchElements {
  PatchElements(
    id: option.Option(String),
    retry: Int,
    elements: option.Option(String),
    selector: option.Option(String),
    mode: consts.ElementPatchMode,
    use_view_transition: Bool,
  )
}

Constructors

Configuration for a PatchSignals event.

pub type PatchSignals {
  PatchSignals(
    id: option.Option(String),
    retry: Int,
    signals: String,
    only_if_missing: Bool,
  )
}

Constructors

  • PatchSignals(
      id: option.Option(String),
      retry: Int,
      signals: String,
      only_if_missing: Bool,
    )

Values

pub fn execute_script_to_datastar_event(
  s: ExecuteScript,
) -> datastar_gleam.DatastarEvent

Convert an ExecuteScript builder into a DatastarEvent.

pub fn new_elements(html: String) -> PatchElements

Create a PatchElements builder that inserts the given HTML fragment.

let patch =
  event.new_elements("<div id='message'>Hello!</div>")
  |> event.with_selector("#target")
  |> event.with_mode(event.Append)
pub fn new_remove(selector: String) -> PatchElements

Create a PatchElements builder that removes the element matched by selector.

pub fn new_script(script: String) -> ExecuteScript

Create an ExecuteScript builder with the given JavaScript source.

let script =
  event.new_script("console.log('Hello from Gleam!')")
  |> event.with_auto_remove(True)
pub fn new_signals(signals: String) -> PatchSignals

Create a PatchSignals builder with the given JSON-encoded signals.

let patch =
  event.new_signals("{ \"count\": 42 }")
  |> event.with_only_if_missing(True)
pub fn patch_elements_to_datastar_event(
  p: PatchElements,
) -> datastar_gleam.DatastarEvent

Convert a PatchElements builder into a DatastarEvent.

pub fn patch_signals_to_datastar_event(
  p: PatchSignals,
) -> datastar_gleam.DatastarEvent

Convert a PatchSignals builder into a DatastarEvent.

pub fn with_attributes(
  s: ExecuteScript,
  attributes: List(String),
) -> ExecuteScript

Add extra attributes to the generated <script> tag.

pub fn with_auto_remove(
  s: ExecuteScript,
  auto_remove: Bool,
) -> ExecuteScript

Automatically remove the <script> tag after it executes.

pub fn with_id(p: PatchElements, id: String) -> PatchElements

Set the SSE event ID.

pub fn with_mode(
  p: PatchElements,
  mode: consts.ElementPatchMode,
) -> PatchElements

Set the insertion mode (default Outer).

pub fn with_only_if_missing(
  p: PatchSignals,
  only_if_missing: Bool,
) -> PatchSignals

Only patch signals that are not already present in the frontend store.

pub fn with_retry(p: PatchElements, retry: Int) -> PatchElements

Set the SSE retry duration in milliseconds.

pub fn with_script_id(
  s: ExecuteScript,
  id: String,
) -> ExecuteScript

Set the SSE event ID.

pub fn with_script_retry(
  s: ExecuteScript,
  retry: Int,
) -> ExecuteScript

Set the SSE retry duration in milliseconds.

pub fn with_selector(
  p: PatchElements,
  selector: String,
) -> PatchElements

Set the CSS selector that identifies the target element.

pub fn with_signals_id(
  p: PatchSignals,
  id: String,
) -> PatchSignals

Set the SSE event ID.

pub fn with_signals_retry(
  p: PatchSignals,
  retry: Int,
) -> PatchSignals

Set the SSE retry duration in milliseconds.

pub fn with_use_view_transition(
  p: PatchElements,
  enabled: Bool,
) -> PatchElements

Enable or disable view transitions.

Search Document