Skip to content

Commit d7e653e

Browse files
authored
Merge pull request #83 from clue-labs/async
Use reactphp/async instead of clue/reactphp-block
2 parents 41c06ae + 7c91c49 commit d7e653e

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,14 @@ object to contain a list of entries.
375375

376376
As stated above, this library provides you a powerful, async API by default.
377377

378-
If, however, you want to integrate this into your traditional, blocking environment,
379-
you should look into also using [clue/reactphp-block](https://github.com/clue/reactphp-block).
380-
381-
The resulting blocking code could look something like this:
378+
You can also integrate this into your traditional, blocking environment by using
379+
[reactphp/async](https://github.com/reactphp/async). This allows you to simply
380+
await responses on the client like this:
382381

383382
```php
384-
use Clue\React\Block;
385-
use React\EventLoop\Loop;
383+
use function React\Async\await;
386384

387-
function getSipPeers()
385+
function getSipPeers(): array
388386
{
389387
$factory = new Clue\React\Ami\Factory();
390388

@@ -398,11 +396,13 @@ function getSipPeers()
398396
return $ret;
399397
});
400398

401-
return Block\await($promise, Loop::get(), 5.0);
399+
return await($promise);
402400
}
403401
```
404402

405-
Refer to [clue/reactphp-block](https://github.com/clue/reactphp-block#readme) for more details.
403+
This is made possible thanks to fibers available in PHP 8.1+ and our
404+
compatibility API that also works on all supported PHP versions.
405+
Please refer to [reactphp/async](https://github.com/reactphp/async#readme) for more details.
406406

407407
### Message
408408

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"react/socket": "^1.14"
1919
},
2020
"require-dev": {
21-
"clue/block-react": "^1.5",
22-
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36"
21+
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36",
22+
"react/async": "^4.2 || ^3.2 || ^2.2"
2323
},
2424
"autoload": {
2525
"psr-4": {

tests/FunctionalTest.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Clue\Tests\React\Ami;
44

5-
use Clue\React\Ami\Factory;
6-
use Clue\React\Ami\Client;
75
use Clue\React\Ami\ActionSender;
8-
use Clue\React\Block;
9-
use React\Promise\PromiseInterface;
6+
use Clue\React\Ami\Client;
7+
use Clue\React\Ami\Factory;
8+
use Clue\React\Ami\Protocol\Response;
9+
use React\EventLoop\Loop;
1010

1111
class FunctionalTest extends TestCase
1212
{
@@ -19,7 +19,6 @@ class FunctionalTest extends TestCase
1919
public static function setUpLoopBeforeClass()
2020
{
2121
self::$address = getenv('LOGIN');
22-
self::$loop = \React\EventLoop\Factory::create();
2322
}
2423

2524
/**
@@ -34,10 +33,16 @@ public function setUpSkipTest()
3433

3534
public function testConnection()
3635
{
37-
$factory = new Factory(self::$loop);
36+
$factory = new Factory();
37+
38+
$client = \React\Async\await($factory->createClient(self::$address));
39+
assert($client instanceof Client);
3840

39-
$client = $this->waitFor($factory->createClient(self::$address));
40-
/* @var $client Client */
41+
// let loop tick for reactphp/async v4 to clean up any remaining references
42+
// @link https://github.com/reactphp/async/pull/65 reported upstream // TODO remove me once merged
43+
if (function_exists('React\Async\async')) {
44+
\React\Async\delay(0.0);
45+
}
4146

4247
$this->assertFalse($client->isBusy());
4348

@@ -52,7 +57,7 @@ public function testPing(Client $client)
5257
{
5358
$sender = new ActionSender($client);
5459

55-
$pong = $this->waitFor($sender->ping());
60+
$pong = \React\Async\await($sender->ping());
5661

5762
$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $pong);
5863
}
@@ -64,7 +69,7 @@ public function testPing(Client $client)
6469
public function testInvalidCommandGetsRejected(Client $client)
6570
{
6671
$this->setExpectedException('Exception');
67-
$this->waitFor($client->request($client->createAction('Invalid')));
72+
\React\Async\await($client->request($client->createAction('Invalid')));
6873
}
6974

7075
/**
@@ -75,15 +80,20 @@ public function testActionSenderLogoffDisconnects(Client $client)
7580
{
7681
$sender = new ActionSender($client);
7782

78-
$ret = $this->waitFor($sender->logoff());
83+
$ret = \React\Async\await($sender->logoff());
84+
assert($ret instanceof Response);
7985

80-
$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $ret);
86+
// let loop tick for reactphp/async v4 to clean up any remaining references
87+
// @link https://github.com/reactphp/async/pull/65 reported upstream // TODO remove me once merged
88+
if (function_exists('React\Async\async')) {
89+
\React\Async\delay(0.0);
90+
}
8191

8292
$this->assertFalse($client->isBusy());
8393

8494
//$client->on('close', $this->expectCallableOnce());
8595

86-
self::$loop->run();
96+
Loop::run();
8797

8898
return $client;
8999
}
@@ -95,11 +105,6 @@ public function testActionSenderLogoffDisconnects(Client $client)
95105
public function testSendRejectedAfterClose(Client $client)
96106
{
97107
$this->setExpectedException('Exception');
98-
$this->waitFor($client->request($client->createAction('Ping')));
99-
}
100-
101-
private function waitFor(PromiseInterface $promise)
102-
{
103-
return Block\await($promise, self::$loop, 5.0);
108+
\React\Async\await($client->request($client->createAction('Ping')));
104109
}
105110
}

0 commit comments

Comments
 (0)