Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callbacks to EventBus #2002

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions guava/src/com/google/common/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ void handleSubscriberException(Throwable e, SubscriberExceptionContext context)
* @param object object whose subscriber methods should be registered.
*/
public void register(Object object) {
subscribers.register(object);
onRegister(object);
subscribers.register(object);
}

/**
Expand All @@ -193,7 +194,8 @@ public void register(Object object) {
* @throws IllegalArgumentException if the object was not previously registered.
*/
public void unregister(Object object) {
subscribers.unregister(object);
onUnregister(object);
subscribers.unregister(object);
}

/**
Expand All @@ -210,13 +212,51 @@ public void unregister(Object object) {
public void post(Object event) {
Iterator<Subscriber> eventSubscribers = subscribers.getSubscribers(event);
if (eventSubscribers.hasNext()) {
onEventPosted(event);
dispatcher.dispatch(event, eventSubscribers);
} else if (!(event instanceof DeadEvent)) {
onNoSubscriberRegistered(event);
// the event had no subscribers and was not itself a DeadEvent
post(new DeadEvent(this, event));
}
}

/**
* Callback before registration of all subscriber methods on {@code object}.
*
* @param object object whose subscriber methods should be registered.
*/
protected void onRegister(Object object) {

}

/**
* Callback before unregistration of all subscriber methods on a registered {@code object}.
*
* @param object object whose subscriber methods should be unregistered.
*/
protected void onUnregister(Object object) {

}

/**
* Callback before {@code event} is posted to all registered subscribers.
* @param event event to post.
*
*/
protected void onEventPosted(final Object event) {

}

/**
* Callback when no subscribers have been subscribed for {@code event}'s class.
*
* @param event event to post.
*/
protected void onNoSubscriberRegistered(final Object event) {

}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down