1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

evented listeners can subscribe to any message

This commit is contained in:
Aaron Patterson 2012-03-21 15:04:22 -07:00
parent 6989db9238
commit c7847f14fd

View file

@ -47,17 +47,19 @@ module ActiveSupport
module Subscribers # :nodoc: module Subscribers # :nodoc:
def self.new(pattern, listener) def self.new(pattern, listener)
if listener.respond_to?(:call) if listener.respond_to?(:call)
if pattern subscriber = Timed.new pattern, listener
TimedSubscriber.new pattern, listener
else else
AllMessages.new pattern, listener subscriber = Evented.new pattern, listener
end end
unless pattern
AllMessages.new(subscriber)
else else
Subscriber.new pattern, listener subscriber
end end
end end
class Subscriber #:nodoc: class Evented #:nodoc:
def initialize(pattern, delegate) def initialize(pattern, delegate)
@pattern = pattern @pattern = pattern
@delegate = delegate @delegate = delegate
@ -81,7 +83,7 @@ module ActiveSupport
end end
end end
class TimedSubscriber < Subscriber class Timed < Evented
def initialize(pattern, delegate) def initialize(pattern, delegate)
@timestack = Hash.new { |h,id| @timestack = Hash.new { |h,id|
h[id] = Hash.new { |ids,name| ids[name] = [] } h[id] = Hash.new { |ids,name| ids[name] = [] }
@ -103,7 +105,23 @@ module ActiveSupport
end end
end end
class AllMessages < TimedSubscriber # :nodoc: class AllMessages # :nodoc:
def initialize(delegate)
@delegate = delegate
end
def start(name, id, payload)
@delegate.start name, id, payload
end
def finish(name, id, payload)
@delegate.finish name, id, payload
end
def publish(name, *args)
@delegate.publish name, *args
end
def subscribed_to?(name) def subscribed_to?(name)
true true
end end