Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active February 7, 2017 07:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/8197481 to your computer and use it in GitHub Desktop.
Save krisleech/8197481 to your computer and use it in GitHub Desktop.
New Wisper feature, an alternative to global listeners: class scoped listeners.
# without new Wisper feature:
# interactor
class CreateDelivery
include Wisper::Publisher
# alternative constructor which also adds some listeners
def self.with_default_listeners
new.tap(&:add_default_listeners)
end
def execure(attributes)
delivery = # ...
broadcast(:create_delivery_successful, delivery)
end
# problem is we are adding hard dependencies here...
def add_default_listeners
[StatsListener.new, NotifierListener.new].each { |listener| subscribe(listener) }
end
end
# usage
interactor = CreateDelivery.with_default_listeners
interactor.execute(params[:form])
# With new Wisper feature:
# adds listeners to all instances of given class, instead of globally.
CreateDelivery.add_listener(StatsListener.new)
CreateDelivery.add_listener(NotifierListener.new)
# This would have the same effect as the first code, except:
# * listeners are no longer hardcoded in the class
# * there is no need for a special constructor
# interactor
class CreateDelivery
include Wisper::Publisher
def execure(attributes)
delivery = # ...
broadcast(:create_delivery_successful, delivery)
end
end
# usage
interactor = CreateDelivery.new
interactor.execute(params[:form])
@krisleech
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment