Skip to content

Commit

Permalink
Fixes codeka#3. In non-debug builds, registering twice on the event b…
Browse files Browse the repository at this point in the history
…us is silently ignored.
  • Loading branch information
codeka committed Oct 10, 2014
1 parent 9986766 commit 40ff222
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 10 additions & 1 deletion client/src/au/com/codeka/warworlds/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import au.com.codeka.common.Log;
import au.com.codeka.warworlds.Util;

/**
* An implementation of the "event bus" pattern.
*
Expand All @@ -21,6 +24,8 @@
* 3. The implementation handles all the details of handling multiple callbacks and so on.
*/
public class EventBus {
private static Log log = new Log("EventBus");

private final List<EventHandlerInfo> mHandlers = new CopyOnWriteArrayList<EventHandlerInfo>();

/** Subscribe the given object to the event bus. */
Expand All @@ -29,7 +34,11 @@ public void register(Object subscriber) {
for (EventHandlerInfo handler : mHandlers) {
Object existingSubscriber = handler.getSubscriber();
if (existingSubscriber != null && existingSubscriber == subscriber) {
throw new AlreadyRegisteredException();
if (Util.isDebug()) {
throw new AlreadyRegisteredException();
} else {
log.error("EventBus.register() called twice on the same object, ignoring second call.");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE); // remove the title bar

mFleetList = new FleetList(this);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
addContentView(mFleetList, lp);

mFleetList.setOnFleetActionListener(new FleetList.OnFleetActionListener() {
Expand Down Expand Up @@ -108,7 +109,7 @@ public void onPause() {
StarManager.eventBus.unregister(mEventHandler);
}

private Object mEventHandler = new Object() {
private final Object mEventHandler = new Object() {
@EventHandler
public void onStarUpdated(Star star) {
if (mStar != null && !mStar.getKey().equals(star.getKey())) {
Expand Down

0 comments on commit 40ff222

Please sign in to comment.