Skip to content

Commit 67f9e77

Browse files
committed
chore: improve documentation
1 parent b449628 commit 67f9e77

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

docs/using-projectors/making-sure-events-get-handled-in-the-right-order.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ class MyProjector extends Projector
3636
}
3737
```
3838

39+
Alternatively, you can determine the weight dynamically based on the event being processed. This allows you to prioritize certain events over others. The `$event` parameter will be `null` when the `resetState()` method is called (during projector reset operations).
40+
41+
```php
42+
namespace App\Projectors;
43+
44+
use App\Events\MoneyAddedEvent;
45+
use App\Events\MoneySubtractedEvent;
46+
use Spatie\EventSourcing\EventHandlers\Projectors\Projector;
47+
use Spatie\EventSourcing\StoredEvents\StoredEvent;
48+
49+
class MyProjector extends Projector
50+
{
51+
public function getWeight(?StoredEvent $event): int
52+
{
53+
return match ($event?->event_class) {
54+
MoneyAddedEvent::class => 2,
55+
MoneySubtractedEvent::class => -2,
56+
default => 0,
57+
};
58+
}
59+
60+
//
61+
}
62+
```
63+
3964
Note that providing a weight on a queued projector won't guarantee execution order.
4065

4166
## Want to know more?

docs/using-reactors/creating-and-configuring-reactors.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,29 @@ class MyReactor extends Reactor
202202
}
203203
```
204204

205+
Alternatively, you can determine the weight dynamically based on the event being processed. This allows you to prioritize certain events over others.
206+
207+
```php
208+
namespace App\Reactors;
209+
210+
use App\Events\MoneyAddedEvent;
211+
use App\Events\MoneySubtractedEvent;
212+
use Spatie\EventSourcing\EventHandlers\Reactors\Reactor;
213+
use Spatie\EventSourcing\StoredEvents\StoredEvent;
214+
215+
class MyReactor extends Reactor
216+
{
217+
public function getWeight(?StoredEvent $event): int
218+
{
219+
return match ($event?->event_class) {
220+
MoneyAddedEvent::class => 2,
221+
MoneySubtractedEvent::class => -2,
222+
default => 0,
223+
};
224+
}
225+
}
226+
```
227+
205228
Note that providing a weight on a queued reactor won't guarantee execution order.
206229

207230
## Want to know more?

0 commit comments

Comments
 (0)