Skip to content

Commit e585eb8

Browse files
committed
Depend on react/promise-timer for more SOLID timer API
1 parent 09668c7 commit e585eb8

File tree

3 files changed

+14
-31
lines changed

3 files changed

+14
-31
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"php": ">=5.3",
1818
"react/socket-client": "^0.5 || ^0.4 || ^0.3",
1919
"react/event-loop": "^0.4 || ^0.3",
20-
"react/promise": "^2.0 || ^1.1"
20+
"react/promise": "^2.0 || ^1.1",
21+
"react/promise-timer": "^1.1"
2122
}
2223
}

src/ConnectionManagerDelay.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use React\SocketClient\ConnectorInterface;
66
use React\EventLoop\LoopInterface;
7-
use React\Promise\Deferred;
7+
use React\Promise\Timer;
88

99
class ConnectionManagerDelay implements ConnectorInterface
1010
{
@@ -21,15 +21,10 @@ public function __construct(ConnectorInterface $connectionManager, LoopInterface
2121

2222
public function create($host, $port)
2323
{
24-
$deferred = new Deferred();
25-
2624
$connectionManager = $this->connectionManager;
27-
$this->loop->addTimer($this->delay, function() use ($deferred, $connectionManager, $host, $port) {
28-
$connectionManager->create($host, $port)->then(
29-
array($deferred, 'resolve'),
30-
array($deferred, 'reject')
31-
);
25+
26+
return Timer\resolve($this->delay, $this->loop)->then(function () use ($connectionManager, $host, $port) {
27+
return $connectionManager->create($host, $port);
3228
});
33-
return $deferred->promise();
3429
}
3530
}

src/ConnectionManagerTimeout.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use React\SocketClient\ConnectorInterface;
66
use React\EventLoop\LoopInterface;
7-
use React\Promise\Deferred;
7+
use React\Promise\Timer;
88
use Exception;
99

1010
class ConnectionManagerTimeout implements ConnectorInterface
@@ -22,28 +22,15 @@ public function __construct(ConnectorInterface $connectionManager, LoopInterface
2222

2323
public function create($host, $port)
2424
{
25-
$deferred = new Deferred();
26-
$timedout = false;
25+
$promise = $this->connectionManager->create($host, $port);
2726

28-
$tid = $this->loop->addTimer($this->timeout, function() use ($deferred, &$timedout) {
29-
$deferred->reject(new Exception('Connection attempt timed out'));
30-
$timedout = true;
31-
// TODO: find a proper way to actually cancel the connection
32-
});
33-
34-
$loop = $this->loop;
35-
$this->connectionManager->create($host, $port)->then(function ($connection) use ($tid, $loop, &$timedout, $deferred) {
36-
if ($timedout) {
37-
// connection successfully established but timeout already expired => close successful connection
27+
return Timer\timeout($promise, $this->timeout, $this->loop)->then(null, function ($e) use ($promise) {
28+
// connection successfully established but timeout already expired => close successful connection
29+
$promise->then(function ($connection) {
3830
$connection->end();
39-
} else {
40-
$loop->cancelTimer($tid);
41-
$deferred->resolve($connection);
42-
}
43-
}, function ($error) use ($loop, $tid, $deferred) {
44-
$loop->cancelTimer($tid);
45-
$deferred->reject($error);
31+
});
32+
33+
throw $e;
4634
});
47-
return $deferred->promise();
4835
}
4936
}

0 commit comments

Comments
 (0)