Conversation
lgrossi
left a comment
There was a problem hiding this comment.
I left some suggestions on possible ways to keep leveraging the generators and avoid dumping it to memory as much as possible, but overall the changes lgtm.
|
|
||
| $this->dispatcher->dispatch( | ||
| ...array_map(fn (Message $message) => $this->decorator->decorate($message), $messages) | ||
| array_map(fn (Message $message) => $this->decorator->decorate($message), (array) $messages) |
There was a problem hiding this comment.
Are you sure this works for iterable? I have the impression that this will just return an empty array to the map function, no?
Maybe something like:
| array_map(fn (Message $message) => $this->decorator->decorate($message), (array) $messages) | |
| $decoratedMessages = (function () use ($messages): Generator { | |
| foreach ($messages as $message) { | |
| yield $this->decorator->decorate($message); | |
| } | |
| })(); | |
| $this->dispatcher->dispatch($decoratedMessages); |
There was a problem hiding this comment.
It seems that this is also not covered by tests.
| */ | ||
| public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRootVersion, iterable $events): void | ||
| { | ||
| $events = [...$events]; |
There was a problem hiding this comment.
This will unpack the iterable fully to memory, right? I wonder if there is a way to achieve this by using only generators. I couldn't think on a straightforward way to do so.
| foreach ($events as $event) { | ||
| $messages[] = $this->decorator->decorate(new Message($event, $headers)); | ||
| } |
There was a problem hiding this comment.
To keep leveraging generators, maybe this could pass messages as a generator instead?
| foreach ($events as $event) { | |
| $messages[] = $this->decorator->decorate(new Message($event, $headers)); | |
| } | |
| $messages = (function() use ($events, $headers): Generator { | |
| foreach ($events as $event) { | |
| yield $this->decorator->decorate(new Message($event, $headers)); | |
| } | |
| })(); |
| public function dispatch(Message ...$messages): void | ||
| public function dispatch(iterable|Message $messages): void | ||
| { | ||
| $forwarded = []; |
There was a problem hiding this comment.
To continue to leverage generators, maybe $forwarded could always be a generator?
| $forwarded = []; | |
| $messages = is_iterable($messages) ? $messages : [$messages]; | |
| $forwarded = (function () use ($messages): Generator { | |
| foreach ($messages as $message) { | |
| if (! $this->filterBefore->allows($message)) { | |
| continue; | |
| } | |
| $message = $this->translator->translateMessage($message); | |
| if (! $this->filterAfter->allows($message)) { | |
| continue; | |
| } | |
| yield $message; | |
| } | |
| })(); |
No description provided.