Skip to content
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

Closed

Conversation

mateusjatenee
Copy link
Contributor

@mateusjatenee mateusjatenee commented Oct 4, 2023

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).

DB::transaction(function () {
    $cart->doSomeAction(); // adds an event
    $cart->finalize(); // adds another event
});

Event::dispatchMany($cart->flushEvents());

It also supports an array syntax to keep it similar to dispatch:

Event::dispatchMany([
    'foo' => [1, 'payload'],
    'bar' => [2', 'more payload']
]);

This PR provides simple syntatic sugar for that use case.

Copy link
Member

@GrahamCampbell GrahamCampbell left a 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.
Copy link
Member

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?

Copy link
Contributor Author

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]
]);

@mateusjatenee mateusjatenee changed the title [10.x] Allow multiple events to be dispatched at once [11.x] Allow multiple events to be dispatched at once Oct 4, 2023
@mateusjatenee mateusjatenee changed the base branch from 10.x to master October 4, 2023 14:39
@mateusjatenee
Copy link
Contributor Author

I don't like this, and in any case, we can't change the contract on 10.x.

my bad — I updated the target to master

@mabdullahsari
Copy link
Contributor

I have had similar needs in the past. 👍

@davorminchorov
Copy link

davorminchorov commented Oct 4, 2023

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

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.

Copy link
Contributor Author

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?

@mpyw
Copy link
Contributor

mpyw commented Oct 10, 2023

I don't like this. Probably you need $afterCommit feature.

https://laravel.com/docs/10.x/events#queued-event-listeners-and-database-transactions

@mateusjatenee
Copy link
Contributor Author

I don't like this. Probably you need $afterCommit feature.

You're not wrong — afterCommit is a possible way to solve this. There are 2 caveats IMO:

  • Some people do not like to publish events from within models, etc
  • When using afterCommit, the event is still published to the bus — so if you have a non-queued listener, or if the listener is not set to be processed after a transaction is committed, it will be a problem. Also, the event still gets published to the bus (so Event::assertNotDispatched(EventThatWouldNotHaveBeenDispatched::Class) would fail.

@taylorotwell
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants