-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[11.x] Allow multiple events to be dispatched at once #48631
[11.x] Allow multiple events to be dispatched at once #48631
Conversation
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.
I don't like this, and in any case, we can't change the contract on 10.x.
public function dispatchMany($events) | ||
{ | ||
foreach ($events as $event => $payload) { | ||
// If the key is an integer, we can assume we are dealing with an array and not a hashmap. |
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.
why do we care if there are string keys?
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 support something similar to the dispatch('key', 'payload')
syntax. e.g:
Event::dispatchMany([
'event' => [1, 2, 3]
]);
my bad — I updated the target to |
I have had similar needs in the past. 👍 |
Same, it allows for better control when events should be dispatched in complex workflows / use cases. |
* @param array<int|string, mixed> $events | ||
* @return void | ||
*/ | ||
public function dispatchMany($events) |
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.
What about halting and getting response? See dispatch() signature.
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.
Yeah — I didn't know if it made a lot of sense to get a response from dispatching multiple events. Maybe return an array if $halt = true
?
I don't like this. Probably you need https://laravel.com/docs/10.x/events#queued-event-listeners-and-database-transactions |
You're not wrong —
|
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions! If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response. |
A common use case I have for events is dispatching several at once. For example, I might have models store events to be published at a later time (e.g outside a transaction).
It also supports an array syntax to keep it similar to
dispatch
:This PR provides simple syntatic sugar for that use case.