Created
March 11, 2018 13:56
-
-
Save vvsevolodovich/41cb2d6fa02aa0d4575a235f4f4a8a8d to your computer and use it in GitHub Desktop.
EventBus
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
public class EventBus { | |
private final SubscriberRegistry subscribers = new SubscriberRegistry(this); | |
public void register(Object object) { | |
subscribers.register(object); | |
} | |
public void unregister(Object object) { | |
subscribers.unregister(object); | |
} | |
public void post(Object event) { | |
Iterator<Subscriber> eventSubscribers = subscribers.getSubscribers(event); | |
if (eventSubscribers.hasNext()) { | |
dispatcher.dispatch(event, eventSubscribers); | |
} else if (!(event instanceof DeadEvent)) { | |
// the event had no subscribers and was not itself a DeadEvent | |
post(new DeadEvent(this, event)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment