AppJars / commons-frontend

Commons utilities for front-end enterprise features
Apache License 2.0
1 stars 1 forks source link

Vaadin Util - UI Event Bus #6

Closed javier-godoy closed 2 years ago

javier-godoy commented 2 years ago

The following utility methods (inspired by https://cookbook.vaadin.com/ui-eventbus) allow dynamically registering a UI event bus that is active while the component is attached, and triggering events against such event bus.

public static <T extends ComponentEvent<?>> void addUIListener(Component component, Class<T> eventType,
        ComponentEventListener<T> listener) {
    component.addAttachListener(ev -> {
        Registration registration = ComponentUtil.addListener(ev.getUI(), eventType, listener);
        component.addDetachListener(event -> registration.remove());
    });
}

public static void fireUIEvent(ComponentEvent<?> event) {
    UI ui = event.getSource().getUI().get();
    ComponentUtil.fireEvent(ui, event);
}

This contribution contains Personal Intellectual Property that is and remains owned by its Author. To the fullest extent possible, the Appjars Project and its controlling shareholder are granted a non-exclusive, irrevocable and transferable license under the terms stated in section 1(D) of the Amendment to the Agreement on Intellectual Property in effect.