-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add early out in subscription engine
- Loading branch information
1 parent
13b647a
commit 87e3ce3
Showing
6 changed files
with
132 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Engine; | ||
|
||
use Countable; | ||
use IteratorAggregate; | ||
use Patchlevel\EventSourcing\Subscription\Subscription; | ||
use Traversable; | ||
|
||
use function array_filter; | ||
use function array_values; | ||
use function count; | ||
|
||
/** | ||
* @interal | ||
* @implements IteratorAggregate<Subscription> | ||
*/ | ||
final class SubscriptionCollection implements IteratorAggregate, Countable | ||
{ | ||
/** @param list<Subscription> $subscriptions */ | ||
public function __construct( | ||
private array $subscriptions = [], | ||
) { | ||
} | ||
|
||
/** @return Traversable<Subscription> */ | ||
public function getIterator(): Traversable | ||
{ | ||
yield from $this->subscriptions; | ||
} | ||
|
||
public function remove(Subscription $subscription): void | ||
{ | ||
$this->subscriptions = array_values( | ||
array_filter( | ||
$this->subscriptions, | ||
static fn (Subscription $s) => $s !== $subscription, | ||
), | ||
); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->subscriptions); | ||
} | ||
|
||
public function lowestPosition(): int | ||
{ | ||
$min = null; | ||
|
||
foreach ($this->subscriptions as $subscription) { | ||
if ($min !== null && $subscription->position() >= $min) { | ||
continue; | ||
} | ||
|
||
$min = $subscription->position(); | ||
} | ||
|
||
if ($min === null) { | ||
return 0; | ||
} | ||
|
||
return $min; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.