Sha256: b8cf808721935ece397326059ca6fe57c5e44463f7507814f3628aee4cdf4868

Contents?: true

Size: 1.3 KB

Versions: 20

Compression:

Stored size: 1.3 KB

Contents

module ActionCable
  module SubscriptionAdapter
    class SubscriberMap
      def initialize
        @subscribers = Hash.new { |h, k| h[k] = [] }
        @sync = Mutex.new
      end

      def add_subscriber(channel, subscriber, on_success)
        @sync.synchronize do
          new_channel = !@subscribers.key?(channel)

          @subscribers[channel] << subscriber

          if new_channel
            add_channel channel, on_success
          elsif on_success
            on_success.call
          end
        end
      end

      def remove_subscriber(channel, subscriber)
        @sync.synchronize do
          @subscribers[channel].delete(subscriber)

          if @subscribers[channel].empty?
            @subscribers.delete channel
            remove_channel channel
          end
        end
      end

      def broadcast(channel, message)
        list = @sync.synchronize do
          return if !@subscribers.key?(channel)
          @subscribers[channel].dup
        end

        list.each do |subscriber|
          invoke_callback(subscriber, message)
        end
      end

      def add_channel(channel, on_success)
        on_success.call if on_success
      end

      def remove_channel(channel)
      end

      def invoke_callback(callback, message)
        callback.call message
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
actioncable-5.1.7 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.7.rc1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.6.2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.6.1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.6 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.5 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.5.rc1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.4 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.4.rc1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.3 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.3.rc3 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.3.rc2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.3.rc1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.2.rc1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.0 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.0.rc2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.0.rc1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-5.1.0.beta1 lib/action_cable/subscription_adapter/subscriber_map.rb