Pass the message to apply methods of projections - #922
Open
DavidBadura wants to merge 1 commit into
Open
DavidBadura wants to merge 1 commit into
DavidBadura wants to merge 1 commit into
Conversation
Decisions sometimes depend on metadata, e.g. when an event was recorded (retention periods, token expiry). Apply methods of a BasicProjection only got the state and the event, so there was no way to reach it. They can now take the Message as an optional third parameter. A third parameter with a different type is reported as ApplyMethodDetectionError.
|
Hello 👋 here is the most recent benchmark result:
This comment gets update everytime a new commit comes in! |
DanielBadura
reviewed
Oct 3, 2026
DanielBadura
left a comment
Member
There was a problem hiding this comment.
We should maybe also think about adding this to normal aggregate applies?
Comment on lines
+271
to
+306
| ```php | ||
| use Patchlevel\EventSourcing\Attribute\Apply; | ||
| use Patchlevel\EventSourcing\Identifier\Uuid; | ||
| use Patchlevel\EventSourcing\Message\Message; | ||
| use Patchlevel\EventSourcing\Projection\BasicProjection; | ||
| use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader; | ||
|
|
||
| final class LastCheckIn extends BasicProjection | ||
| { | ||
| public function __construct( | ||
| private readonly Uuid $hotelId, | ||
| ) { | ||
| } | ||
|
|
||
| public function initialState(): DateTimeImmutable|null | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| /** @return list<string> */ | ||
| protected function tagFilter(): array | ||
| { | ||
| return ["hotel:{$this->hotelId->toString()}"]; | ||
| } | ||
|
|
||
| #[Apply] | ||
| public function applyGuestIsCheckedIn( | ||
| DateTimeImmutable|null $state, | ||
| GuestIsCheckedIn $event, | ||
| Message $message, | ||
| ): DateTimeImmutable { | ||
| return $message->header(RecordedOnHeader::class)->recordedOn; | ||
| } | ||
| } | ||
| ``` | ||
| ::: |
Member
There was a problem hiding this comment.
not sure how this will look rendered tbh :D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Some decisions depend on metadata, like when an event was recorded. The dcb.events examples need this for the username retention period, the price grace period and the opt-in token expiry. Apply methods of a
BasicProjectiononly got the state and the event, so there was no way to get there.Apply methods can now take the
Messageas an optional third parameter:If the third parameter has another type, the projection throws an
ApplyMethodDetectionErrorinstead of failing later with a type error.