-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace thebigcrafter\Hydrogen\future; | ||
use thebigcrafter\Hydrogen\trait\ForbidCloning; | ||
use thebigcrafter\Hydrogen\trait\ForbidSerialization; | ||
|
||
class DeferredFuture | ||
{ | ||
use ForbidCloning; | ||
use ForbidSerialization; | ||
|
||
private readonly FutureState $state; | ||
|
||
private readonly Future $future; | ||
|
||
public function __construct() | ||
{ | ||
$this->state = new FutureState(); | ||
$this->future = new Future($this->state); | ||
} | ||
|
||
/** | ||
* Completes the operation with a result value. | ||
* | ||
* @param T $value Result of the operation. | ||
*/ | ||
public function complete(mixed $value = null): void | ||
Check failure on line 29 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)
Check failure on line 29 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)
|
||
{ | ||
$this->state->complete($value); | ||
} | ||
|
||
/** | ||
* Marks the operation as failed. | ||
* | ||
* @param \Throwable $throwable Throwable to indicate the error. | ||
*/ | ||
public function error(\Throwable $throwable): void | ||
{ | ||
$this->state->error($throwable); | ||
} | ||
|
||
/** | ||
* @return bool True if the operation has completed. | ||
*/ | ||
public function isComplete(): bool | ||
{ | ||
return $this->state->isComplete(); | ||
} | ||
|
||
/** | ||
* @return Future<T> The future associated with this Deferred. | ||
*/ | ||
public function getFuture(): Future | ||
Check failure on line 55 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)
Check failure on line 55 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)
Check failure on line 55 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)
Check failure on line 55 in src/thebigcrafter/Hydrogen/future/DeferredFuture.php GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)
|
||
{ | ||
return $this->future; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace thebigcrafter\Hydrogen\utils; | ||
|
||
use pocketmine\utils\InternetException; | ||
use thebigcrafter\Hydrogen\EventLoop; | ||
use thebigcrafter\Hydrogen\future\DeferredFuture; | ||
|
||
class Internet | ||
{ | ||
public static function fetch(string $url) | ||
{ | ||
$deferred = new DeferredFuture(); | ||
|
||
EventLoop::defer(function () use($deferred, $url) { | ||
$res = \pocketmine\utils\Internet::getURL($url); | ||
|
||
if ($res instanceof InternetException) { | ||
throw $res; | ||
} | ||
|
||
$deferred->complete($res->getBody()); | ||
}); | ||
|
||
return $deferred->getFuture(); | ||
} | ||
} |