Module: Ably::Modules::MessageEmitter
- Includes:
- SafeYield
- Defined in:
- lib/submodules/ably-ruby/lib/ably/modules/message_emitter.rb
Overview
Message emitter, subscriber and unsubscriber (Pub/Sub) functionality common to Channels and Presence In addition to standard Pub/Sub functionality, it allows subscribers to subscribe to :all.
Instance Method Summary collapse
-
#emit_message(name, payload) ⇒ void
private
Emit a message to message subscribers.
-
#subscribe(*names) {|Object| ... } ⇒ void
Subscribe to events on this object.
-
#unsubscribe(*names, &callback) ⇒ void
Unsubscribe the matching block for events on the this object.
Instance Method Details
#emit_message(name, payload) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Emit a message to message subscribers
param name [String,Symbol] the event name param payload [Object] the event object to emit
55 56 57 58 |
# File 'lib/submodules/ably-ruby/lib/ably/modules/message_emitter.rb', line 55 def (name, payload) [:all].each { |cb| safe_yield(cb, payload) } [name].each { |cb| safe_yield(cb, payload) } if name end |
#subscribe(*names) {|Object| ... } ⇒ void
This method returns an undefined value.
Subscribe to events on this object
17 18 19 20 21 22 23 |
# File 'lib/submodules/ably-ruby/lib/ably/modules/message_emitter.rb', line 17 def subscribe(*names, &callback) raise ArgumentError, 'Block required to subscribe to events' unless block_given? names = :all unless names && !names.empty? Array(names).uniq.each do |name| [(name)] << callback end end |
#unsubscribe(*names, &callback) ⇒ void
This method returns an undefined value.
Unsubscribe the matching block for events on the this object. If a block is not provided, all subscriptions will be unsubscribed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/submodules/ably-ruby/lib/ably/modules/message_emitter.rb', line 32 def unsubscribe(*names, &callback) names = :all unless names && !names.empty? Array(names).each do |name| if name == :all .keys else Array((name)) end.each do |key| [key].delete_if do |block| !block_given? || callback == block end end end end |