Class: StateMachine::TimedTransition

Inherits:
Transition show all
Defined in:
lib/motion-state-machine/transition.rb

Overview

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 Apple's GCD documentation for more information about this parameter.

Examples:

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

Instance Attribute Summary

Attributes inherited from Transition

#destination_state, #event_trigger_value, #options, #source_state, #state_machine

Instance Method Summary collapse

Methods inherited from Transition

#allowed?, #initialize, #inspect, make, types

Constructor Details

This class inherits a constructor from StateMachine::Transition

Instance Method Details

#armObject


331
332
333
334
335
336
337
338
339
340
341
342
# 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_descriptionObject


325
326
327
328
# File 'lib/motion-state-machine/transition.rb', line 325

def event_description
  "after #{event_trigger_value} seconds of "\
  "#{source_state.name} (timeout)"
end

#unarmObject


344
345
346
347
# File 'lib/motion-state-machine/transition.rb', line 344

def unarm
  @state_machine.log "Timer unarmed"
  @timer.cancel!
end