class StateMachine::TimedTransition

Transitions of this type are triggered on a given timeout (in seconds). Created when supplying an :after option in the transition definition.

The timeout is canceled when the state is left.

The transition uses Grand Central Dispatch’s timer source mechanism: It adds a timer source to the state machine’s initial GCD queue.

The system tries to achieve an accuracy of 1 millisecond for the timeout. You can change this behavior to trade timing accuracy vs. system performance by using the leeway option (given in seconds). See {developer.apple.com/mac/library/DOCUMENTATION/Darwin/Reference/ManPages/man3/dispatch_source_set_timer.3.html Apple’s GCD documentation} for more information about this parameter.

@example Create a transition that timeouts after 8 hours:

state_machine.when :sleeping do |state|
  # Timeout after 28800 seconds
  state.transition_to :awake, after: 8 * 60 * 60
end

Public Instance Methods

arm() click to toggle source
# File lib/motion-state-machine/transition.rb, line 331
def arm
  @state_machine.log "Starting timeout -> #{options[:to]}, "\
    "after #{options[:after]}"
  delay = event_trigger_value
  interval = Dispatch::TIME_FOREVER
  leeway = @options[:leeway] || 0.001
  queue = @state_machine.initial_queue
  @timer = Dispatch::Source.timer(delay, interval, leeway, queue) do
    @state_machine.log "Timeout!"
    self.handle_in_source_state
  end
end
event_description() click to toggle source
# File lib/motion-state-machine/transition.rb, line 325
def event_description
  "after #{event_trigger_value} seconds of "\
  "#{source_state.name} (timeout)"
end
unarm() click to toggle source
# File lib/motion-state-machine/transition.rb, line 344
def unarm
  @state_machine.log "Timer unarmed"
  @timer.cancel!
end