22declare(strict_types=1);
24namespace pocketmine\network\mcpe\compression;
26use
function array_push;
33 private array $callbacks = [];
35 private ?
string $result =
null;
37 private bool $cancelled =
false;
42 public function onResolve(\Closure ...$callbacks) : void{
43 $this->checkCancelled();
44 if($this->result !==
null){
45 foreach($callbacks as $callback){
49 array_push($this->callbacks, ...$callbacks);
53 public function resolve(
string $result) : void{
54 if(!$this->cancelled){
55 if($this->result !==
null){
56 throw new \LogicException(
"Cannot resolve promise more than once");
58 $this->result = $result;
59 foreach($this->callbacks as $callback){
62 $this->callbacks = [];
71 return $this->callbacks;
74 public function getResult() : string{
75 $this->checkCancelled();
76 if($this->result ===
null){
77 throw new \LogicException(
"Promise has not yet been resolved");
82 public function hasResult() : bool{
83 return $this->result !== null;
86 public function isCancelled() : bool{
87 return $this->cancelled;
90 public function cancel() : void{
91 if($this->hasResult()){
92 throw new \LogicException(
"Cannot cancel a resolved promise");
94 $this->cancelled =
true;
97 private function checkCancelled() : void{
99 throw new \InvalidArgumentException(
"Promise has been cancelled");
onResolve(\Closure ... $callbacks)