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

index dd7321e..be22b1c 100644 (file)
@@ -65,6 +65,18 @@ public class ChatRoom
       throw new InvalidUsernameException(user);
 
     Message.MessageKey key = Message.MessageKey.of(user, id);
+
+    Mono<Message> other = 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) ->
@@ -79,16 +91,7 @@ public class ChatRoom
         }
       });
 
-      return mono.<Mono<Message>>switchIfEmpty(service
-    .persistMessage(key, LocalDateTime.now(clock), text)
-    .<Mono<Message>>doOnNext(m ->
-    {
-      Sinks.EmitResult result = sink.tryEmitNext(m);
-      if (result.isFailure())
-      {
-        log.warn("Emitting of message failed with {} for {}", result.name(), m);
-      }
-    }));
+      return mono.switchIfEmpty(other);
   }