module Garufa::Subscriptions

Public Instance Methods

add(subscription) click to toggle source
# File lib/garufa/subscriptions.rb, line 19
def add(subscription)
  subs = subscriptions[subscription.channel] ||= Set.new
  @semaphore.synchronize { subs.add subscription }
end
all() click to toggle source
# File lib/garufa/subscriptions.rb, line 11
def all
  subscriptions
end
channel_size(channel) click to toggle source
# File lib/garufa/subscriptions.rb, line 59
def channel_size(channel)
  stats.channel_size(channel)
end
channel_stats(channel) click to toggle source
# File lib/garufa/subscriptions.rb, line 63
def channel_stats(channel)
  stats.single_channel(channel)
end
include?(subscription) click to toggle source
# File lib/garufa/subscriptions.rb, line 54
def include?(subscription)
  subs = subscriptions[subscription.channel]
  subs && subs.include?(subscription)
end
notify(channels, event, options = {}) click to toggle source
# File lib/garufa/subscriptions.rb, line 30
def notify(channels, event, options = {})
  channels.each do |channel|
    notify_channel(channel, event, options)
  end
end
notify_channel(channel, event, options) click to toggle source
# File lib/garufa/subscriptions.rb, line 36
def notify_channel(channel, event, options)
  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 == options[:socket_id]

      # Skip notifying the same member (probably from different tabs)
      next if sub.presence_channel? and sub.channel_data == options[:data]

      sub.notify Message.channel_event(channel, event, options[:data])
    end
  }
end
remove(subscription) click to toggle source
# File lib/garufa/subscriptions.rb, line 24
def remove(subscription)
  channel = subscription.channel
  subscriptions[channel].delete subscription
  subscriptions.delete(channel) if channel_size(channel).zero?
end
stats() click to toggle source
# File lib/garufa/subscriptions.rb, line 15
def stats
  @stats ||= API::Stats.new(subscriptions)
end

Private Instance Methods

subscriptions() click to toggle source
# File lib/garufa/subscriptions.rb, line 69
def subscriptions
  @subscriptions ||= {}
end