diff --git a/src/Common/Promises.php b/src/Common/Promises.php index b896627..33cede1 100644 --- a/src/Common/Promises.php +++ b/src/Common/Promises.php @@ -4,13 +4,13 @@ use ArrayObject; use InvalidArgumentException; +use ipl\Scheduler\ObjectStorage; use Ramsey\Uuid\UuidInterface; use React\Promise\PromiseInterface; -use SplObjectStorage; trait Promises { - /** @var SplObjectStorage> */ + /** @var ObjectStorage> */ protected $promises; /** diff --git a/src/Common/Timers.php b/src/Common/Timers.php index 2d0641f..719202a 100644 --- a/src/Common/Timers.php +++ b/src/Common/Timers.php @@ -2,13 +2,13 @@ namespace ipl\Scheduler\Common; +use ipl\Scheduler\ObjectStorage; use Ramsey\Uuid\UuidInterface; use React\EventLoop\TimerInterface; -use SplObjectStorage; trait Timers { - /** @var SplObjectStorage */ + /** @var ObjectStorage */ protected $timers; /** diff --git a/src/ObjectStorage.php b/src/ObjectStorage.php new file mode 100644 index 0000000..9553afb --- /dev/null +++ b/src/ObjectStorage.php @@ -0,0 +1,29 @@ + + */ +class ObjectStorage extends SplObjectStorage +{ + public function getHash($object): string + { + if ($object instanceof Task) { + return $object->getUuid()->toString(); + } + + if ($object instanceof UuidInterface) { + return $object->toString(); + } + + return parent::getHash($object); + } +} diff --git a/src/Scheduler.php b/src/Scheduler.php index 25ad3a1..0d6618b 100644 --- a/src/Scheduler.php +++ b/src/Scheduler.php @@ -12,7 +12,6 @@ use React\EventLoop\Loop; use React\Promise; use React\Promise\ExtendedPromiseInterface; -use SplObjectStorage; use Throwable; class Scheduler @@ -124,15 +123,15 @@ class Scheduler */ public const ON_TASK_EXPIRED = 'task-expired'; - /** @var SplObjectStorage The scheduled tasks of this scheduler */ + /** @var ObjectStorage The scheduled tasks of this scheduler */ protected $tasks; public function __construct() { - $this->tasks = new SplObjectStorage(); + $this->tasks = new ObjectStorage(); - $this->promises = new SplObjectStorage(); - $this->timers = new SplObjectStorage(); + $this->promises = new ObjectStorage(); + $this->timers = new ObjectStorage(); $this->init(); } @@ -177,7 +176,7 @@ public function removeTasks(): self $this->cancelTask($task); } - $this->tasks = new SplObjectStorage(); + $this->tasks = new ObjectStorage(); return $this; }