mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-20 07:39:42 +00:00
Added a generic Promise type
I anticipate increasing demand for promises, and since all the libraries I could find suck, we'll stick to our own impl for now.
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\world;
|
||||
|
||||
use function spl_object_id;
|
||||
|
||||
final class ChunkPopulationPromise{
|
||||
/**
|
||||
* @var \Closure[]
|
||||
* @phpstan-var array<int, \Closure() : void>
|
||||
*/
|
||||
private array $onSuccess = [];
|
||||
/**
|
||||
* @var \Closure[]
|
||||
* @phpstan-var array<int, \Closure() : void>
|
||||
*/
|
||||
private array $onFailure = [];
|
||||
|
||||
private ?bool $success = null;
|
||||
|
||||
/**
|
||||
* @phpstan-param \Closure() : void $onSuccess
|
||||
* @phpstan-param \Closure() : void $onFailure
|
||||
*/
|
||||
public function onCompletion(\Closure $onSuccess, \Closure $onFailure) : void{
|
||||
if($this->success !== null){
|
||||
$this->success ? $onSuccess() : $onFailure();
|
||||
}else{
|
||||
$this->onSuccess[spl_object_id($onSuccess)] = $onSuccess;
|
||||
$this->onFailure[spl_object_id($onFailure)] = $onFailure;
|
||||
}
|
||||
}
|
||||
|
||||
public function resolve() : void{
|
||||
$this->success = true;
|
||||
foreach($this->onSuccess as $callback){
|
||||
$callback();
|
||||
}
|
||||
$this->onSuccess = [];
|
||||
$this->onFailure = [];
|
||||
}
|
||||
|
||||
public function reject() : void{
|
||||
$this->success = false;
|
||||
foreach($this->onFailure as $callback){
|
||||
$callback();
|
||||
}
|
||||
$this->onSuccess = [];
|
||||
$this->onFailure = [];
|
||||
}
|
||||
|
||||
public function isCompleted() : bool{
|
||||
return $this->success !== null;
|
||||
}
|
||||
}
|
@@ -68,6 +68,7 @@ use pocketmine\Server;
|
||||
use pocketmine\timings\Timings;
|
||||
use pocketmine\utils\AssumptionFailedError;
|
||||
use pocketmine\utils\Limits;
|
||||
use pocketmine\utils\Promise;
|
||||
use pocketmine\utils\ReversePriorityQueue;
|
||||
use pocketmine\world\biome\Biome;
|
||||
use pocketmine\world\biome\BiomeRegistry;
|
||||
@@ -240,8 +241,8 @@ class World implements ChunkManager{
|
||||
/** @var int */
|
||||
private $maxConcurrentChunkPopulationTasks = 2;
|
||||
/**
|
||||
* @var ChunkPopulationPromise[] chunkHash => promise
|
||||
* @phpstan-var array<int, ChunkPopulationPromise>
|
||||
* @var Promise[] chunkHash => promise
|
||||
* @phpstan-var array<int, Promise<Chunk>>
|
||||
*/
|
||||
private array $chunkPopulationRequestMap = [];
|
||||
/**
|
||||
@@ -2110,7 +2111,7 @@ class World implements ChunkManager{
|
||||
}
|
||||
}
|
||||
unset($this->activeChunkPopulationTasks[$index]);
|
||||
$this->chunkPopulationRequestMap[$index]->resolve();
|
||||
$this->chunkPopulationRequestMap[$index]->resolve($chunk);
|
||||
unset($this->chunkPopulationRequestMap[$index]);
|
||||
|
||||
$this->drainPopulationRequestQueue();
|
||||
@@ -2698,10 +2699,13 @@ class World implements ChunkManager{
|
||||
}
|
||||
}
|
||||
|
||||
private function enqueuePopulationRequest(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : ChunkPopulationPromise{
|
||||
/**
|
||||
* @phpstan-return Promise<Chunk>
|
||||
*/
|
||||
private function enqueuePopulationRequest(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : Promise{
|
||||
$chunkHash = World::chunkHash($chunkX, $chunkZ);
|
||||
$this->chunkPopulationRequestQueue->enqueue($chunkHash);
|
||||
$promise = $this->chunkPopulationRequestMap[$chunkHash] = new ChunkPopulationPromise();
|
||||
$promise = $this->chunkPopulationRequestMap[$chunkHash] = new Promise();
|
||||
if($associatedChunkLoader === null){
|
||||
$temporaryLoader = new class implements ChunkLoader{};
|
||||
$this->registerChunkLoader($temporaryLoader, $chunkX, $chunkZ);
|
||||
@@ -2721,8 +2725,10 @@ class World implements ChunkManager{
|
||||
* A ChunkLoader can be associated with the generation request to ensure that the generation request is cancelled if
|
||||
* no loaders are attached to the target chunk. If no loader is provided, one will be assigned (and automatically
|
||||
* removed when the generation request completes).
|
||||
*
|
||||
* @phpstan-return Promise<Chunk>
|
||||
*/
|
||||
public function requestChunkPopulation(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : ChunkPopulationPromise{
|
||||
public function requestChunkPopulation(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : Promise{
|
||||
$chunkHash = World::chunkHash($chunkX, $chunkZ);
|
||||
$promise = $this->chunkPopulationRequestMap[$chunkHash] ?? null;
|
||||
if($promise !== null && isset($this->activeChunkPopulationTasks[$chunkHash])){
|
||||
@@ -2743,8 +2749,10 @@ class World implements ChunkManager{
|
||||
*
|
||||
* If the chunk is currently locked (for example due to another chunk using it for async generation), the request
|
||||
* will be queued and executed at the earliest opportunity.
|
||||
*
|
||||
* @phpstan-return Promise<Chunk>
|
||||
*/
|
||||
public function orderChunkPopulation(int $x, int $z, ?ChunkLoader $associatedChunkLoader) : ChunkPopulationPromise{
|
||||
public function orderChunkPopulation(int $x, int $z, ?ChunkLoader $associatedChunkLoader) : Promise{
|
||||
$index = World::chunkHash($x, $z);
|
||||
$promise = $this->chunkPopulationRequestMap[$index] ?? null;
|
||||
if($promise !== null && isset($this->activeChunkPopulationTasks[$index])){
|
||||
@@ -2766,7 +2774,7 @@ class World implements ChunkManager{
|
||||
|
||||
$this->activeChunkPopulationTasks[$index] = true;
|
||||
if($promise === null){
|
||||
$promise = new ChunkPopulationPromise();
|
||||
$promise = new Promise();
|
||||
$this->chunkPopulationRequestMap[$index] = $promise;
|
||||
}
|
||||
|
||||
@@ -2788,8 +2796,8 @@ class World implements ChunkManager{
|
||||
}
|
||||
|
||||
//chunk is already populated; return a pre-resolved promise that will directly fire callbacks assigned
|
||||
$result = new ChunkPopulationPromise();
|
||||
$result->resolve();
|
||||
$result = new Promise();
|
||||
$result->resolve($chunk);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user