refactor: Refined return-type of `StorageStrategy#write`
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / implementation / StorageStrategy.java
index c6c5017..94b2bb4 100644 (file)
@@ -6,6 +6,7 @@ import de.juplo.kafka.chat.backend.domain.Message;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
 
 import java.util.UUID;
 
@@ -14,22 +15,24 @@ public interface StorageStrategy
 {
   Logger log = LoggerFactory.getLogger(StorageStrategy.class.getCanonicalName());
 
-  default Flux<ChatRoomInfo> write(ChatHomeService chatHomeService)
+  default Mono<Void> write(ChatHomeService chatHomeService)
   {
     return writeChatRoomInfo(
         chatHomeService
             .getChatRoomInfo()
-            .doOnComplete(() -> log.info("Stored {}", chatHomeService))
-            .doOnError(throwable -> log.error("Could not store {}: {}", chatHomeService, throwable))
-            .doOnNext(chatRoomInfo -> writeChatRoomData(
+            .flatMap(chatRoomInfo -> writeChatRoomData(
                 chatRoomInfo.getId(),
                 chatHomeService
                     .getChatRoomData(chatRoomInfo.getId())
                     .flatMapMany(chatRoomData -> chatRoomData.getMessages())
-                    .doOnComplete(() -> log.info("Stored {}", chatRoomInfo))
-                    .doOnError(throwable -> log.error("Could not store {}: {}", chatRoomInfo, throwable))
                 )
-                .subscribe()));
+                .then(Mono.just(chatRoomInfo))
+                .doOnSuccess(emittedChatRoomInfo -> log.info("Stored {}", chatRoomInfo))
+                .doOnError(throwable -> log.error("Could not store {}: {}", chatRoomInfo, throwable)))
+        )
+        .then()
+        .doOnSuccess(empty -> log.info("Stored {}", chatHomeService))
+        .doOnError(throwable -> log.error("Could not store {}: {}", chatHomeService, throwable));
   }
 
   Flux<ChatRoomInfo> writeChatRoomInfo(Flux<ChatRoomInfo> chatRoomInfoFlux);