Class: HotCocoa::NotificationListener

Inherits:
Object
  • Object
show all
Defined in:
lib/hotcocoa/notification_listener.rb

Overview

Object that can register a block to receive arbitrary notifications from the default NSNotificationCenter or NSDistributedNotificationCenter.

Constant Summary

DistributedBehaviors =

Map Ruby-ish names for distributed behaviors.

{
  drop:                NSNotificationSuspensionBehaviorDrop,
  coalesce:            NSNotificationSuspensionBehaviorCoalesce,
  hold:                NSNotificationSuspensionBehaviorHold,
  deliver_immediately: NSNotificationSuspensionBehaviorDeliverImmediately
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|notification| ... } ⇒ NotificationListener

TODO:

Would be better to use #define_singleton_method to define a new #receive for each instance?

Returns a new instance of NotificationListener

Yields:

  • the block given here will become the callback for the notification

Yield Parameters:

  • notification (String)

    the name of the notification received



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hotcocoa/notification_listener.rb', line 68

def initialize options = {}, &block
  unless block_given?
    raise ArgumentError, 'You must pass a block to act as the callback'
  end
  @callback = block

  @name                = options[:named]
  @sender              = options[:sent_by]
  @suspension_behavior = DistributedBehaviors[options[:when_suspended] || :coalesce]
  @distributed         = (options[:distributed] == true)
  self.class.registered_listeners << self
  observe
end

Class Attribute Details

.registered_listenersHotCocoa::NotificationListener (readonly)



45
46
47
# File 'lib/hotcocoa/notification_listener.rb', line 45

def registered_listeners
  @registered_listeners
end

Instance Attribute Details

#distributedBoolean (readonly) Also known as: distributed?

Whether or not the object is registered with the default distributed or regular notification center.



37
38
39
# File 'lib/hotcocoa/notification_listener.rb', line 37

def distributed
  @distributed
end

#nameString (readonly)

Name of the notification to listen for.



21
22
23
# File 'lib/hotcocoa/notification_listener.rb', line 21

def name
  @name
end

#senderString (readonly)

Name of the notification sender.



27
28
29
# File 'lib/hotcocoa/notification_listener.rb', line 27

def sender
  @sender
end

#suspension_behaviorNSNotificationSuspensionBehavior (readonly)



30
31
32
# File 'lib/hotcocoa/notification_listener.rb', line 30

def suspension_behavior
  @suspension_behavior
end

Instance Method Details

#receive(notification) ⇒ Object

The callback called when a notification is posted. You should not be directly calling this yourself.



108
109
110
# File 'lib/hotcocoa/notification_listener.rb', line 108

def receive notification
  @callback.call notification
end

#stop_listening(options = {}) ⇒ Object

Stop the listener from listening to any future notifications. The options available here are the same as the #initialize methods :named and :sent_by options.



94
95
96
97
98
99
100
101
102
103
# File 'lib/hotcocoa/notification_listener.rb', line 94

def stop_listening options = {}
  if options.has_key?(:named) || options.has_key?(:sent_by)
    notification_center.removeObserver self,
                                 name: options[:named],
                               object: options[:sent_by]
  else
    notification_center.removeObserver self
  end
  self.class.registered_listeners.delete self
end

#stop_notifications(options = {}) ⇒ Object

Deprecated.

Use #stop_listening instead. This API is scheduled to be removed in HotCocoa 0.7.

Stop the listener from listening to any future notifications. The options available here are the same as the #initialize methods :named and :sent_by options.



86
87
88
# File 'lib/hotcocoa/notification_listener.rb', line 86

def stop_notifications options = {}
  stop_listening options
end