Skip to content

Commit

Permalink
Replace EventBus type with Injection Module event bus strategy
Browse files Browse the repository at this point in the history
The event bus maps event types so a developer can publish or observe
event of a specific type. EventBus was a quite useful utility, but now
with type injections, module definitions and async libraries like RxJava
you can use the concept of event bus without the explicit event bus. Now
the event bus is implicit in the whole app. If you wan to emmit events,
you inject Observer<EventType> and if you want to listen to events you
inject Observable<EventType>. This IMO is even better than traditional
event bus bc it force you to describe which kind of event you are
actually using.
  • Loading branch information
ibaca authored and morisil committed Aug 1, 2017
1 parent 6b0ee2d commit a300026
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 27 deletions.
6 changes: 6 additions & 0 deletions github-users-web-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
<compilerArgs>-generateJsInteropExports,-optimize,9,-XnoclassMetadata</compilerArgs>
</configuration>
</plugin>
<plugin>
<!-- just here to make IntelliJ happy -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwt.version}</version>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package com.xemantic.ankh;

import com.intendia.rxgwt.elemental2.RxElemental2;
import com.xemantic.githubusers.logic.eventbus.Trigger;
import com.xemantic.githubusers.logic.event.Trigger;
import elemental2.dom.Element;
import elemental2.dom.Event;
import elemental2.dom.HTMLButtonElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
import com.intendia.gwt.autorest.client.ResourceVisitor;
import com.xemantic.githubusers.logic.driver.UrlOpener;
import com.xemantic.githubusers.logic.error.ErrorAnalyzer;
import com.xemantic.githubusers.logic.eventbus.DefaultEventBus;
import com.xemantic.githubusers.logic.eventbus.EventBus;
import com.xemantic.githubusers.logic.event.SnackbarMessageEvent;
import com.xemantic.githubusers.logic.event.UserQueryEvent;
import com.xemantic.githubusers.logic.event.UserSelectedEvent;
import com.xemantic.githubusers.logic.service.UserService;
import com.xemantic.githubusers.logic.view.*;
import com.xemantic.githubusers.web.driver.WebUrlOpener;
Expand All @@ -38,8 +39,12 @@
import dagger.Module;
import dagger.Provides;

import java.util.function.Consumer;
import javax.inject.Named;
import javax.inject.Singleton;
import rx.Observable;
import rx.Observer;
import rx.subjects.PublishSubject;

/**
* Dagger module defining component binding for the whole app.
Expand Down Expand Up @@ -71,11 +76,20 @@ static String getProjectGitHubUrl() {
return "https://github.com/xemantic/github-users-web";
}

@Provides
@Singleton
static EventBus getEventBus() {
return new DefaultEventBus();
}
// Snackbar message event bus

This comment has been minimized.

Copy link
@ibaca

ibaca Oct 8, 2017

Author Contributor

maybe calling this "message event bus" is not correct, this is just an event channel and is the InjectionModule the bus, as a bus means multiple types of events.

@Provides @Singleton static PublishSubject<SnackbarMessageEvent> snackbarMessageBus() { return PublishSubject.create(); }
@Provides static Consumer<SnackbarMessageEvent> snackbarMessageConsumer(PublishSubject<SnackbarMessageEvent> bus) { return bus::onNext; }
@Binds abstract Observable<SnackbarMessageEvent> snackbarMessageObservable(PublishSubject<SnackbarMessageEvent> bus);

// User query event bus
@Provides @Singleton static PublishSubject<UserQueryEvent> userQueryBus() { return PublishSubject.create(); }
@Provides static Consumer<UserQueryEvent> userQueryConsumer(PublishSubject<UserQueryEvent> bus) { return bus::onNext; }
@Binds abstract Observable<UserQueryEvent> userQueryObservable(PublishSubject<UserQueryEvent> bus);

// User selected event bus
@Provides @Singleton static PublishSubject<UserSelectedEvent> userSelectedBus() { return PublishSubject.create(); }
@Provides static Consumer<UserSelectedEvent> userSelectedConsumer(PublishSubject<UserSelectedEvent> bus) { return bus::onNext; }
@Binds abstract Observable<UserSelectedEvent> userSelectedObservable(PublishSubject<UserSelectedEvent> bus);

@Binds
abstract ErrorAnalyzer getErrorAnalyzer(DefaultErrorAnalyzer analyzer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@

import com.google.gwt.core.client.GWT;
import com.xemantic.githubusers.logic.event.SnackbarMessageEvent;
import com.xemantic.githubusers.logic.eventbus.EventBus;

import java.util.function.Consumer;
import javax.inject.Inject;
import javax.inject.Singleton;
import rx.Observer;

/**
* Handler of uncaught exceptions in this app. It will log the error using
Expand All @@ -39,11 +40,11 @@
@Singleton
public class ExceptionHandler implements GWT.UncaughtExceptionHandler {

private final EventBus eventBus;
private final Consumer<SnackbarMessageEvent> snackbarMessageConsumer;

@Inject
public ExceptionHandler(EventBus eventBus) {
this.eventBus = eventBus;
public ExceptionHandler(Consumer<SnackbarMessageEvent> snackbarMessageConsumer) {
this.snackbarMessageConsumer = snackbarMessageConsumer;
}

/** {@inheritDoc} */
Expand All @@ -54,7 +55,7 @@ public void onUncaughtException(Throwable e) {

// use this method if you need to notify user about certain errors.
private void postSnackbarMessage(String message) {
eventBus.post(new SnackbarMessageEvent(message));
snackbarMessageConsumer.accept(new SnackbarMessageEvent(message));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

import com.xemantic.githubusers.logic.driver.UrlOpener;
import com.xemantic.githubusers.logic.event.UserSelectedEvent;
import com.xemantic.githubusers.logic.eventbus.EventBus;

import javax.inject.Inject;
import javax.inject.Singleton;
import rx.Observable;

/**
* Will open user profile page if the user is selected.
Expand All @@ -37,22 +37,21 @@
@Singleton
public class UserSelectionNavigator {

private final EventBus eventBus;
private final Observable<UserSelectedEvent> userSelected$;

private final UrlOpener urlOpener;

@Inject
public UserSelectionNavigator(
EventBus eventBus,
Observable<UserSelectedEvent> userSelected$,
UrlOpener urlOpener) {

this.eventBus = eventBus;
this.userSelected$ = userSelected$;
this.urlOpener = urlOpener;
}

public void start() {
eventBus.observe(UserSelectedEvent.class)
.subscribe(event -> urlOpener.openUrl(event.getUser().getHtmlUrl()));
userSelected$.subscribe(event -> urlOpener.openUrl(event.getUser().getHtmlUrl()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@

import com.intendia.gwt.autorest.client.RequestResourceBuilder.FailedStatusCodeException;
import com.xemantic.githubusers.logic.event.SnackbarMessageEvent;
import com.xemantic.githubusers.logic.eventbus.EventBus;

import java.util.function.Consumer;
import javax.inject.Inject;
import javax.inject.Singleton;
import rx.Observer;

/**
* Error handler for GitHub APIs.
Expand All @@ -37,11 +38,11 @@
@Singleton
public class GitHubApiErrorHandler {

private final EventBus eventBus;
private final Consumer<SnackbarMessageEvent> snackbarMessageConsumer;

@Inject
public GitHubApiErrorHandler(EventBus eventBus) {
this.eventBus = eventBus;
public GitHubApiErrorHandler(Consumer<SnackbarMessageEvent> snackbarMessageConsumer) {
this.snackbarMessageConsumer = snackbarMessageConsumer;
}

public void handleError(Throwable throwable) {
Expand All @@ -56,7 +57,7 @@ && handleStatusCode((FailedStatusCodeException) throwable)
private boolean handleStatusCode(FailedStatusCodeException e) {
String message = getMessage(e.getStatusCode());
if (message != null) {
eventBus.post(new SnackbarMessageEvent(message));
snackbarMessageConsumer.accept(new SnackbarMessageEvent(message));
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import com.xemantic.ankh.Elements;
import com.xemantic.ankh.IncrementalDom;
import com.xemantic.githubusers.logic.eventbus.Trigger;
import com.xemantic.githubusers.logic.event.Trigger;
import com.xemantic.githubusers.logic.view.DrawerView;
import elemental2.dom.Element;
import mdc.drawer.MDCTemporaryDrawer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import com.xemantic.ankh.Elements;
import com.xemantic.ankh.IncrementalDom;
import com.xemantic.githubusers.logic.eventbus.Trigger;
import com.xemantic.githubusers.logic.event.Trigger;
import com.xemantic.githubusers.logic.view.UserListView;
import com.xemantic.githubusers.logic.view.UserView;
import elemental2.dom.Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package com.xemantic.githubusers.web.view;

import com.xemantic.ankh.Elements;
import com.xemantic.githubusers.logic.eventbus.Trigger;
import com.xemantic.githubusers.logic.event.Trigger;
import com.xemantic.githubusers.logic.model.User;
import com.xemantic.githubusers.logic.view.UserView;
import com.xemantic.ankh.IncrementalDom;
Expand Down

0 comments on commit a300026

Please sign in to comment.