Sha256: 999a15d28775c8b9dfb4d88e788750811ee6f37262bf60f1d75ef5fc139b2d7a

Contents?: true

Size: 1.49 KB

Versions: 270

Compression:

Stored size: 1.49 KB

Contents

module ActiveSupport
  module Notifications
    # This is a default queue implementation that ships with Notifications.
    # It just pushes events to all registered log subscribers.
    class Fanout
      def initialize
        @subscribers = []
        @listeners_for = {}
      end

      def subscribe(pattern = nil, block = Proc.new)
        subscriber = Subscriber.new(pattern, block).tap do |s|
          @subscribers << s
        end
        @listeners_for.clear
        subscriber
      end

      def unsubscribe(subscriber)
        @subscribers.reject! {|s| s.matches?(subscriber)}
        @listeners_for.clear
      end

      def publish(name, *args)
        listeners_for(name).each { |s| s.publish(name, *args) }
      end

      def listeners_for(name)
        @listeners_for[name] ||= @subscribers.select { |s| s.subscribed_to?(name) }
      end

      def listening?(name)
        listeners_for(name).any?
      end

      # This is a sync queue, so there is no waiting.
      def wait
      end

      class Subscriber #:nodoc:
        def initialize(pattern, delegate)
          @pattern = pattern
          @delegate = delegate
        end

        def publish(message, *args)
          @delegate.call(message, *args)
        end

        def subscribed_to?(name)
          !@pattern || @pattern === name.to_s
        end

        def matches?(subscriber_or_name)
          self === subscriber_or_name ||
            @pattern && @pattern === subscriber_or_name
        end
      end
    end
  end
end

Version data entries

270 entries across 226 versions & 25 rubygems

Version Path
activesupport-3.2.10 lib/active_support/notifications/fanout.rb
activesupport-3.1.9 lib/active_support/notifications/fanout.rb
classiccms-0.6.9 vendor/bundle/gems/activesupport-3.2.3/lib/active_support/notifications/fanout.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
jquery_regex-0.0.3 vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.2/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
jquery_regex-0.0.3 vendor/bundle/gems/jquery_regex-0.0.3/vendor/bundle/gems/jquery_regex-0.0.1/vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
jquery_regex-0.0.1 vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
dirty_history-0.7.3 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/fanout.rb
dirty_history-0.7.2 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/fanout.rb
dirty_history-0.7.1 dirty_history/ruby/1.9.1/gems/activesupport-3.1.1/lib/active_support/notifications/fanout.rb
challah-rolls-0.2.0 vendor/bundle/gems/activesupport-3.2.9/lib/active_support/notifications/fanout.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/activesupport-3.2.8/lib/active_support/notifications/fanout.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/activesupport-3.2.7/lib/active_support/notifications/fanout.rb
challah-rolls-0.2.0 vendor/bundle/gems/activesupport-3.2.7/lib/active_support/notifications/fanout.rb
challah-rolls-0.2.0 vendor/bundle/gems/activesupport-3.2.8/lib/active_support/notifications/fanout.rb