-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConnectionManagerDelay.php
45 lines (36 loc) · 1.24 KB
/
ConnectionManagerDelay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace ConnectionManager\Extra;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Timer;
use React\Socket\ConnectorInterface;
class ConnectionManagerDelay implements ConnectorInterface
{
/** @var ConnectorInterface */
private $connectionManager;
/** @var float */
private $delay;
/** @var LoopInterface */
private $loop;
/**
* @param ConnectorInterface $connectionManager
* @param float $delay
* @param ?LoopInterface $loop
*/
public function __construct(ConnectorInterface $connectionManager, $delay, $loop = null)
{
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
}
$this->connectionManager = $connectionManager;
$this->delay = $delay;
$this->loop = $loop ?: Loop::get();
}
public function connect($uri)
{
$connectionManager = $this->connectionManager;
return Timer\resolve($this->delay, $this->loop)->then(function () use ($connectionManager, $uri) {
return $connectionManager->connect($uri);
});
}
}