mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-15 02:09:42 +00:00
TaskScheduler: use a Ds\Set to index tasks internally
this removes all remaining use-cases for taskID.
This commit is contained in:
parent
dca4bf424e
commit
d738504e24
@ -27,6 +27,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\scheduler;
|
namespace pocketmine\scheduler;
|
||||||
|
|
||||||
|
use Ds\Set;
|
||||||
use pocketmine\utils\ReversePriorityQueue;
|
use pocketmine\utils\ReversePriorityQueue;
|
||||||
|
|
||||||
class TaskScheduler{
|
class TaskScheduler{
|
||||||
@ -42,8 +43,11 @@ class TaskScheduler{
|
|||||||
*/
|
*/
|
||||||
protected $queue;
|
protected $queue;
|
||||||
|
|
||||||
/** @var TaskHandler[] */
|
/**
|
||||||
protected $tasks = [];
|
* @var Set|TaskHandler[]
|
||||||
|
* @phpstan-var Set<TaskHandler>
|
||||||
|
*/
|
||||||
|
protected $tasks;
|
||||||
|
|
||||||
/** @var int */
|
/** @var int */
|
||||||
private $ids = 1;
|
private $ids = 1;
|
||||||
@ -54,6 +58,7 @@ class TaskScheduler{
|
|||||||
public function __construct(?string $owner = null){
|
public function __construct(?string $owner = null){
|
||||||
$this->owner = $owner;
|
$this->owner = $owner;
|
||||||
$this->queue = new ReversePriorityQueue();
|
$this->queue = new ReversePriorityQueue();
|
||||||
|
$this->tasks = new Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scheduleTask(Task $task) : TaskHandler{
|
public function scheduleTask(Task $task) : TaskHandler{
|
||||||
@ -76,7 +81,7 @@ class TaskScheduler{
|
|||||||
foreach($this->tasks as $id => $task){
|
foreach($this->tasks as $id => $task){
|
||||||
$task->cancel();
|
$task->cancel();
|
||||||
}
|
}
|
||||||
$this->tasks = [];
|
$this->tasks->clear();
|
||||||
while(!$this->queue->isEmpty()){
|
while(!$this->queue->isEmpty()){
|
||||||
$this->queue->extract();
|
$this->queue->extract();
|
||||||
}
|
}
|
||||||
@ -84,7 +89,7 @@ class TaskScheduler{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function isQueued(TaskHandler $task) : bool{
|
public function isQueued(TaskHandler $task) : bool{
|
||||||
return isset($this->tasks[$task->getTaskId()]);
|
return $this->tasks->contains($task);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -116,7 +121,7 @@ class TaskScheduler{
|
|||||||
}
|
}
|
||||||
|
|
||||||
$handler->setNextRun($nextRun);
|
$handler->setNextRun($nextRun);
|
||||||
$this->tasks[$handler->getTaskId()] = $handler;
|
$this->tasks->add($handler);
|
||||||
$this->queue->insert($handler, $nextRun);
|
$this->queue->insert($handler, $nextRun);
|
||||||
|
|
||||||
return $handler;
|
return $handler;
|
||||||
@ -137,7 +142,7 @@ class TaskScheduler{
|
|||||||
/** @var TaskHandler $task */
|
/** @var TaskHandler $task */
|
||||||
$task = $this->queue->extract();
|
$task = $this->queue->extract();
|
||||||
if($task->isCancelled()){
|
if($task->isCancelled()){
|
||||||
unset($this->tasks[$task->getTaskId()]);
|
$this->tasks->remove($task);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$task->run();
|
$task->run();
|
||||||
@ -146,7 +151,7 @@ class TaskScheduler{
|
|||||||
$this->queue->insert($task, $this->currentTick + $task->getPeriod());
|
$this->queue->insert($task, $this->currentTick + $task->getPeriod());
|
||||||
}else{
|
}else{
|
||||||
$task->remove();
|
$task->remove();
|
||||||
unset($this->tasks[$task->getTaskId()]);
|
$this->tasks->remove($task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user