Sha256: 53f186ac89dcedaaa3cb5fd85400a223425fc885976ee5201454bba077ccfc45

Contents?: true

Size: 1.33 KB

Versions: 162

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

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

162 entries across 157 versions & 12 rubygems

Version Path
actioncable-7.1.5 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.1.4.2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.0.8.6 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-6.1.7.10 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-6.1.7.9 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.1.4.1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.0.8.5 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.1.4 lib/action_cable/subscription_adapter/subscriber_map.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actioncable-7.0.8.4/lib/action_cable/subscription_adapter/subscriber_map.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actioncable-7.1.3.4/lib/action_cable/subscription_adapter/subscriber_map.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actioncable-7.0.5.1/lib/action_cable/subscription_adapter/subscriber_map.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actioncable-7.0.5.1/lib/action_cable/subscription_adapter/subscriber_map.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actioncable-7.0.5.1/lib/action_cable/subscription_adapter/subscriber_map.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/actioncable-7.1.3.4/lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.1.3.4 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.0.8.4 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-6.1.7.8 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.1.3.2 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.1.3.1 lib/action_cable/subscription_adapter/subscriber_map.rb
actioncable-7.0.8.1 lib/action_cable/subscription_adapter/subscriber_map.rb