mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-18 20:14:31 +00:00
Merge branch 'release/3.4'
This commit is contained in:
@@ -23,6 +23,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\scheduler;
|
||||
|
||||
use pocketmine\utils\Utils;
|
||||
|
||||
/**
|
||||
* Manages general-purpose worker threads used for processing asynchronous tasks, and the tasks submitted to those
|
||||
* workers.
|
||||
@@ -84,6 +86,7 @@ class AsyncPool{
|
||||
* @param \Closure $hook
|
||||
*/
|
||||
public function addWorkerStartHook(\Closure $hook) : void{
|
||||
Utils::validateCallableSignature(function(int $worker) : void{}, $hook);
|
||||
$this->workerStartHooks[spl_object_hash($hook)] = $hook;
|
||||
foreach($this->workers as $i => $worker){
|
||||
$hook($i);
|
||||
|
@@ -45,6 +45,7 @@ class ClosureTask extends Task{
|
||||
* @param \Closure $closure Must accept only ONE parameter, $currentTick
|
||||
*/
|
||||
public function __construct(\Closure $closure){
|
||||
Utils::validateCallableSignature(function(int $currentTick) : void{}, $closure);
|
||||
$this->closure = $closure;
|
||||
}
|
||||
|
||||
|
@@ -27,6 +27,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
use DaveRandom\CallbackValidator\CallbackType;
|
||||
use pocketmine\ThreadManager;
|
||||
|
||||
/**
|
||||
@@ -550,4 +551,20 @@ class Utils{
|
||||
throw new \InvalidArgumentException("Class $className cannot be constructed");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the given callable is compatible with the desired signature. Throws a TypeError if they are
|
||||
* incompatible.
|
||||
*
|
||||
* @param callable $signature Dummy callable with the required parameters and return type
|
||||
* @param callable $subject Callable to check the signature of
|
||||
*
|
||||
* @throws \DaveRandom\CallbackValidator\InvalidCallbackException
|
||||
* @throws \TypeError
|
||||
*/
|
||||
public static function validateCallableSignature(callable $signature, callable $subject) : void{
|
||||
if(!($sigType = CallbackType::createFromCallable($signature))->isSatisfiedBy($subject)){
|
||||
throw new \TypeError("Declaration of callable `" . CallbackType::createFromCallable($subject) . "` must be compatible with `" . $sigType . "`");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user