Class: HotCocoa::NotificationListener
- 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 collapse
- DistributedBehaviors =
Map Ruby-ish names for distributed behaviors.
{ drop: NSNotificationSuspensionBehaviorDrop, coalesce: NSNotificationSuspensionBehaviorCoalesce, hold: NSNotificationSuspensionBehaviorHold, deliver_immediately: NSNotificationSuspensionBehaviorDeliverImmediately }
Class Attribute Summary collapse
-
.registered_listeners ⇒ HotCocoa::NotificationListener
readonly
List of all NotificationListeners.
Instance Attribute Summary collapse
-
#distributed ⇒ Boolean
(also: #distributed?)
readonly
Whether or not the object is registered with the default distributed or regular notification center.
-
#name ⇒ String
readonly
Name of the notification to listen for.
-
#sender ⇒ String
readonly
Name of the notification sender.
- #suspension_behavior ⇒ NSNotificationSuspensionBehavior readonly
Instance Method Summary collapse
-
#initialize(options = {}) {|notification| ... } ⇒ NotificationListener
constructor
A new instance of NotificationListener.
-
#receive(notification) ⇒ Object
The callback called when a notification is posted.
-
#stop_listening(options = {}) ⇒ Object
Stop the listener from listening to any future notifications.
-
#stop_notifications(options = {}) ⇒ Object
deprecated
Deprecated.
Use #stop_listening instead. This API is scheduled to be removed in HotCocoa 0.7.
Constructor Details
#initialize(options = {}) {|notification| ... } ⇒ NotificationListener
Would be better to use #define_singleton_method to define a new #receive for each instance?
Returns a new instance of NotificationListener.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/hotcocoa/notification_listener.rb', line 68 def initialize = {}, &block unless block_given? raise ArgumentError, 'You must pass a block to act as the callback' end @callback = block @name = [:named] @sender = [:sent_by] @suspension_behavior = DistributedBehaviors[[:when_suspended] || :coalesce] @distributed = ([:distributed] == true) self.class.registered_listeners << self observe end |
Class Attribute Details
.registered_listeners ⇒ HotCocoa::NotificationListener (readonly)
List of all HotCocoa::NotificationListeners.
45 46 47 |
# File 'lib/hotcocoa/notification_listener.rb', line 45 def registered_listeners @registered_listeners end |
Instance Attribute Details
#distributed ⇒ Boolean (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 |
#name ⇒ String (readonly)
Name of the notification to listen for.
21 22 23 |
# File 'lib/hotcocoa/notification_listener.rb', line 21 def name @name end |
#sender ⇒ String (readonly)
Name of the notification sender.
27 28 29 |
# File 'lib/hotcocoa/notification_listener.rb', line 27 def sender @sender end |
#suspension_behavior ⇒ NSNotificationSuspensionBehavior (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 = {} if .has_key?(:named) || .has_key?(:sent_by) notification_center.removeObserver self, name: [:named], object: [:sent_by] else notification_center.removeObserver self end self.class.registered_listeners.delete self end |
#stop_notifications(options = {}) ⇒ Object
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 = {} stop_listening end |