Skip to content

Commit 16859a7

Browse files
committed
Remove optional $loop constructor argument, always use default loop
1 parent e9de03f commit 16859a7

File tree

6 files changed

+177
-87
lines changed

6 files changed

+177
-87
lines changed

README.md

-6
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,6 @@ $connector = new React\Socket\Connector([
413413
$redis = new Clue\React\Redis\RedisClient('localhost', $connector);
414414
```
415415

416-
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
417-
pass the event loop instance to use for this object. You can use a `null` value
418-
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
419-
This value SHOULD NOT be given unless you're sure you want to explicitly use a
420-
given event loop instance.
421-
422416
#### __call()
423417

424418
The `__call(string $name, string[] $args): PromiseInterface<mixed>` method can be used to

src/Io/Factory.php

+5-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Clue\Redis\Protocol\Factory as ProtocolFactory;
66
use React\EventLoop\Loop;
7-
use React\EventLoop\LoopInterface;
87
use React\Promise\Deferred;
98
use React\Promise\Promise;
109
use React\Promise\PromiseInterface;
@@ -18,24 +17,19 @@
1817
*/
1918
class Factory
2019
{
21-
/** @var LoopInterface */
22-
private $loop;
23-
2420
/** @var ConnectorInterface */
2521
private $connector;
2622

2723
/** @var ProtocolFactory */
2824
private $protocol;
2925

3026
/**
31-
* @param ?LoopInterface $loop
3227
* @param ?ConnectorInterface $connector
3328
* @param ?ProtocolFactory $protocol
3429
*/
35-
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, ProtocolFactory $protocol = null)
30+
public function __construct(ConnectorInterface $connector = null, ProtocolFactory $protocol = null)
3631
{
37-
$this->loop = $loop ?: Loop::get();
38-
$this->connector = $connector ?: new Connector([], $this->loop);
32+
$this->connector = $connector ?: new Connector();
3933
$this->protocol = $protocol ?: new ProtocolFactory();
4034
}
4135

@@ -182,13 +176,13 @@ function (\Exception $e) use ($redis, $uri) {
182176
$timer = null;
183177
$promise = $promise->then(function (StreamingClient $v) use (&$timer, $resolve): void {
184178
if ($timer) {
185-
$this->loop->cancelTimer($timer);
179+
Loop::cancelTimer($timer);
186180
}
187181
$timer = false;
188182
$resolve($v);
189183
}, function (\Throwable $e) use (&$timer, $reject): void {
190184
if ($timer) {
191-
$this->loop->cancelTimer($timer);
185+
Loop::cancelTimer($timer);
192186
}
193187
$timer = false;
194188
$reject($e);
@@ -200,7 +194,7 @@ function (\Exception $e) use ($redis, $uri) {
200194
}
201195

202196
// start timeout timer which will cancel the pending promise
203-
$timer = $this->loop->addTimer($timeout, function () use ($timeout, &$promise, $reject, $uri): void {
197+
$timer = Loop::addTimer($timeout, function () use ($timeout, &$promise, $reject, $uri): void {
204198
$reject(new \RuntimeException(
205199
'Connection to ' . $uri . ' timed out after ' . $timeout . ' seconds (ETIMEDOUT)',
206200
\defined('SOCKET_ETIMEDOUT') ? \SOCKET_ETIMEDOUT : 110

src/RedisClient.php

+6-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Clue\React\Redis\Io\StreamingClient;
77
use Evenement\EventEmitter;
88
use React\EventLoop\Loop;
9-
use React\EventLoop\LoopInterface;
109
use React\Promise\PromiseInterface;
1110
use React\Socket\ConnectorInterface;
1211
use React\Stream\Util;
@@ -40,9 +39,6 @@ class RedisClient extends EventEmitter
4039
/** @var ?PromiseInterface<StreamingClient> */
4140
private $promise = null;
4241

43-
/** @var LoopInterface */
44-
private $loop;
45-
4642
/** @var float */
4743
private $idlePeriod = 0.001;
4844

@@ -58,12 +54,7 @@ class RedisClient extends EventEmitter
5854
/** @var array<string,bool> */
5955
private $psubscribed = [];
6056

61-
/**
62-
* @param string $url
63-
* @param ?ConnectorInterface $connector
64-
* @param ?LoopInterface $loop
65-
*/
66-
public function __construct($url, ConnectorInterface $connector = null, LoopInterface $loop = null)
57+
public function __construct(string $url, ConnectorInterface $connector = null)
6758
{
6859
$args = [];
6960
\parse_str((string) \parse_url($url, \PHP_URL_QUERY), $args);
@@ -72,8 +63,7 @@ public function __construct($url, ConnectorInterface $connector = null, LoopInte
7263
}
7364

7465
$this->target = $url;
75-
$this->loop = $loop ?: Loop::get();
76-
$this->factory = new Factory($this->loop, $connector);
66+
$this->factory = new Factory($connector);
7767
}
7868

7969
/**
@@ -102,7 +92,7 @@ private function client(): PromiseInterface
10292
$this->subscribed = $this->psubscribed = [];
10393

10494
if ($this->idleTimer !== null) {
105-
$this->loop->cancelTimer($this->idleTimer);
95+
Loop::cancelTimer($this->idleTimer);
10696
$this->idleTimer = null;
10797
}
10898
});
@@ -235,7 +225,7 @@ public function close(): void
235225
}
236226

237227
if ($this->idleTimer !== null) {
238-
$this->loop->cancelTimer($this->idleTimer);
228+
Loop::cancelTimer($this->idleTimer);
239229
$this->idleTimer = null;
240230
}
241231

@@ -248,7 +238,7 @@ private function awake(): void
248238
++$this->pending;
249239

250240
if ($this->idleTimer !== null) {
251-
$this->loop->cancelTimer($this->idleTimer);
241+
Loop::cancelTimer($this->idleTimer);
252242
$this->idleTimer = null;
253243
}
254244
}
@@ -258,7 +248,7 @@ private function idle(): void
258248
--$this->pending;
259249

260250
if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed && $this->promise !== null) {
261-
$this->idleTimer = $this->loop->addTimer($this->idlePeriod, function () {
251+
$this->idleTimer = Loop::addTimer($this->idlePeriod, function () {
262252
assert($this->promise instanceof PromiseInterface);
263253
$this->promise->then(function (StreamingClient $redis) {
264254
$redis->close();

tests/FunctionalTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
class FunctionalTest extends TestCase
1212
{
13-
1413
/** @var string */
1514
private $uri;
1615

0 commit comments

Comments
 (0)