import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;
+import reactor.core.publisher.SynchronousSink;
import java.time.Clock;
import java.time.LocalDateTime;
ChatRoomService service,
int bufferSize)
{
+ log.info("Created ChatRoom {} with buffer-size {}", id, bufferSize);
this.id = id;
this.name = name;
this.shard = shard;
Message.MessageKey key = Message.MessageKey.of(user, id);
return service
.getMessage(key)
- .flatMap(existing -> text.equals(existing.getMessageText())
- ? Mono.just(existing)
- : Mono.error(() -> new MessageMutationException(existing, text)))
+ .handle((Message existing, SynchronousSink<Message> sink) ->
+ {
+ if (existing.getMessageText().equals(text))
+ {
+ sink.next(existing);
+ }
+ else
+ {
+ sink.error(new MessageMutationException(existing, text));
+ }
+ })
.switchIfEmpty(
Mono
- .fromSupplier(() ->service.persistMessage(key, LocalDateTime.now(clock), text))
+ .defer(() -> service.persistMessage(key, LocalDateTime.now(clock), text))
.doOnNext(m ->
{
Sinks.EmitResult result = sink.tryEmitNext(m);