-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
toby7002
committed
Sep 27, 2023
1 parent
d1a9e96
commit 5e7b870
Showing
3 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
$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 | ||
{ | ||
return $this->future; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |