-
-
Notifications
You must be signed in to change notification settings - Fork 85
Iterable message dispatching #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: version/4.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,16 +4,20 @@ | |||||||||||||||||
|
|
||||||||||||||||||
| namespace EventSauce\EventSourcing; | ||||||||||||||||||
|
|
||||||||||||||||||
| use function is_iterable; | ||||||||||||||||||
|
|
||||||||||||||||||
| class DecoratingMessageDispatcher implements MessageDispatcher | ||||||||||||||||||
| { | ||||||||||||||||||
| public function __construct(private MessageDispatcher $dispatcher, private MessageDecorator $decorator) | ||||||||||||||||||
| { | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public function dispatch(Message ...$messages): void | ||||||||||||||||||
| public function dispatch(iterable|Message $messages): void | ||||||||||||||||||
| { | ||||||||||||||||||
| $messages = is_iterable($messages) ? $messages : [$messages]; | ||||||||||||||||||
|
|
||||||||||||||||||
| $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. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that this is also not covered by tests. |
||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,12 +77,17 @@ public function persist(object $aggregateRoot): void | |
| $this->persistEvents( | ||
| $aggregateRoot->aggregateRootId(), | ||
| $aggregateRoot->aggregateRootVersion(), | ||
| ...$aggregateRoot->releaseEvents() | ||
| $aggregateRoot->releaseEvents() | ||
| ); | ||
| } | ||
|
|
||
| public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRootVersion, object ...$events): void | ||
| /** | ||
| * @param iterable<object> $events | ||
| */ | ||
| public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRootVersion, iterable $events): void | ||
| { | ||
| $events = [...$events]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| if (count($events) === 0) { | ||
| return; | ||
| } | ||
|
|
@@ -102,7 +107,7 @@ public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRo | |
| )); | ||
| }, $events); | ||
|
|
||
| $this->messages->persist(...$messages); | ||
| $this->dispatcher->dispatch(...$messages); | ||
| $this->messages->persist($messages); | ||
| $this->dispatcher->dispatch($messages); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,19 +15,20 @@ public function __construct(MessageDispatcher $dispatcher, ?MessageDecorator $de | |||||||||||||||||
| $this->decorator = $decorator ?: new DefaultHeadersDecorator(); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public function dispatch(object ...$events): void | ||||||||||||||||||
| public function dispatch(iterable|object $events): void | ||||||||||||||||||
| { | ||||||||||||||||||
| $this->dispatchWithHeaders([], ...$events); | ||||||||||||||||||
| $this->dispatchWithHeaders([], $events); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public function dispatchWithHeaders(array $headers, object ...$events): void | ||||||||||||||||||
| public function dispatchWithHeaders(array $headers, iterable|object $events): void | ||||||||||||||||||
| { | ||||||||||||||||||
| $events = is_iterable($events) ? $events : [$events]; | ||||||||||||||||||
| $messages = []; | ||||||||||||||||||
|
|
||||||||||||||||||
| foreach ($events as $event) { | ||||||||||||||||||
| $messages[] = $this->decorator->decorate(new Message($event, $headers)); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
28
to
30
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||
|
|
||||||||||||||||||
| $this->dispatcher->dispatch(...$messages); | ||||||||||||||||||
| $this->dispatcher->dispatch($messages); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
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?