package org.javacodegeeks.examples.v3.bus;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;

public class ReactorEventBusImpl<T> {
    private final Sinks.Many<T> sink = Sinks.many().replay().limit(5);

    private final Flux<T> eventFlux = sink.asFlux()
        .publishOn(Schedulers.newParallel("bus"));

    public void publish(T event) {
        sink.emitNext(event, Sinks.EmitFailureHandler.busyLooping(Duration.ofMinutes(1)));
    }

    public Flux<T> receive() {

        return eventFlux;
    }
}
