class FirstPublisher
include Wisper::Publisher
include Wisper::Bubble
def call
broadcast(:it_happened)
end
end
class SecondPublisher
include Wisper::Publisher
def call
first_publisher = FirstPublisher.new
first_publisher.bubble(self)
first_publisher.call
end
end
Downside is that we are including the Bubble
module in the publisher which doesn't actually need to bubble. Also since it is a public method, it can be used for more than just bubbling, but general purpose delegating of events, so maybe should be called #delegate_to
or #relay_to
.
The #bubble
method could be implemented in a number of ways.
OR
class FirstPublisher
include Wisper::Publisher
def call
broadcast(:it_happened)
end
end
class SecondPublisher
include Wisper::Publisher
include Wisper::Bubble
def call
first_publisher = bubble(FirstPublisher.new)
first_publisher.call
end
end
#bubble
would subscribe a "relay" (listener which accepts all broadcast events via method_missing) to the publisher, which relays (i.e. re-broadcasts) events to a target, self
.
def bubble(publisher)
publisher.subscribe(Wisper::Bubble::RelayListener.new(self))
publisher
end
class RelayListener
def initialize(target)
end
def method_missing(method_name, *args)
target.broadcast(method_name, *args) # FIXME: broadcast is a private method [1]
end
end
# [1] could be 'fixed' by adding a public method, provided by `Wisper::Bubble`, e.g. `rebroadcast`, which then calls `broadcast`.