Using SimpleEventBus the following does not behave like you expect:
eventBus.addHandler(FirstEvent.TYPE, new FirstHandler() {
void onFirstEvent(FirstEvent e) {
//will be enqueued to be added. So handler is not fully registered
eventBus.addHandler(SecondEvent.TYPE, new SecondHandler() {
void onSecondEvent(SecondEvent e) {
sysout("second event")
}
}
//wont trigger handler from above
eventBus.fire(new SecondEvent());
sysout("first event done");
}
});
The output would be "first event done" and you wont see "second event" on the console. As Thomas said handlers that you add to the event bus while an event is actually dispatched (in this case FirstEvent) will cause the SimpleEventBus to put the new handler into a queue. After all events are dispatched the contents of this queue is added to the list of known handlers ready to be executed. But as all events are already dispatched these new handlers wont get executed.
And thats exactly what happens with your original code. Once your DataReceiver has finished loading your data you fire an event "OnSometimeEvent". The handler of this event calls continueStart() which in turn adds a handler to the event bus via historyHandler.register(...) and then fires a second event via historyHandler.handleCurrentHistory(). Because you register the history handler while your OnSometimeEvent gets dispatched this handler will be stored in a queue in SimpleEventBus. Then the second event will be dispatched (fired by historyHandler.handleCurrentHistory()) and after this event has been executed the handler in the queue will be added to the list of known handlers.
To solve this you either have to refactor your code in a way that all handlers are registered to the event bus before OnSometimeEvent is fired or you have to break your code execution flow by taking the continueStart() method and defer it until OnSometimeEvent has been dispatched.
So:
public void start() {
if(dataReady) {
Scheduler.get().scheduleFinally(
//....
continueStart();
);
} else { //wait }
}
should do the trick. If scheduleFinally does not work try scheduleDeferred which executes the code even later. If you want to understand the Scheduler class you should read something about the browser event loop.
-- J.