4 require_once __DIR__
."/../vendor/autoload.php";
7 use Amp\Loop
as AmpLoop
;
9 $client = new http\
Client("curl", "seekat");
11 "use_eventloop" => new class($client, AmpLoop
::get()) implements http\Client\Curl\User
{
16 private ?
string $timeout = null;
17 private \Closure
$runcb;
19 function __construct(private http\Client
$client, private AmpLoop\Driver
$driver) {
22 function init($run) : void
{
23 $this->runcb
= $run(...);
27 * @param resource $socket
29 function run($socket = null, int $action = null) : void
{
31 $remaining = ($this->runcb
)($this->client
, $socket, $action);
33 $remaining = ($this->runcb
)($this->client
);
40 function once() : void
{
41 if (!$this->driver
->getInfo()["running"]) {
45 function send() : never
{
47 throw new BadMethodCallException(__FUNCTION__
);
49 function wait(int $timeout_ms = null) : never
{
51 throw new BadMethodCallException(__FUNCTION__
);
53 function timer(int $timeout_ms = null) : void
{
55 $this->driver
->cancel($this->timeout
);
58 $this->timeout
= $this->driver
->delay($timeout_ms, function() {
62 function done() : void
{
64 $this->driver
->cancel($this->timeout
);
65 $this->timeout
= null;
70 * @param resource $socket
72 function socket($socket, int $poll) : void
{
73 foreach ((array) $this->driver
->getState((string) $socket) as $id) {
74 $this->driver
->disable($id);
80 $id = $this->driver
->onReadable($socket, function($id, $socket) {
81 $this->run($socket, self
::POLL_IN
);
83 $this->driver
->setState((string) $socket, $id);
86 $id = $this->driver
->onWritable($socket, function($id, $socket) {
87 $this->run($socket, self
::POLL_OUT
);
89 $this->driver
->setState((string) $socket, $id);
91 case self
::POLL_INOUT
:
93 $this->driver
->onReadable($socket, function($id, $socket) {
94 $this->run($socket, self
::POLL_IN
);
96 $this->driver
->onWritable($socket, function($id, $socket) {
97 $this->run($socket, self
::POLL_OUT
);
100 $this->driver
->setState((string) $socket, $id);
102 case self
::POLL_REMOVE
:
103 foreach ((array) $this->driver
->getState((string) $socket) as $id) {
104 $this->driver
->cancel($id);
106 $this->driver
->setState((string) $socket, null);
113 $future = API\Future\amp
();
115 $api = include "examples.inc";
117 AmpLoop
::run(function() use($api) {
118 list($m6w6, $seekat) = yield
[$api->users
->m6w6(), $api->repos
->m6w6
->seekat()];
119 printf("Hi, my name is %s!\n", $m6w6->login
);
120 printf("Have fun with %s; %s!\n", $seekat->name
, $seekat->description
);