Alternative to Timer.java class of cuba in jmix 2.0

I dont see Timer class in jmix 2.0. Is there any alternative to Timer cuba facet in jmix 2.0?

2 Likes

Hello!

I’ve created an issue: Implement Timer facet · Issue #2106 · jmix-framework/jmix · GitHub.

Due to calling JavaScript becomes easier you can implement similar timer component. For instance:

@Tag(Tag.DIV)
public class MyTimer extends HtmlComponent {

    public MyTimer() {
        getElement().getStyle().set("display", "none");
        getElement().executeJs("""
                        const elem = this;
                        this.onIntervalEvent = function() {
                            let event = new Event("timerEvent");
                            elem.dispatchEvent(event);
                        }""");

        getElement().addEventListener("timerEvent", event -> {
            fireTimerEvent();
        });
    }

    public void start() {
        getElement().executeJs("this.intervalId = setInterval(this.onIntervalEvent, 1000);");
    }

    public void stop() {
        getElement().executeJs("clearInterval(this.intervalId)");
    }

    protected void fireTimerEvent() {
        getEventBus().fireEvent(new TimerEvent(this, true));
    }

    public Registration addTimerEventListener(ComponentEventListener<TimerEvent> listener) {
        return getEventBus().addListener(TimerEvent.class, listener);
    }

    public static class TimerEvent extends ComponentEvent<MyTimer> {

        public TimerEvent(MyTimer source, boolean fromClient) {
            super(source, fromClient);
        }
    }
1 Like