PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
PromiseResolver.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\promise;
25
29final class PromiseResolver{
31 private PromiseSharedData $shared;
33 private Promise $promise;
34
35 public function __construct(){
36 $this->shared = new PromiseSharedData();
37 $this->promise = new Promise($this->shared);
38 }
39
43 public function resolve(mixed $value) : void{
44 if($this->shared->state !== null){
45 throw new \LogicException("Promise has already been resolved/rejected");
46 }
47 $this->shared->state = true;
48 $this->shared->result = $value;
49 foreach($this->shared->onSuccess as $c){
50 $c($value);
51 }
52 $this->shared->onSuccess = [];
53 $this->shared->onFailure = [];
54 }
55
56 public function reject() : void{
57 if($this->shared->state !== null){
58 throw new \LogicException("Promise has already been resolved/rejected");
59 }
60 $this->shared->state = false;
61 foreach($this->shared->onFailure as $c){
62 $c();
63 }
64 $this->shared->onSuccess = [];
65 $this->shared->onFailure = [];
66 }
67
71 public function getPromise() : Promise{
72 return $this->promise;
73 }
74}