Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdiddydave committed Dec 9, 2023
1 parent 021838e commit 9525725
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
24 changes: 24 additions & 0 deletions examples/Counter/src/Events/DecrementCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Thunk\Verbs\Examples\Counter\Events;

use Thunk\Verbs\Event;
use Thunk\Verbs\Examples\Counter\Events\ResetCount;
use Thunk\Verbs\Examples\Counter\States\CountState;
use Thunk\Verbs\Attributes\Autodiscovery\AppliesToSingletonState;

#[AppliesToSingletonState(CountState::class)]
class DecrementCount extends Event
{
public function apply(CountState $state)
{
$state->count--;
}

public function handle()
{
if ($this->state(CountState::class)->count < 0) {
ResetCount::fire();
}
}
}
16 changes: 16 additions & 0 deletions examples/Counter/src/Events/ResetCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Thunk\Verbs\Examples\Counter\Events;

use Thunk\Verbs\Event;
use Thunk\Verbs\Examples\Counter\States\CountState;
use Thunk\Verbs\Attributes\Autodiscovery\AppliesToSingletonState;

#[AppliesToSingletonState(CountState::class)]
class ResetCount extends Event
{
public function apply(CountState $state)
{
$state->count = 0;
}
}
19 changes: 19 additions & 0 deletions examples/Counter/tests/FireOnReplayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Thunk\Verbs\Facades\Verbs;
use Thunk\Verbs\Examples\Counter\Events\DecrementCount;

it('does not fire nested events while replaying', function () {
$state = DecrementCount::fire()->state();

Verbs::commit();

// The DecrementCount handle() fires
// ResetCount since count < 0
expect($state->count)->toBe(0);

Verbs::replay();

// This time only DecrementCount fires
expect($state->fresh()->count)->toBe(-1);
});
2 changes: 1 addition & 1 deletion src/Lifecycle/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
) {
}

public function fire(Event $event): ?Event
public function fire(Event $event): Event|null
{
if ($this->is_replaying) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Support/PendingEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function hydrate(array $data): static
return $this;
}

public function fire(...$args): Event
public function fire(...$args): Event|null
{
if (! empty($args) || is_string($this->event)) {
$this->hydrate(static::normalizeArgs($args));
Expand Down

0 comments on commit 9525725

Please sign in to comment.