Last active
July 16, 2019 14:49
-
-
Save krisleech/09c3f41d5ec943bd49c9058a67306eea to your computer and use it in GitHub Desktop.
Simple Event Bus based on Wisper
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
class EventBus | |
def initialize | |
@publisher = Class.new do | |
include Wisper::Publisher | |
public :broadcast | |
end.new | |
end | |
def subscribe(listener, options = {}) | |
@publisher.subscribe(listener, options) | |
self | |
end | |
def broadcast(event_name, payload) | |
@publisher.broadcast(event_name, payload) | |
self | |
end | |
end | |
event_bus = EventBus.new | |
listener = double | |
payload = {} | |
event_name = 'my_engine.thing_created' | |
expect(listener).to receive(event_name).with(payload) | |
event_bus.subscribe(listener) | |
event_bus.broadcast(event_name, payload) | |
# Provided events are namspaced to avoid collisions this might be preferable to subscribing globally to concrete classes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment