Class: StateMachine::NotificationTransition

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

Overview

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.

Examples:

state_machine.when :awake do |state|
  state.transition_to :sleeping,
    on_notificaiton: UIApplicationDidEnterBackgroundNotification
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?, #inspect, make, types

Constructor Details

#initialize(options) ⇒ NotificationTransition

Returns a new instance of NotificationTransition.


368
369
370
# File 'lib/motion-state-machine/transition.rb', line 368

def initialize(options)
  super options
end

Instance Method Details

#armObject


376
377
378
379
380
381
382
383
384
# 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_descriptionObject


372
373
374
# File 'lib/motion-state-machine/transition.rb', line 372

def event_description
  "after getting a #{event_trigger_value}"
end

#unarmObject


386
387
388
389
# File 'lib/motion-state-machine/transition.rb', line 386

def unarm
  NSNotificationCenter.defaultCenter.removeObserver @observer
  @state_machine.log "Removed as observer"
end