class StateMachine::NotificationTransition

Triggered on a specified NSNotification name. Created when supplying an :on_notification option in the transition definition.

On entering the source state, the transition registers itself as NSNotification observer on the default NSNotificationCenter. It deregisters when the state is exited.

@example

state_machine.when :awake do |state|
  state.transition_to :sleeping,
    on_notificaiton: UIApplicationDidEnterBackgroundNotification
end

Public Class Methods

new(options) click to toggle source
Calls superclass method StateMachine::Transition::new
# File lib/motion-state-machine/transition.rb, line 368
def initialize(options)
  super options
end

Public Instance Methods

arm() click to toggle source
# File lib/motion-state-machine/transition.rb, line 376
def arm
  @observer = NSNotificationCenter.defaultCenter.addObserverForName event_trigger_value, 
    object: nil, 
    queue: NSOperationQueue.mainQueue, 
    usingBlock: -> notification {
      handle_in_initial_queue
      state_machine.log "Registered notification #{event_trigger_value}"
    }      
end
event_description() click to toggle source
# File lib/motion-state-machine/transition.rb, line 372
def event_description
  "after getting a #{event_trigger_value}"
end
unarm() click to toggle source
# File lib/motion-state-machine/transition.rb, line 386
def unarm
  NSNotificationCenter.defaultCenter.removeObserver @observer
  @state_machine.log "Removed as observer"
end

Private Instance Methods

handle_in_initial_queue() click to toggle source

This makes sure that state entry/exit and transition actions are called within the initial queue/thread.

# File lib/motion-state-machine/transition.rb, line 397
def handle_in_initial_queue
  if state_machine.initial_queue.to_s == Dispatch::Queue.main.to_s
    handle_in_source_state
  else
    state_machine.initial_queue.async do
      handle_in_source_state
    end
  end
end