Skip to content

Commit b89a459

Browse files
committed
Simplify usage by supporting new default loop
1 parent 05b1e69 commit b89a459

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,20 @@ $connectorRepeater->connect('www.google.com:80')->then(function ($stream) {
9292

9393
### Timeout
9494

95-
The `ConnectionManagerTimeout($connector, $timeout, $loop)` sets a maximum `$timeout` in seconds on when to give up
95+
The `ConnectionManagerTimeout($connector, $timeout)` sets a maximum `$timeout` in seconds on when to give up
9696
waiting for the connection to complete.
9797

9898
```php
99-
$connector = new ConnectionManagerTimeout($connector, 3.0, $loop);
99+
$connector = new ConnectionManagerTimeout($connector, 3.0);
100100
```
101101

102102
### Delay
103103

104-
The `ConnectionManagerDelay($connector, $delay, $loop)` sets a fixed initial `$delay` in seconds before actually
104+
The `ConnectionManagerDelay($connector, $delay)` sets a fixed initial `$delay` in seconds before actually
105105
trying to connect. (Not to be confused with [`ConnectionManagerTimeout`](#timeout) which sets a _maximum timeout_.)
106106

107107
```php
108-
$delayed = new ConnectionManagerDelayed($connector, 0.5, $loop);
108+
$delayed = new ConnectionManagerDelayed($connector, 0.5);
109109
```
110110

111111
### Reject
@@ -221,11 +221,11 @@ retrying unreliable hosts:
221221

222222
```php
223223
// delay connection by 2 seconds
224-
$delayed = new ConnectionManagerDelay($connector, 2.0, $loop);
224+
$delayed = new ConnectionManagerDelay($connector, 2.0);
225225

226226
// maximum of 3 tries, each taking no longer than 2.0 seconds
227227
$retry = new ConnectionManagerRepeat(
228-
new ConnectionManagerTimeout($connector, 2.0, $loop),
228+
new ConnectionManagerTimeout($connector, 2.0),
229229
3
230230
);
231231

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require": {
2020
"php": ">=5.3",
2121
"react/socket": "^1.0 || ^0.8 || ^0.7",
22-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
22+
"react/event-loop": "^1.2",
2323
"react/promise": "^2.1 || ^1.2.1",
2424
"react/promise-timer": "^1.1"
2525
},

src/ConnectionManagerDelay.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
namespace ConnectionManager\Extra;
44

5-
use React\Socket\ConnectorInterface;
5+
use React\EventLoop\Loop;
66
use React\EventLoop\LoopInterface;
77
use React\Promise\Timer;
8+
use React\Socket\ConnectorInterface;
89

910
class ConnectionManagerDelay implements ConnectorInterface
1011
{
1112
private $connectionManager;
1213
private $delay;
1314
private $loop;
1415

15-
public function __construct(ConnectorInterface $connectionManager, $delay, LoopInterface $loop)
16+
public function __construct(ConnectorInterface $connectionManager, $delay, LoopInterface $loop = null)
1617
{
1718
$this->connectionManager = $connectionManager;
1819
$this->delay = $delay;
19-
$this->loop = $loop;
20+
$this->loop = $loop ?: Loop::get();
2021
}
2122

2223
public function connect($uri)

tests/ConnectionManagerDelayTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ConnectionManager\Tests\Extra;
44

55
use ConnectionManager\Extra\ConnectionManagerDelay;
6+
use React\EventLoop\Loop;
67

78
class ConnectionManagerDelayTest extends TestCase
89
{
@@ -13,7 +14,19 @@ class ConnectionManagerDelayTest extends TestCase
1314
*/
1415
public function setUpLoop()
1516
{
16-
$this->loop = \React\EventLoop\Factory::create();
17+
$this->loop = Loop::get();
18+
}
19+
20+
public function testConstructWithoutLoopAssignsLoopAutomatically()
21+
{
22+
$unused = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
23+
$cm = new ConnectionManagerDelay($unused, 0);
24+
25+
$ref = new \ReflectionProperty($cm, 'loop');
26+
$ref->setAccessible(true);
27+
$loop = $ref->getValue($cm);
28+
29+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
1730
}
1831

1932
public function testDelayTenth()
@@ -24,7 +37,7 @@ public function testDelayTenth()
2437
$promise = $cm->connect('www.google.com:80');
2538
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
2639

27-
$this->loop->run();
40+
Loop::run();
2841
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
2942
}
3043

@@ -38,6 +51,6 @@ public function testCancellationOfPromiseBeforeDelayDoesNotStartConnection()
3851
$promise = $cm->connect('www.google.com:80');
3952
$promise->cancel();
4053

41-
$this->loop->run();
54+
Loop::run();
4255
}
4356
}

0 commit comments

Comments
 (0)