An example of a using Reactive classes with Medusa controllers
Back to overviewAny controller method, be it setupAttributes() or any action method, provides two different implementations. You can either use a simple List
Behind the scenes, we're always using Reactive streams. If you use a List
<div th:text="${counter}"></div>
<button m:click="updateCounter(1)">Increase counter</button>
<button m:click="reset()">Reset counter</button>
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
}
}