Task: Remove currentTick parameter (#3498)

This parameter is not used for the vast majority of task use cases and just serves as extra useless boilerplate code, especially for closure-based tasks.
This use case can be replaced using Server->getTick() in the cases where it matters.
This commit is contained in:
Dylan T 2020-05-16 15:28:45 +01:00 committed by GitHub
parent 38e28f91e8
commit 67666db827
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 21 deletions

View File

@ -32,8 +32,8 @@ use pocketmine\utils\Utils;
* Example usage: * Example usage:
* *
* ``` * ```
* TaskScheduler->scheduleTask(new CancellableClosureTask(function(int $currentTick) : bool{ * TaskScheduler->scheduleTask(new CancellableClosureTask(function() : bool{
* echo "HI on $currentTick\n"; * echo "HI\n";
* $continue = false; * $continue = false;
* return $continue; //stop repeating * return $continue; //stop repeating
* }); * });
@ -47,20 +47,20 @@ class CancellableClosureTask extends Task{
/** /**
* @var \Closure * @var \Closure
* @phpstan-var \Closure(int $currentTick) : bool * @phpstan-var \Closure() : bool
*/ */
private $closure; private $closure;
/** /**
* CancellableClosureTask constructor. * CancellableClosureTask constructor.
* *
* The closure should follow the signature callback(int $currentTick) : bool. The return value will be used to * The closure should follow the signature callback() : bool. The return value will be used to
* decide whether to continue repeating. * decide whether to continue repeating.
* *
* @phpstan-param \Closure(int $currentTick) : bool $closure * @phpstan-param \Closure() : bool $closure
*/ */
public function __construct(\Closure $closure){ public function __construct(\Closure $closure){
Utils::validateCallableSignature(function(int $currentTick) : bool{ return false; }, $closure); Utils::validateCallableSignature(function() : bool{ return false; }, $closure);
$this->closure = $closure; $this->closure = $closure;
} }
@ -68,8 +68,8 @@ class CancellableClosureTask extends Task{
return Utils::getNiceClosureName($this->closure); return Utils::getNiceClosureName($this->closure);
} }
public function onRun(int $currentTick) : void{ public function onRun() : void{
if(!($this->closure)($currentTick)){ if(!($this->closure)()){
$this->getHandler()->cancel(); $this->getHandler()->cancel();
} }
} }

View File

@ -31,8 +31,8 @@ use pocketmine\utils\Utils;
* Example usage: * Example usage:
* *
* ``` * ```
* TaskScheduler->scheduleTask(new ClosureTask(function(int $currentTick) : void{ * TaskScheduler->scheduleTask(new ClosureTask(function() : void{
* echo "HI on $currentTick\n"; * echo "HI\n";
* }); * });
* ``` * ```
*/ */
@ -40,16 +40,16 @@ class ClosureTask extends Task{
/** /**
* @var \Closure * @var \Closure
* @phpstan-var \Closure(int) : void * @phpstan-var \Closure() : void
*/ */
private $closure; private $closure;
/** /**
* @param \Closure $closure Must accept only ONE parameter, $currentTick * @param \Closure $closure Must accept zero parameters
* @phpstan-param \Closure(int) : void $closure * @phpstan-param \Closure() : void $closure
*/ */
public function __construct(\Closure $closure){ public function __construct(\Closure $closure){
Utils::validateCallableSignature(function(int $currentTick) : void{}, $closure); Utils::validateCallableSignature(function() : void{}, $closure);
$this->closure = $closure; $this->closure = $closure;
} }
@ -57,7 +57,7 @@ class ClosureTask extends Task{
return Utils::getNiceClosureName($this->closure); return Utils::getNiceClosureName($this->closure);
} }
public function onRun(int $currentTick) : void{ public function onRun() : void{
($this->closure)($currentTick); ($this->closure)();
} }
} }

View File

@ -60,7 +60,7 @@ abstract class Task{
* *
* @return void * @return void
*/ */
abstract public function onRun(int $currentTick); abstract public function onRun();
/** /**
* Actions to execute if the Task is cancelled * Actions to execute if the Task is cancelled

View File

@ -116,10 +116,10 @@ class TaskHandler{
$this->task->setHandler(null); $this->task->setHandler(null);
} }
public function run(int $currentTick) : void{ public function run() : void{
$this->timings->startTiming(); $this->timings->startTiming();
try{ try{
$this->task->onRun($currentTick); $this->task->onRun();
}finally{ }finally{
$this->timings->stopTiming(); $this->timings->stopTiming();
} }

View File

@ -147,7 +147,7 @@ class TaskScheduler{
unset($this->tasks[$task->getTaskId()]); unset($this->tasks[$task->getTaskId()]);
continue; continue;
} }
$task->run($this->currentTick); $task->run();
if($task->isRepeating()){ if($task->isRepeating()){
$task->setNextRun($this->currentTick + $task->getPeriod()); $task->setNextRun($this->currentTick + $task->getPeriod());
$this->queue->insert($task, $this->currentTick + $task->getPeriod()); $this->queue->insert($task, $this->currentTick + $task->getPeriod());

View File

@ -34,7 +34,7 @@ class CheckTestCompletionTask extends Task{
$this->plugin = $plugin; $this->plugin = $plugin;
} }
public function onRun(int $currentTick){ public function onRun(){
$test = $this->plugin->getCurrentTest(); $test = $this->plugin->getCurrentTest();
if($test === null){ if($test === null){
if(!$this->plugin->startNextTest()){ if(!$this->plugin->startNextTest()){