Last active
February 10, 2020 10:33
-
-
Save JackNoordhuis/2be6bce14de17aee36c794af78299207 to your computer and use it in GitHub Desktop.
Script plugin for testing PocketMine async worker start closures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @name AsyncPoolStartClosures | |
* @main jacknoordhuis\test\AsyncPoolStartClosures | |
* @version 1.0.0 | |
* @api 4.0.0 | |
* @author Jack Noordhuis | |
*/ | |
declare(strict_types=1); | |
namespace jacknoordhuis\test { | |
use GlobalLogger; | |
use pocketmine\plugin\PluginBase; | |
use pocketmine\scheduler\AsyncTask; | |
class AsyncPoolStartClosures extends PluginBase { | |
/** @var \Closure[] */ | |
protected $closures = []; | |
protected function onEnable() : void { | |
$this->closures = [ | |
static function() : void{ | |
GlobalLogger::get()->info("async worker start closure"); | |
} | |
]; | |
// will log the message to any existing workers | |
$this->getServer()->getAsyncPool()->addWorkerStartHook($closure = function(int $worker) : void { | |
$task = new class($this->closures) extends AsyncTask { | |
/** @var string */ | |
private $closures; | |
/** | |
* @param \Closure[] $closures | |
*/ | |
public function __construct(array $closures) { | |
$this->closures = $closures; | |
} | |
public function onRun() : void { | |
/** @var \Closure[] $closures */ | |
$closures = $this->closures; | |
foreach($closures as $closure) { | |
($closure)(); | |
} | |
} | |
}; | |
$this->getServer()->getAsyncPool()->submitTaskToWorker($task, $worker); | |
}); | |
// the message will not be logged on any new workers | |
$this->getServer()->getAsyncPool()->removeWorkerStartHook($closure); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment