Module: Garufa::Subscriptions
Instance Method Summary collapse
- #add(subscription) ⇒ Object
- #all ⇒ Object
- #channel_size(channel) ⇒ Object
- #include?(subscription) ⇒ Boolean
- #notify(channels, event, options = {}) ⇒ Object
- #notify_channel(channel, event, options) ⇒ Object
- #remove(subscription) ⇒ Object
Instance Method Details
#add(subscription) ⇒ Object
14 15 16 17 |
# File 'lib/garufa/subscriptions.rb', line 14 def add(subscription) subs = subscriptions[subscription.channel] ||= Set.new @semaphore.synchronize { subs.add subscription } end |
#all ⇒ Object
10 11 12 |
# File 'lib/garufa/subscriptions.rb', line 10 def all subscriptions end |
#channel_size(channel) ⇒ Object
54 55 56 |
# File 'lib/garufa/subscriptions.rb', line 54 def channel_size(channel) (subscriptions[channel] || []).size end |
#include?(subscription) ⇒ Boolean
49 50 51 52 |
# File 'lib/garufa/subscriptions.rb', line 49 def include?(subscription) subs = subscriptions[subscription.channel] subs && subs.include?(subscription) end |
#notify(channels, event, options = {}) ⇒ Object
25 26 27 28 29 |
# File 'lib/garufa/subscriptions.rb', line 25 def notify(channels, event, = {}) channels.each do |channel| notify_channel(channel, event, ) end end |
#notify_channel(channel, event, options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/garufa/subscriptions.rb', line 31 def notify_channel(channel, event, ) return if channel_size(channel).zero? subs = subscriptions[channel] @semaphore.synchronize { subs.each do |sub| # Skip notifying if the same socket_id is provided next if sub.socket_id == [:socket_id] # Skip notifying the same member (probably from different tabs) next if sub.presence_channel? and sub.channel_data == [:data] sub.notify Message.channel_event(channel, event, [:data]) end } end |
#remove(subscription) ⇒ Object
19 20 21 22 23 |
# File 'lib/garufa/subscriptions.rb', line 19 def remove(subscription) channel = subscription.channel subscriptions[channel].delete subscription subscriptions.delete(channel) if channel_size(channel).zero? end |