[8.x] Added ability to cancel notifications immediately prior to sending #37930
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR makes it easier to cancel notifications immediately prior to sending, without having to register custom event listeners or create job classes.
Use case
Queued or delayed notifications make it possible that data changes between the notification being queued and its job being processed. Sometimes the up-to-date data is no longer compatible with the kind of notification being sent, for example a "DispatchConfirmation" email on an order that has since been cancelled.
Currently the primary way to prevent notifications being dispatched with stale information is to register a custom event listener, listen for the
NotificationSending
event, and attempt to call user-defined methods on the notification class in order to determine if the notification should be sent or not. Alternatively, a Job can be queued which synchronously dispatches a notification after the current state is checked.This PR just simplifies this process by checking for the existance of a
shouldBeSent()
method on the notification and cancelling sending if the method returns anything buttrue
.Non-breaking changes
The
NotificationSending
event is still dispatched, so applications using the technique described above will not be affected.