Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active February 13, 2018 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/b63cd60422d5089964734fb32e60b803 to your computer and use it in GitHub Desktop.
Save krisleech/b63cd60422d5089964734fb32e60b803 to your computer and use it in GitHub Desktop.
Sketch for Wisper::Bubble
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment