Last active
February 7, 2017 07:28
-
-
Save krisleech/8197481 to your computer and use it in GitHub Desktop.
New Wisper feature, an alternative to global listeners: class scoped listeners.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
krisleech/wisper#31