Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active July 16, 2019 14:49
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/09c3f41d5ec943bd49c9058a67306eea to your computer and use it in GitHub Desktop.
Save krisleech/09c3f41d5ec943bd49c9058a67306eea to your computer and use it in GitHub Desktop.
Simple Event Bus based on Wisper
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