From 4c6e2f71ff2816c61cec32117b5abd95f0142063 Mon Sep 17 00:00:00 2001 From: David Badura Date: Tue, 9 Jan 2024 15:57:56 +0100 Subject: [PATCH] execute archive only once --- src/Repository/DefaultRepository.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Repository/DefaultRepository.php b/src/Repository/DefaultRepository.php index de58dedfc..eacdfa70f 100644 --- a/src/Repository/DefaultRepository.php +++ b/src/Repository/DefaultRepository.php @@ -281,17 +281,25 @@ private function archive(Message ...$messages): void return; } + $lastMessageWithNewStreamStart = null; + foreach ($messages as $message) { if (!$message->newStreamStart()) { continue; } - $this->store->archiveMessages( - $message->aggregateClass(), - $message->aggregateId(), - $message->playhead(), - ); + $lastMessageWithNewStreamStart = $message; + } + + if ($lastMessageWithNewStreamStart === null) { + return; } + + $this->store->archiveMessages( + $lastMessageWithNewStreamStart->aggregateClass(), + $lastMessageWithNewStreamStart->aggregateId(), + $lastMessageWithNewStreamStart->playhead(), + ); } /** @return Traversable */