Class: Rooibos::Command::Clock

Inherits:
Object
  • Object
show all
Includes:
Custom
Defined in:
lib/rooibos/command/clock.rb

Overview

A one-shot clock command.

Applications display the time, throttle refreshes, and show “last updated 30 seconds ago.” All of these call Time.now. But Time.now in Update is a side effect. The same model and message produce different results depending on when you call them. Testing becomes non-deterministic.

This command waits, then sends a Message::Clock with the current time. It responds to cancellation cooperatively. When canceled, it sends Message::Canceled so you know the clock stopped.

Use it for periodic time updates, scheduling, or any feature that asks “what time is it?”

Prefer the Command.clock factory method for convenience.

Example: Periodic refresh

def update(msg, model)
  case msg
  in { type: :clock, envelope: :refresh, time: }
    [model.with(current_time: time.utc.to_s),
     Command.clock(1, :refresh)]
  end
end

Example: “Last updated” display

def update(msg, model)
  case msg
  in { type: :clock, envelope: :clock, time: }
    ago = (time - model.last_fetch).round
    [model.with(status: "Updated #{ago}s ago"),
     Command.clock(1, :clock)]
  end
end

Method Summary

Methods included from Custom

#deconstruct_keys, #rooibos_cancellation_grace_period, #rooibos_command?