mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 17:59:48 +00:00
Added AsyncTask progress update API
This commit is contained in:
@ -34,6 +34,9 @@ abstract class AsyncTask extends Collectable{
|
||||
/** @var AsyncWorker $worker */
|
||||
public $worker = null;
|
||||
|
||||
/** @var \Threaded */
|
||||
public $progressUpdates = null;
|
||||
|
||||
private $result = null;
|
||||
private $serialized = false;
|
||||
private $cancelRun = false;
|
||||
@ -66,6 +69,7 @@ abstract class AsyncTask extends Collectable{
|
||||
}
|
||||
|
||||
public function run(){
|
||||
$this->progressUpdates = new \Threaded; // Do not move this to __construct for backwards compatibility.
|
||||
$this->result = null;
|
||||
|
||||
if($this->cancelRun !== true){
|
||||
@ -169,7 +173,36 @@ abstract class AsyncTask extends Collectable{
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this method from {@link #onCompletion} to fetch the data stored in the constructor, if any.
|
||||
* Call this method from {@link AsyncTask#onRun} (AsyncTask execution therad) to schedule a call to
|
||||
* {@link AsyncTask#onProgressUpdate} from the main thread with the given progress parameter.
|
||||
*
|
||||
* @param \Threaded|mixed $progress A Threaded object, or a value that can be safely serialize()'ed.
|
||||
*/
|
||||
public function publishProgress($progress){
|
||||
$this->progressUpdates[] = $progress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called from the main thread after {@link AsyncTask#publishProgress} is called.
|
||||
* All {@link AsyncTask#publishProgress} calls should result in {@link AsyncTask#onProgressUpdate} calls before
|
||||
* {@link AsyncTask#onCompletion} is called.
|
||||
*
|
||||
* @param Server $server
|
||||
* @param \Threaded|mixed $progress The parameter passed to {@link AsyncTask#publishProgress}. If it is not a
|
||||
* Threaded object, it would be serialize()'ed and later unserialize()'ed, as if it
|
||||
* has been cloned.
|
||||
*/
|
||||
public function onProgressUpdate(Server $server, $progress){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this method from {@link AsyncTask#onCompletion} to fetch the data stored in the constructor, if any, and
|
||||
* clears it from the storage.
|
||||
*
|
||||
* Do not call this method from {@link AsyncTask#onProgressUpdate}, because this method deletes the data and cannot
|
||||
* be used in the next {@link AsyncTask#onProgressUpdate} call or from {@link AsyncTask#onCompletion}. Use
|
||||
* {@link AsyncTask#peekLocal} instead.
|
||||
*
|
||||
* @param Server $server default null
|
||||
*
|
||||
@ -186,6 +219,28 @@ abstract class AsyncTask extends Collectable{
|
||||
return $server->getScheduler()->fetchLocalComplex($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this method from {@link AsyncTask#onProgressUpdate} to fetch the data stored in the constructor.
|
||||
*
|
||||
* Use {@link AsyncTask#peekLocal} instead from {@link AsyncTask#onCompletion}, because this method does not delete
|
||||
* the data, and not clearing the data will result in a warning for memory leak after {@link AsyncTask#onCompletion}
|
||||
* finished executing.
|
||||
*
|
||||
* @param Server|null $server default null
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \RuntimeException if no data were stored by this AsyncTask instance
|
||||
*/
|
||||
protected function peekLocal(Server $server = null){
|
||||
if($server === null){
|
||||
$server = Server::getInstance();
|
||||
assert($server !== null, "Call this method only from the main thread!");
|
||||
}
|
||||
|
||||
return $server->getScheduler()->peekLocalComplex($this);
|
||||
}
|
||||
|
||||
public function cleanObject(){
|
||||
foreach($this as $p => $v){
|
||||
if(!($v instanceof \Threaded)){
|
||||
|
Reference in New Issue
Block a user