Require Closures in more places instead of callable

Non-closure callables have strange context-dependent behaviour and are slower to call than a closure. It's possible to use Closure::fromCallable() in the origin scope, which guarantees that the callable will remain callable no matter where it's used.
This commit is contained in:
Dylan K. Taylor 2019-05-27 17:11:35 +01:00
parent 2720ff9607
commit 7eb8d8e366
3 changed files with 9 additions and 9 deletions

View File

@ -27,7 +27,7 @@ use pocketmine\utils\Utils;
use function array_push;
class CompressBatchPromise{
/** @var callable[] */
/** @var \Closure[] */
private $callbacks = [];
/** @var string|null */
@ -36,7 +36,7 @@ class CompressBatchPromise{
/** @var bool */
private $cancelled = false;
public function onResolve(callable ...$callbacks) : void{
public function onResolve(\Closure ...$callbacks) : void{
$this->checkCancelled();
foreach($callbacks as $callback){
Utils::validateCallableSignature(function(CompressBatchPromise $promise){}, $callback);
@ -64,7 +64,7 @@ class CompressBatchPromise{
}
/**
* @return callable[]
* @return \Closure[]
*/
public function getResolveCallbacks() : array{
return $this->callbacks;

View File

@ -184,16 +184,16 @@ class Internet{
* NOTE: This is a blocking operation and can take a significant amount of time. It is inadvisable to use this method on the main thread.
*
* @param string $page
* @param float|int $timeout The maximum connect timeout and timeout in seconds, correct to ms.
* @param float|int $timeout The maximum connect timeout and timeout in seconds, correct to ms.
* @param string[] $extraHeaders extra headers to send as a plain string array
* @param array $extraOpts extra CURLOPT_* to set as an [opt => value] map
* @param callable|null $onSuccess function to be called if there is no error. Accepts a resource argument as the cURL handle.
* @param array $extraOpts extra CURLOPT_* to set as an [opt => value] map
* @param \Closure|null $onSuccess function to be called if there is no error. Accepts a resource argument as the cURL handle.
*
* @return array a plain array of three [result body : string, headers : array[], HTTP response code : int]. Headers are grouped by requests with strtolower(header name) as keys and header value as values
*
* @throws InternetException if a cURL error occurs
*/
public static function simpleCurl(string $page, $timeout = 10, array $extraHeaders = [], array $extraOpts = [], ?callable $onSuccess = null) : array{
public static function simpleCurl(string $page, $timeout = 10, array $extraHeaders = [], array $extraOpts = [], ?\Closure $onSuccess = null) : array{
if(!self::$online){
throw new InternetException("Cannot execute web request while offline");
}

View File

@ -134,9 +134,9 @@ class BlockTransaction{
* The callable signature should be the same as the below dummy function.
* @see BlockTransaction::dummyValidator()
*
* @param callable $validator
* @param \Closure $validator
*/
public function addValidator(callable $validator) : void{
public function addValidator(\Closure $validator) : void{
Utils::validateCallableSignature([$this, 'dummyValidator'], $validator);
$this->validators[] = $validator;
}