Lavash.Lifecycle.AsyncRuntime (Lavash v0.4.0-rc.3)

Copy Markdown View Source

Runtime support for async :name do run fn assigns -> ... end end declarations and the fire :name op.

fire/3 uses Phoenix.LiveView.start_async/3 to spawn the task, which:

  • tracks the task pid on the socket so render_async/2 in tests sees the in-flight work — crucial for parity with vanilla assign_async/start_async
  • routes the result through handle_async/3 (generated by the compile-time transformer; see handle_lavash_async/3 below)
  • cancels and restarts the task if fire is called again while one is in flight

Decoupling rationale

The declaration (what the work IS) lives at the async :name do ... end site. The trigger (WHEN it runs) lives wherever fire :name is called — mount do, an action body, or a message body. One declaration, multiple trigger paths, no duplication.

Summary

Functions

Fire the named async task on the given socket. Looks up the module's registered async definitions and starts the task via Phoenix.LiveView.start_async/3. The field is set to AsyncResult.loading() immediately.

Routes a handle_async/3 callback for a lavash-declared async task. Called by the generated handle_async/3 clause when the result name matches a registered async declaration.

Functions

fire(socket, module, name)

Fire the named async task on the given socket. Looks up the module's registered async definitions and starts the task via Phoenix.LiveView.start_async/3. The field is set to AsyncResult.loading() immediately.

Returns the updated socket.

handle_lavash_async(socket, module, name, result)

Routes a handle_async/3 callback for a lavash-declared async task. Called by the generated handle_async/3 clause when the result name matches a registered async declaration.

Wraps the result in AsyncResult.ok/1 or AsyncResult.failed/2 to match vanilla assign_async shape, then runs through the reactive recompute + project pipeline so downstream derived fields see the new value.