Skip to content

Iterable message dispatching#237

Open
frankdejonge wants to merge 1 commit intoversion/4.xfrom
version/4.x-iterable-message-dispatching
Open

Iterable message dispatching#237
frankdejonge wants to merge 1 commit intoversion/4.xfrom
version/4.x-iterable-message-dispatching

Conversation

@frankdejonge
Copy link
Member

No description provided.

Copy link

@lgrossi lgrossi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this is also not covered by tests.

*/
public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRootVersion, iterable $events): void
{
$events = [...$events];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 28 to 30
foreach ($events as $event) {
$messages[] = $this->decorator->decorate(new Message($event, $headers));
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep leveraging generators, maybe this could pass messages as a generator instead?

Suggested change
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 = [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To continue to leverage generators, maybe $forwarded could always be a generator?

Suggested change
$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;
}
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants