Basic button, but reactive

An example of a using Reactive classes with Medusa controllers

Back to overview

Reactive method usage

Any controller method, be it setupAttributes() or any action method, provides two different implementations. You can either use a simple List when not using anything but a simple implementation. Or, when using reactive streams, you could use Mono > instead.

Behind the scenes, we're always using Reactive streams. If you use a List , we will wrap it with a Mono internally.

0
JDK22.0.2 w/ Medusa 0.9.6-SNAPSHOT

Client

<div th:text="${counter}"></div>

<button m:click="updateCounter(1)">Increase counter</button>
<button m:click="reset()">Reset counter</button>

Server

import io.getmedusa.medusa.core.annotation.UIEventPage;
import io.getmedusa.medusa.core.attributes.Attribute;
import io.getmedusa.medusa.core.session.Session;
import reactor.core.publisher.Mono;

import java.util.List;

import static io.getmedusa.medusa.core.attributes.Attribute.$$;

@UIEventPage(path = "/detail/reactive", file = "/pages/reactive.html")
public class ReactiveController {

    public Mono<List<Attribute>> setupAttributes() {
        return Mono.just(0)
                .map(i -> $$("counter", i));
    }

    public Mono<List<Attribute>> updateCounter(int amount, Session session) {
        Flux<List<Attribute>> listA = Flux.just($$("listA_valueA", 1, "listA_valueB", 2));

        int counter = session.getAttribute("counter");
        counter += amount;
        final Mono<List<Attribute>> listB = Mono.just($$("counter", counter));

        return FluxUtils.join(listA, listB); //convenience method for joining multiple retrievals
    }
}