Search code examples
gwtgwtpgwt-platformevent-bus

My fired GWT event is not handled by the parent presenter


I have a child presenter (added to the parent in a slot) that fires an event through the eventBus (on a condition) when onBind() is called :

class ChildPresenter extends PresenterWidget<?> {

        void onBind() {

               instance = initMyInstance();

               if (instance == null) {
                        eventBus.fireEvent(new MyEvent());
               }
        }

       //...
}

On the parent, i handle the event in the onBind() method like this :

class ParentPresenter extends Presenter<..>{

     void onBind() {

           eventBus.addHandler(MyEvent.TYPE, new MyEventHandler() {...} );
     }
}

I tried also this in the parent: addRegisteredHandler(LoggedOutEvent.TYPE, this) and addVisibleHandler(...) too, where i implement the MyEventHandler interface in the parent presenter but it does not handle the fired event.

Am i doing it wrong? I noticed also when i move the handling process to the onReveal() method of my parent presenter, it worked but twice!!


Solution

  • Most likely you have a timing issue. Make sure that addHandler() in the parent is called before fire in the child, and also I think it should happened in a separate browser event. Try this to see if it helps:

    class ChildPresenter extends PresenterWidget<?> {
    
            void onBind() {
    
                   instance = initMyInstance();
    
                   if (instance == null) {
                       Scheduler.get().scheduleDeferred(new ScheduledCommand() {
    
                           @Override
                           public void execute() {
                                eventBus.fireEvent(new MyEvent());
                           }
                       });
    
                   }
            }
    
           //...
    }