Class: Rooibos::Command::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/rooibos/command/batch.rb

Overview

A fire-and-forget parallel command.

Applications fetch data from multiple sources. Dashboard panels load users, stats, and notifications. Waiting sequentially is slow. Managing threads and error handling manually is error-prone.

This command runs children in parallel. Each child sends its own messages independently. The batch completes when all children finish or when cancellation fires. On cancellation, emits Message::Canceled.

Use it for parallel fetches, concurrent refreshes, or any work that does not need coordinated results.

Prefer the Command.batch factory method for convenience.

Example

# Using the factory method (recommended)
Command.batch(
  Command.http(:get, "/users", :users),
  Command.http(:get, "/stats", :stats),
)

# Using the class directly
Batch.new(
  Command.http(:get, "/users", :users),
  Command.http(:get, "/stats", :stats),
)

# Handle each response independently
def update(msg, model)
  case msg
  in { type: :http, envelope: :users, body: }
    model.with(users: JSON.parse(body))
  in { type: :http, envelope: :stats, body: }
    model.with(stats: JSON.parse(body))
  end
end