Question

I have an application that uses EventBus for dispatching Application wide events. For some reason if I call one event and then try to register handler immediately before firing the second event it does not get dispatched. Is there any other way to dynamically register handlers on event ? Please see the code below:

MyEntry.java

package com.example.eventbus.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class MyEntry 
implements EntryPoint {
    SimpleEventBus bus;
    @Override
    public void onModuleLoad() {
        bus = new SimpleEventBus();
        fireEvent1();
    }

    private void fireEvent1(){
      bus.addHandler(MyEvent1.TYPE,new MyEvent1.Handler() {

          @Override
          public void onEvent1(MyEvent1 event) {
              RootPanel.get().add(new Label("Event1"));
              fireEvent2();
          }
      });
      bus.fireEvent(new MyEvent1());
    }

    private void fireEvent2(){
        bus.addHandler(MyEvent2.TYPE,new MyEvent2.Handler() {

            @Override
            public void onEvent2(MyEvent2 event) {
                RootPanel.get().add(new Label("Event2")); //!!!!!This line is not being called
            }
        });
        bus.fireEvent(new MyEvent2());
    }
}

MyEvent1.java

package com.example.eventbus.client;

import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;

public class MyEvent1 extends GwtEvent<MyEvent1.Handler>{
    public static Type<MyEvent1.Handler> TYPE=new Type<MyEvent1.Handler>();

    @Override
    public com.google.gwt.event.shared.GwtEvent.Type<Handler> getAssociatedType() {
        return TYPE;
    }

    @Override
    protected void dispatch(Handler handler) {
        System.out.println("dispatch Event1");
        handler.onEvent1(this);
    }

    public interface Handler extends EventHandler{
        public void onEvent1(MyEvent1 event);
    }
}

MyEvent2.java

package com.example.eventbus.client;

import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;

public class MyEvent2 extends GwtEvent<MyEvent2.Handler>{
    public static Type<MyEvent2.Handler> TYPE=new Type<MyEvent2.Handler>();

    @Override
    public com.google.gwt.event.shared.GwtEvent.Type<Handler> getAssociatedType() {
        return TYPE;
    }

    @Override
    protected void dispatch(Handler handler) {
        System.out.println("dispatch Event2"); //!!!! This line is never called
        handler.onEvent2(this);
    }

    public interface Handler extends EventHandler{
        public void onEvent2(MyEvent2 event);
    }
}
Was it helpful?

Solution

The issue is that while an event bus is firing events, it queues up any added or removed handler, and deals with them when it is finished. The idea is that if you only start listening to something while another event is still going off, then you are not interested for this round of events, but for the next round. The same thing happens when removing a handler - you will still hear about events that are still in the process of happening, but after the event bus is finished firing, it will deal with removal.

Try changing your code to wire up both handlers before firing anything, then try firing one event to the other.

OTHER TIPS

Let me suggest to take a try to a new feature in gwtquery called 'custom events'.

We have introduced the new events mechanism during the recent GWT.create conferences, take a look to this slide (use arrows to move between slides).

You can attach custom events to any element in the DOM tree, widgets, and the window element.

Then you can trigger the event from any point in your code and every handler with this custom event name will be executed.

The main goal of the gQuery approach apart from being simpler, is that it performs significantly better than gwt eventbus, since the native browser selects in a low-level way which handlers to execute instead of using javascript to loop over a list, and execute handlers sequentially.

You might want to check out GWTP @ProxyEvent:

So when should you use @ProxyEvent? Basically, you use it every time that an event should have the ability to “wake up” your presenter. That is, whenever the presenter should be notified of an event even before it is first instantiated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top