Skip to content

Commit

Permalink
react/promise 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Nov 13, 2023
1 parent d19afde commit 7659a70
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 102 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"nette/mail": "^3.1.10 || ^4.0.0",
"psr/log": "^1.1.4 || ^2.0.0 || ^3.0.0",
"react/event-loop": "^1.3.0",
"react/promise": "^2.9.0",
"react/promise": "~3.0.0",
"react/promise-timer": "^1.10.0",
"react/socket": "^1.12.0"
},
Expand Down
5 changes: 2 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon

parameters:
ignoreErrors:
- '#should return React\\Promise\\ExtendedPromiseInterface but returns React\\Promise\\PromiseInterface#'
- '#should be covariant with return type \(React\\Promise\\PromiseInterface#'
checkGenericClassInNonGenericObjectType: false

ignoreErrors:
# ignored strict rules
- '#Dynamic call to static method#'
- '#Call to method PHPUnit\\Framework\\Assert::assertTrue\(\) with true will always evaluate to true\.#'
22 changes: 11 additions & 11 deletions src/AsyncConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Psr\Log\LoggerInterface;
use React\Promise\Deferred;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\PromiseInterface;
use React\Promise\Timer\TimeoutException;
use Throwable;
use function React\Promise\resolve;
Expand All @@ -31,7 +31,7 @@ public function __construct(
$this->logger = $logger;
}

public function connect(): ExtendedPromiseInterface
public function connect(): PromiseInterface
{
if ($this->isConnected() && !$this->isDisconnecting()) {
$this->logger->debug('Connected');
Expand All @@ -50,14 +50,14 @@ public function connect(): ExtendedPromiseInterface
$waitUntilDisconnectEnds = $this->disconnectionPromise->promise();

} else {
$waitUntilDisconnectEnds = resolve();
$waitUntilDisconnectEnds = resolve(true);
}

$this->connectionPromise = new Deferred();

$doAfterFailedDisconnect = function (Throwable $e) {
$this->logger->debug('Disconnection failed. No need to reconnect now.');
$this->connectionPromise->resolve();
$this->connectionPromise->resolve(null);
$this->connectionPromise = null;

return resolve(new AsyncConnectionResult($this->writer, false));
Expand All @@ -70,7 +70,7 @@ public function connect(): ExtendedPromiseInterface
function (AsyncConnectionWriter $writer) {
$this->logger->debug('Connecting succeeded');
$this->writer = $writer;
$this->connectionPromise->resolve();
$this->connectionPromise->resolve(null);
$this->connectionPromise = null;

return resolve(new AsyncConnectionResult($writer, true));
Expand All @@ -90,7 +90,7 @@ function (Throwable $e): void {
}, $doAfterFailedDisconnect);
}

public function disconnect(): ExtendedPromiseInterface
public function disconnect(): PromiseInterface
{
if ($this->isDisconnecting()) {
$this->logger->debug('Already disconnecting');
Expand All @@ -109,7 +109,7 @@ public function disconnect(): ExtendedPromiseInterface
$waitUntilFinished = $this->connectionPromise->promise();

} else {
$waitUntilFinished = resolve();
$waitUntilFinished = resolve(true);
}

$this->disconnectionPromise = new Deferred();
Expand All @@ -120,24 +120,24 @@ public function disconnect(): ExtendedPromiseInterface
return $this->asyncConnector->disconnect($this->writer)
->then(function ($value) {
$this->logger->debug('Disconnection succeeded');
$this->disconnectionPromise->resolve();
$this->disconnectionPromise->resolve(null);
$this->disconnectionPromise = null;
$this->writer = null;

return resolve($value);
}, function (Throwable $e): void {
$this->logger->debug('Disconnection failed');
$this->disconnectionPromise->reject();
$this->disconnectionPromise->reject($e);
$this->disconnectionPromise = null;

throw $e;
});
}, function (Throwable $e) {
$this->logger->error('Connection failed. No need to disconnect now.');
$this->disconnectionPromise->resolve();
$this->disconnectionPromise->resolve(null);
$this->disconnectionPromise = null;

return resolve();
return resolve(true);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/AsyncConnectionWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace AsyncConnection;

use React\Promise\ExtendedPromiseInterface;
use React\Promise\PromiseInterface;

interface AsyncConnectionWriter
{

public function isValid(): bool;// checks whether server has not closed connection meanwhile

public function write(AsyncMessage $message): ExtendedPromiseInterface;
public function write(AsyncMessage $message): PromiseInterface;

}
23 changes: 11 additions & 12 deletions src/AsyncMessageQueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use AsyncConnection\Timer\PromiseTimer;
use Psr\Log\LoggerInterface;
use React\Promise\Deferred;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\PromiseInterface;
use Throwable;
use function array_slice;
Expand Down Expand Up @@ -69,10 +68,10 @@ public function __construct(
$this->maxIntervalBetweenMessages = $maxIntervalBetweenMessages ?? self::MAX_INTERVAL_BETWEEN_MESSAGES;
$this->minIntervalBetweenMessages = $minIntervalBetweenMessages ?? self::MIN_INTERVAL_BETWEEN_MESSAGES;
$this->maxMessagesPerConnection = $maxMessagesPerConnection ?? self::MAX_MESSAGES_PER_CONNECTION;
$this->minIntervalPromise = resolve();
$this->minIntervalPromise = resolve(true);
}

public function send(AsyncMessage $message): ExtendedPromiseInterface
public function send(AsyncMessage $message): PromiseInterface
{
static $requestsCounter = 0;
$previousRequestsCount = $requestsCounter;
Expand All @@ -83,7 +82,7 @@ public function send(AsyncMessage $message): ExtendedPromiseInterface
$this->messageQueue[$requestsCounter] = $message;

if (count($this->messageQueue) === 1) {
$previousRequestPromise = resolve();
$previousRequestPromise = resolve(null);

} else {
/** @var Deferred $previousRequest */
Expand All @@ -99,20 +98,20 @@ public function send(AsyncMessage $message): ExtendedPromiseInterface
function () use ($requestsCounter) {
$this->log('previous request finished', $requestsCounter);

return resolve();
return resolve(true);
},
function (Throwable $e) use ($requestsCounter) {
$this->log('previous request failed', $requestsCounter);

return resolve();
return resolve(true);
},
)->then(
function () use ($requestsCounter) {
if ($this->shouldReconnect()) {
return $this->reconnect($requestsCounter);
}

return resolve();
return resolve(true);
},
)->then(
function () use ($requestsCounter) {
Expand Down Expand Up @@ -182,13 +181,13 @@ private function finishWithError(Throwable $exception, int $requestsCounter): vo
{
unset($this->messageQueue[$requestsCounter]);
$this->processingRequests[$requestsCounter]->reject($exception);
$this->minIntervalPromise = resolve();
$this->minIntervalPromise = resolve(true);
}

private function finishWithSuccess(int $requestsCounter): void
{
unset($this->messageQueue[$requestsCounter]);
$this->processingRequests[$requestsCounter]->resolve();
$this->processingRequests[$requestsCounter]->resolve(null);

$this->minIntervalPromise = $this->promiseTimer->wait($this->minIntervalBetweenMessages);
}
Expand All @@ -200,15 +199,15 @@ private function shouldReconnect(): bool
|| $this->forceReconnect;
}

private function reconnect(int $requestsCounter): ExtendedPromiseInterface
private function reconnect(int $requestsCounter): PromiseInterface
{
$this->log('reconnecting started', $requestsCounter);
$this->forceReconnect = false;

return $this->asyncConnectionManager->disconnect()->then(function (): ExtendedPromiseInterface {
return $this->asyncConnectionManager->disconnect()->then(function (): PromiseInterface {
$this->resetSentMessagesCounter();

return resolve();
return resolve(true);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/AsyncMessageSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace AsyncConnection;

use React\Promise\ExtendedPromiseInterface;
use React\Promise\PromiseInterface;

interface AsyncMessageSender
{

public function sendMessage(
AsyncConnectionWriter $writer,
AsyncMessage $message
): ExtendedPromiseInterface;
): PromiseInterface;

}
4 changes: 2 additions & 2 deletions src/Connector/TcpConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace AsyncConnection\Connector;

use React\EventLoop\LoopInterface;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use React\Socket\Connection;
use React\Socket\ConnectorInterface;
use RuntimeException;
Expand Down Expand Up @@ -40,7 +40,7 @@ public function __construct(LoopInterface $loop, array $context = [])
*
* @param string $uri
*/
public function connect($uri): ExtendedPromiseInterface
public function connect($uri): PromiseInterface
{
$socket = @stream_socket_client(
$uri,
Expand Down
12 changes: 6 additions & 6 deletions src/Smtp/AsyncSmtpConnectionWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Nette\Mail\Message;
use Psr\Log\LoggerInterface;
use React\Promise\Deferred;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\PromiseInterface;
use React\Socket\ConnectionInterface;
use Throwable;
use function array_shift;
Expand Down Expand Up @@ -66,7 +66,7 @@ public function isValid(): bool
return $this->connection->isReadable() && $this->connection->isWritable();
}

public function write(AsyncMessage $message): ExtendedPromiseInterface
public function write(AsyncMessage $message): PromiseInterface
{
$this->logger->debug($message->getText());

Expand Down Expand Up @@ -110,7 +110,7 @@ public function write(AsyncMessage $message): ExtendedPromiseInterface
];

} else {
$deferred->resolve();
$deferred->resolve(null);
}

return $deferred->promise();
Expand All @@ -136,7 +136,7 @@ private function processDataResponse(string $data): void
$code = (int) $data;
if (in_array($code, $expectedCodes, true)) {
$this->logger->debug('code OK');
$deferred->resolve();
$deferred->resolve(null);

} else {
$this->logger->debug('code WRONG');
Expand All @@ -157,7 +157,7 @@ private function processDataResponse(string $data): void
$deferred->reject($exception);
}

$this->dataProcessingPromise->resolve();
$this->dataProcessingPromise->resolve(null);
$this->dataProcessingPromise = null;
}

Expand All @@ -172,7 +172,7 @@ private function processNonDataResponse(
return;
}

$dataProcessingPromise = $this->dataProcessingPromise !== null ? $this->dataProcessingPromise->promise() : resolve();
$dataProcessingPromise = $this->dataProcessingPromise !== null ? $this->dataProcessingPromise->promise() : resolve(null);
$dataProcessingPromise->then(function () use ($exceptionMessage, $previousException): void {
if (count($this->expectedResponses) <= 0) {
return;
Expand Down
6 changes: 3 additions & 3 deletions src/Smtp/AsyncSmtpMessageSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use AsyncConnection\AsyncMessageSender;
use InvalidArgumentException;
use Nette\Mail\Message;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\PromiseInterface;
use function array_keys;
use function array_merge;
use function key;
Expand All @@ -18,7 +18,7 @@
class AsyncSmtpMessageSender implements AsyncMessageSender
{

public function sendMessage(AsyncConnectionWriter $writer, AsyncMessage $message): ExtendedPromiseInterface
public function sendMessage(AsyncConnectionWriter $writer, AsyncMessage $message): PromiseInterface
{
if (!$message instanceof Message) {
throw new InvalidArgumentException('Only \Nette\Mail\Message is accepted');
Expand All @@ -36,7 +36,7 @@ public function sendMessage(AsyncConnectionWriter $writer, AsyncMessage $message
(array) $message->getHeader('Bcc'),
);

$previousPromise = resolve();
$previousPromise = resolve(null);
foreach (array_keys($recipients, null, true) as $email) {
$previousPromise = $previousPromise->then(static function () use ($email, $writer) {
$message = sprintf('RCPT TO:<%s>', $email);
Expand Down
6 changes: 3 additions & 3 deletions src/Timer/PromiseTimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\PromiseInterface;

class PromiseTimer
{
Expand All @@ -19,11 +19,11 @@ public function __construct(LoopInterface $loop)
/**
* @param int|float $seconds
*/
public function wait($seconds): ExtendedPromiseInterface
public function wait($seconds): PromiseInterface
{
$deferred = new Deferred();
$this->loop->addTimer($seconds, static function () use ($deferred): void {
$deferred->resolve();
$deferred->resolve(null);
});

return $deferred->promise();
Expand Down
Loading

0 comments on commit 7659a70

Please sign in to comment.