module Rooibos::Command::Custom

Mixin for user-defined custom commands.

Custom commands extend Rooibos with side effects: WebSockets, gRPC, database polls, background tasks. The runtime dispatches them in threads and routes results back as messages.

Include this module to identify your class as a command. The runtime uses rooibos_command? to distinguish commands from plain models. Override rooibos_cancellation_grace_period if your cleanup takes longer than 100 milliseconds.

Use it to build real-time features, long-polling connections, or background workers.

Example

class WebSocketCommand
  include Rooibos::Command::Custom

  def initialize(url)
    @url = url
  end

  def call(out, token)
    ws = WebSocket::Client.new(@url)
    ws.on_message { |msg| out.put(:ws_message, msg) }
    ws.connect

    until token.canceled?
      ws.ping
      sleep 1
    end

    ws.close
  end

  # WebSocket close handshake needs extra time
  def rooibos_cancellation_grace_period
    5.0
  end
end