Search code examples
javagwterrai

CDI events not firing


I have an Errai app which CDI events are not firing:

@Dependent
@Templated
@Page(path = "person")
public class PersonPage extends Composite {

    @Inject
    @Loaded
    Event<Person> loadedEvent;

    @PageShowing
    public void showing() {
        Browser.getWindow().getConsole().log("Firing event..."); // works fine
        loadedEvent.fire(new Person());
    }

    public void loaded(@Observes @Loaded Person person) {
        Window.alert("Fired");
    }

}

I think this code is pretty straightforward and I wonder why it is not firing.


Solution

  • Your event is not being received because it is a native JS type. The issue is that Errai's Event implementation dispatches based on a Class object for that event type. This works for normal classes and even non-native JS types. But native JS types don't actually have classes at runtime; the compiler replaces any references with JavaScriptObject.class.

    On the observer side, Errai generates code that registers observer methods based on the class name that it figures out at compile time.

    Thus there is a mismatch, where even though it looks like your event is firing for Person events, it is really being dispatched for observers of JavaScriptObject.