Module: Ably::Modules::MessageEmitter
- Included in:
- Realtime::Channel, Realtime::Presence
- Defined in:
- 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
52 53 54 55 |
# File 'lib/ably/modules/message_emitter.rb', line 52 def (name, payload) [:all].each { |cb| cb.call(payload) } [name].each { |cb| cb.call(payload) } if name end |
#subscribe(*names) {|Object| ... } ⇒ void
This method returns an undefined value.
Subscribe to events on this object
14 15 16 17 18 19 20 |
# File 'lib/ably/modules/message_emitter.rb', line 14 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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ably/modules/message_emitter.rb', line 29 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 |