lightspeed/form_compat

LiveView-style form change/submit compatibility semantics.

Types

Client-side rate limit mode for one input.

pub type ChangeMode {
  Immediate
  Debounce(wait_ms: Int)
  DebounceBlur
  Throttle(window_ms: Int)
}

Constructors

  • Immediate
  • Debounce(wait_ms: Int)
  • DebounceBlur
  • Throttle(window_ms: Int)

Deterministic dispatch decision.

pub type Dispatch {
  DispatchChange(input: String, payload: form.FormData)
  DispatchSubmit(payload: form.FormData)
  Queued(input: String)
  Suppressed(input: String, reason: String)
  NoDispatch
}

Constructors

  • DispatchChange(input: String, payload: form.FormData)
  • DispatchSubmit(payload: form.FormData)
  • Queued(input: String)
  • Suppressed(input: String, reason: String)
  • NoDispatch

Input registration used by the form compatibility runtime.

pub type InputConfig {
  InputConfig(name: String, mode: ChangeMode)
}

Constructors

Runtime

opaque

Form compatibility runtime with one state record per registered input.

pub opaque type Runtime

Values

pub fn blur(
  runtime: Runtime,
  input: String,
  at_ms: Int,
) -> #(Runtime, Dispatch)

Handle one blur event used by phx-debounce="blur".

pub fn change(
  runtime: Runtime,
  input: String,
  payload: String,
  at_ms: Int,
) -> #(Runtime, Dispatch)

Handle one phx-change-style event.

Semantics:

  • debounce stores pending payload
  • debounce blur stores pending payload until blur
  • throttle emits immediately then suppresses inside window
  • change for one input resets timers for all other inputs
pub fn dispatch_label(dispatch: Dispatch) -> String

Stable dispatch label for tests and logs.

pub fn has_pending(runtime: Runtime, input: String) -> Bool

Return pending status for one input.

pub fn mode(
  runtime: Runtime,
  input: String,
) -> option.Option(ChangeMode)

Return the current mode for one input.

pub fn new(configs: List(InputConfig)) -> Runtime

Build a runtime from known inputs.

pub fn submit(
  runtime: Runtime,
  payload: String,
  at_ms: Int,
) -> #(Runtime, Dispatch)

Handle one phx-submit-style event.

Submit resets existing change timers.

pub fn tick(
  runtime: Runtime,
  at_ms: Int,
) -> #(Runtime, List(Dispatch))

Flush due debounce timers.

Search Document