WIP:refactor
authorKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 17:45:53 +0000 (18:45 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 17:45:53 +0000 (18:45 +0100)
src/main/java/de/juplo/kafka/chat/backend/domain/ChatRoom.java

index 878d788..88099c0 100644 (file)
@@ -66,17 +66,6 @@ public class ChatRoom
 
     Message.MessageKey key = Message.MessageKey.of(user, id);
 
-    Mono<Message> other = Mono.defer(() -> service
-      .persistMessage(key, LocalDateTime.now(clock), text)
-      .doOnNext(m ->
-      {
-        Sinks.EmitResult result = sink.tryEmitNext(m);
-        if (result.isFailure())
-        {
-          log.warn("Emitting of message failed with {} for {}", result.name(), m);
-        }
-      }));
-
     Mono<Message> mono = service
       .getMessage(key)
       .handle((existing, sink) ->
@@ -91,7 +80,16 @@ public class ChatRoom
         }
       });
 
-      return mono.switchIfEmpty(other);
+      return mono.switchIfEmpty(Mono.defer(() -> service
+        .persistMessage(key, LocalDateTime.now(clock), text)
+        .doOnNext(m ->
+        {
+          Sinks.EmitResult result = sink.tryEmitNext(m);
+          if (result.isFailure())
+          {
+            log.warn("Emitting of message failed with {} for {}", result.name(), m);
+          }
+        })));
   }