ChunkRequestTask
extends AsyncTask
in package
Class used to run async tasks in other threads.
An AsyncTask does not have its own thread. It is queued into an AsyncPool and executed if there is an async worker with no AsyncTask running. Therefore, an AsyncTask SHOULD NOT execute for more than a few seconds. For tasks that run for a long time or infinitely, start another thread instead.
WARNING: Any non-Threaded objects WILL BE SERIALIZED when assigned to members of AsyncTasks or other Threaded object. If later accessed from said Threaded object, you will be operating on a COPY OF THE OBJECT, NOT THE ORIGINAL OBJECT. If you want to store non-serializable objects to access when the task completes, store them using .
WARNING: As of pthreads v3.1.6, arrays are converted to Volatile objects when assigned as members of Threaded objects. Keep this in mind when using arrays stored as members of your AsyncTask.
WARNING: Do not call PocketMine-MP API methods from other Threads!!
Table of Contents
- TLS_KEY_ERROR_HOOK = "errorHook"
- TLS_KEY_PROMISE = "promise"
- $progressUpdates : Threaded
- $worker : AsyncWorker|null
- $chunk : string
- $chunkX : int
- $chunkZ : int
- $compressor : Compressor
- $cancelRun : bool
- $crashed : bool
- $finished : bool
- $result : scalar|null
- $serialized : bool
- $submitted : bool
- $threadLocalStorage : ArrayObject|array<string|int, mixed>|null
- $tiles : string
- __construct() : mixed
- __destruct() : mixed
- cancelRun() : void
- getResult() : mixed
- hasCancelledRun() : bool
- hasResult() : bool
- isCrashed() : bool
- isFinished() : bool
- Returns whether this task has finished executing, whether successfully or not. This differs from isRunning() because it is not true prior to task execution.
- isSubmitted() : bool
- onCompletion() : void
- Actions to execute when completed (on main thread) Implement this if you want to handle the data in your AsyncTask after it has been processed
- onError() : void
- Called from the main thread when the async task experiences an error during onRun(). Use this for things like promise rejection.
- onProgressUpdate() : void
- Called from the main thread after {@link AsyncTask::publishProgress} is called.
- onRun() : void
- Actions to execute when run
- publishProgress() : void
- Call this method from {@link AsyncTask::onRun} (AsyncTask execution thread) to schedule a call to {@link AsyncTask::onProgressUpdate} from the main thread with the given progress parameter.
- run() : void
- setResult() : void
- setSubmitted() : void
- fetchLocal() : mixed
- Retrieves data stored in thread-local storage.
- reallyDestruct() : void
- Override this to do normal __destruct() cleanup from a child class.
- storeLocal() : void
- Saves mixed data in thread-local storage. Data stored using this storage is **only accessible from the thread it was stored on**. Data stored using this method will **not** be serialized.
Constants
TLS_KEY_ERROR_HOOK
private
mixed
TLS_KEY_ERROR_HOOK
= "errorHook"
TLS_KEY_PROMISE
private
mixed
TLS_KEY_PROMISE
= "promise"
Properties
$progressUpdates
public
Threaded
$progressUpdates
$worker
public
AsyncWorker|null
$worker
= null
$chunk
protected
string
$chunk
$chunkX
protected
int
$chunkX
$chunkZ
protected
int
$chunkZ
$compressor
protected
Compressor
$compressor
$cancelRun
private
bool
$cancelRun
= false
$crashed
private
bool
$crashed
= false
$finished
private
bool
$finished
= false
$result
private
scalar|null
$result
= null
$serialized
private
bool
$serialized
= false
$submitted
private
bool
$submitted
= false
$threadLocalStorage
private
static ArrayObject|array<string|int, mixed>|null
$threadLocalStorage
= null
object hash => mixed data
Tags
$tiles
private
string
$tiles
= ""
Methods
__construct()
public
__construct(int $chunkX, int $chunkZ, Chunk $chunk, CompressBatchPromise $promise, Compressor $compressor[, Closure|null $onError = null ]) : mixed
Parameters
- $chunkX : int
- $chunkZ : int
- $chunk : Chunk
- $promise : CompressBatchPromise
- $compressor : Compressor
- $onError : Closure|null = null
Tags
Return values
mixed —__destruct()
public
final __destruct() : mixed
Return values
mixed —cancelRun()
public
cancelRun() : void
Return values
void —getResult()
public
getResult() : mixed
Return values
mixed —hasCancelledRun()
public
hasCancelledRun() : bool
Return values
bool —hasResult()
public
hasResult() : bool
Return values
bool —isCrashed()
public
isCrashed() : bool
Return values
bool —isFinished()
Returns whether this task has finished executing, whether successfully or not. This differs from isRunning() because it is not true prior to task execution.
public
isFinished() : bool
Return values
bool —isSubmitted()
public
isSubmitted() : bool
Return values
bool —onCompletion()
Actions to execute when completed (on main thread) Implement this if you want to handle the data in your AsyncTask after it has been processed
public
onCompletion() : void
Return values
void —onError()
Called from the main thread when the async task experiences an error during onRun(). Use this for things like promise rejection.
public
onError() : void
Return values
void —onProgressUpdate()
Called from the main thread after {@link AsyncTask::publishProgress} is called.
public
onProgressUpdate(mixed $progress) : void
Parameters
- $progress : mixed
-
The parameter passed to . It is serialize()'ed and then unserialize()'ed, as if it has been cloned.
Return values
void —onRun()
Actions to execute when run
public
onRun() : void
Return values
void —publishProgress()
Call this method from {@link AsyncTask::onRun} (AsyncTask execution thread) to schedule a call to {@link AsyncTask::onProgressUpdate} from the main thread with the given progress parameter.
public
publishProgress(mixed $progress) : void
Parameters
- $progress : mixed
-
A value that can be safely serialize()'ed.
Return values
void —run()
public
run() : void
Return values
void —setResult()
public
setResult(mixed $result) : void
Parameters
- $result : mixed
Return values
void —setSubmitted()
public
setSubmitted() : void
Return values
void —fetchLocal()
Retrieves data stored in thread-local storage.
protected
fetchLocal(string $key) : mixed
If you used storeLocal(), you can use this on the same thread to fetch data stored. This should be used during onProgressUpdate() and onCompletion() to fetch thread-local data stored on the parent thread.
Parameters
- $key : string
Tags
Return values
mixed —reallyDestruct()
Override this to do normal __destruct() cleanup from a child class.
protected
reallyDestruct() : void
Return values
void —storeLocal()
Saves mixed data in thread-local storage. Data stored using this storage is **only accessible from the thread it was stored on**. Data stored using this method will **not** be serialized.
protected
storeLocal(string $key, mixed $complexData) : void
This can be used to store references to variables which you need later on on the same thread, but not others.
For example, plugin references could be stored in the constructor of the async task (which is called on the main thread) using this, and then fetched in onCompletion() (which is also called on the main thread), without them becoming serialized.
Scalar types can be stored directly in class properties instead of using this storage.
Objects stored in this storage can be retrieved using fetchLocal() on the same thread that this method was called from.
Parameters
- $key : string
- $complexData : mixed
-
the data to store