Sha256: c96a4eb97cf04e0d9919e287f04ac9675ae67f421972f96bcfd732a4f1df73e7

Contents?: true

Size: 1.35 KB

Versions: 19

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

# :markup: markdown

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

19 entries across 19 versions & 2 rubygems

Version Path
actioncable-8.0.0 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.2.2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-8.0.0.rc2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.2.1.2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-8.0.0.rc1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.2.1.1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-8.0.0.beta1 lib/action_cable/subscription_adapter/subscriber_map.rb
omg-actioncable-8.0.0.alpha9 lib/action_cable/subscription_adapter/subscriber_map.rb
omg-actioncable-8.0.0.alpha8 lib/action_cable/subscription_adapter/subscriber_map.rb
omg-actioncable-8.0.0.alpha7 lib/action_cable/subscription_adapter/subscriber_map.rb
omg-actioncable-8.0.0.alpha4 lib/action_cable/subscription_adapter/subscriber_map.rb
omg-actioncable-8.0.0.alpha3 lib/action_cable/subscription_adapter/subscriber_map.rb
omg-actioncable-8.0.0.alpha2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.2.1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.2.0 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.2.0.rc1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.2.0.beta3 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.2.0.beta2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.2.0.beta1 lib/action_cable/subscription_adapter/subscriber_map.rb