Skip to content

Commit

Permalink
Remove optional $loop constructor argument, always use default loop
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Feb 12, 2024
1 parent e9de03f commit 16859a7
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 87 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,6 @@ $connector = new React\Socket\Connector([
$redis = new Clue\React\Redis\RedisClient('localhost', $connector);
```

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

#### __call()

The `__call(string $name, string[] $args): PromiseInterface<mixed>` method can be used to
Expand Down
16 changes: 5 additions & 11 deletions src/Io/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Clue\Redis\Protocol\Factory as ProtocolFactory;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
Expand All @@ -18,24 +17,19 @@
*/
class Factory
{
/** @var LoopInterface */
private $loop;

/** @var ConnectorInterface */
private $connector;

/** @var ProtocolFactory */
private $protocol;

/**
* @param ?LoopInterface $loop
* @param ?ConnectorInterface $connector
* @param ?ProtocolFactory $protocol
*/
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, ProtocolFactory $protocol = null)
public function __construct(ConnectorInterface $connector = null, ProtocolFactory $protocol = null)
{
$this->loop = $loop ?: Loop::get();
$this->connector = $connector ?: new Connector([], $this->loop);
$this->connector = $connector ?: new Connector();
$this->protocol = $protocol ?: new ProtocolFactory();
}

Expand Down Expand Up @@ -182,13 +176,13 @@ function (\Exception $e) use ($redis, $uri) {
$timer = null;
$promise = $promise->then(function (StreamingClient $v) use (&$timer, $resolve): void {
if ($timer) {
$this->loop->cancelTimer($timer);
Loop::cancelTimer($timer);
}
$timer = false;
$resolve($v);
}, function (\Throwable $e) use (&$timer, $reject): void {
if ($timer) {
$this->loop->cancelTimer($timer);
Loop::cancelTimer($timer);
}
$timer = false;
$reject($e);
Expand All @@ -200,7 +194,7 @@ function (\Exception $e) use ($redis, $uri) {
}

// start timeout timer which will cancel the pending promise
$timer = $this->loop->addTimer($timeout, function () use ($timeout, &$promise, $reject, $uri): void {
$timer = Loop::addTimer($timeout, function () use ($timeout, &$promise, $reject, $uri): void {
$reject(new \RuntimeException(
'Connection to ' . $uri . ' timed out after ' . $timeout . ' seconds (ETIMEDOUT)',
\defined('SOCKET_ETIMEDOUT') ? \SOCKET_ETIMEDOUT : 110
Expand Down
22 changes: 6 additions & 16 deletions src/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Clue\React\Redis\Io\StreamingClient;
use Evenement\EventEmitter;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use React\Socket\ConnectorInterface;
use React\Stream\Util;
Expand Down Expand Up @@ -40,9 +39,6 @@ class RedisClient extends EventEmitter
/** @var ?PromiseInterface<StreamingClient> */
private $promise = null;

/** @var LoopInterface */
private $loop;

/** @var float */
private $idlePeriod = 0.001;

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

/**
* @param string $url
* @param ?ConnectorInterface $connector
* @param ?LoopInterface $loop
*/
public function __construct($url, ConnectorInterface $connector = null, LoopInterface $loop = null)
public function __construct(string $url, ConnectorInterface $connector = null)
{
$args = [];
\parse_str((string) \parse_url($url, \PHP_URL_QUERY), $args);
Expand All @@ -72,8 +63,7 @@ public function __construct($url, ConnectorInterface $connector = null, LoopInte
}

$this->target = $url;
$this->loop = $loop ?: Loop::get();
$this->factory = new Factory($this->loop, $connector);
$this->factory = new Factory($connector);
}

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

if ($this->idleTimer !== null) {
$this->loop->cancelTimer($this->idleTimer);
Loop::cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
});
Expand Down Expand Up @@ -235,7 +225,7 @@ public function close(): void
}

if ($this->idleTimer !== null) {
$this->loop->cancelTimer($this->idleTimer);
Loop::cancelTimer($this->idleTimer);
$this->idleTimer = null;
}

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

if ($this->idleTimer !== null) {
$this->loop->cancelTimer($this->idleTimer);
Loop::cancelTimer($this->idleTimer);
$this->idleTimer = null;
}
}
Expand All @@ -258,7 +248,7 @@ private function idle(): void
--$this->pending;

if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed && $this->promise !== null) {
$this->idleTimer = $this->loop->addTimer($this->idlePeriod, function () {
$this->idleTimer = Loop::addTimer($this->idlePeriod, function () {
assert($this->promise instanceof PromiseInterface);
$this->promise->then(function (StreamingClient $redis) {
$redis->close();
Expand Down
1 change: 0 additions & 1 deletion tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class FunctionalTest extends TestCase
{

/** @var string */
private $uri;

Expand Down
Loading

0 comments on commit 16859a7

Please sign in to comment.