Skip to content

Commit

Permalink
Merge pull request #80 from WyriHaximus/renovate/react-promise-3.x
Browse files Browse the repository at this point in the history
Update dependency react/promise to v3
  • Loading branch information
WyriHaximus authored Nov 3, 2023
2 parents f502a4e + 5368044 commit 4dbeb48
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 33 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^8.2",
"react/event-loop": "^1.4",
"react/promise": "^2.10"
"react/promise": "^2.10 || ^3.0"
},
"require-dev": {
"wyrihaximus/async-test-utilities": "^7.2.0"
Expand Down
21 changes: 11 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 41 additions & 22 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@
use React\EventLoop\Loop;
use React\EventLoop\TimerInterface;
use React\Promise\Deferred;
use React\Promise\Promise;
use React\Promise\PromiseInterface;

/**
* Promise that resolves once future tick is called.
* @param T $value Value to return on resolve.
*
* @param mixed $value Value to return on resolve.
* @return PromiseInterface<T>
*
* @template T
* Promise that resolves once future tick is called.
* @phpstan-ignore-next-line
*/
function futurePromise(mixed $value = null): PromiseInterface
{
/** @var Deferred<T> $deferred */
$deferred = new Deferred();

Check failure on line 24 in src/functions.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference

PHPDoc tag @var for variable $deferred contains generic type React\Promise\Deferred<mixed> but class React\Promise\Deferred is not generic.
Loop::futureTick(static function () use ($deferred, $value): void {
$deferred->resolve($value);
Expand All @@ -31,12 +33,16 @@ function futurePromise(mixed $value = null): PromiseInterface
* Promise that resolves after $interval has passed.
*
* @param float $interval The number of seconds to wait before execution.
* @param mixed $value Value to return on resolve.
* @param T $value Value to return on resolve.
*
* @return PromiseInterface<T>
*
* @template T
* @phpstan-ignore-next-line
*/
function timedPromise(float $interval, mixed $value = null): PromiseInterface
{
/** @var Deferred<T> $deferred */
$deferred = new Deferred();

Check failure on line 46 in src/functions.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference

PHPDoc tag @var for variable $deferred contains generic type React\Promise\Deferred<mixed> but class React\Promise\Deferred is not generic.
Loop::addTimer($interval, static function () use ($deferred, $value): void {
$deferred->resolve($value);
Expand All @@ -50,12 +56,16 @@ function timedPromise(float $interval, mixed $value = null): PromiseInterface
*
* @param float $interval The number of seconds between each interval to run $check.
* @param callable(mixed): (false|mixed) $check Callable to run at the specified $interval.
* @param mixed $value Value to pass into $check on tick.
* @param T $value Value to pass into $check on tick.
*
* @return PromiseInterface<T>
*
* @template T
* @phpstan-ignore-next-line
*/
function tickingPromise(float $interval, callable $check, mixed $value = null): PromiseInterface
{
/** @var Deferred<T> $deferred */
$deferred = new Deferred();

Check failure on line 69 in src/functions.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference

PHPDoc tag @var for variable $deferred contains generic type React\Promise\Deferred<mixed> but class React\Promise\Deferred is not generic.
Loop::addPeriodicTimer($interval, static function (TimerInterface $timer) use ($deferred, $check, $value): void {
/** @psalm-suppress MixedAssignment */
Expand All @@ -75,38 +85,47 @@ function tickingPromise(float $interval, callable $check, mixed $value = null):
* Promise that resolves once $check returns something other then false. Runs at future tick interval.
*
* @param callable $check Callable to run at tick.
* @param mixed $value Value to pass into $check on tick.
* @param T $value Value to pass into $check on tick.
* @param int $iterations Number of iterations to call $check in one tick.
*
* @return PromiseInterface<T>
*
* @template T
* @phpstan-ignore-next-line
*/
function tickingFuturePromise(callable $check, mixed $value = null, int $iterations = 1): PromiseInterface
{
return new Promise(static function (callable $resolve) use ($check, $iterations, $value): void {
$runCheck = static function () use ($check, &$runCheck, $resolve, $iterations, $value): void {
for ($i = 0; $i <= $iterations; $i++) {
$result = $check($value);
if ($result !== false) {
$runCheck = null;
$resolve($result);

return;
}
}
/** @var Deferred<T> $deferred */
$deferred = new Deferred();

Check failure on line 99 in src/functions.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference

PHPDoc tag @var for variable $deferred contains generic type React\Promise\Deferred<mixed> but class React\Promise\Deferred is not generic.
$runCheck = static function () use ($check, &$runCheck, $deferred, $iterations, $value): void {
for ($i = 0; $i <= $iterations; $i++) {
$result = $check($value);
if ($result !== false) {
$runCheck = null;
$deferred->resolve($result);

/** @psalm-suppress MixedArgument */
futurePromise()->then($runCheck);
};
return;
}
}

/** @psalm-suppress MixedArgument */
futurePromise()->then($runCheck);
});
};

futurePromise()->then($runCheck);

return $deferred->promise();
}

/**
* Sandwich a $function call within two futureTicks.
*
* @param mixed $value Value to pass into $function.
* @param T $value Value to pass into $function.
* @param callable $function Function to wrap.
*
* @return PromiseInterface<T>
*
* @template T
*/
function futureFunctionPromise(mixed $value, callable $function): PromiseInterface

Check failure on line 130 in src/functions.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference

PHPDoc tag @return contains generic type React\Promise\PromiseInterface<mixed> but interface React\Promise\PromiseInterface is not generic.
{
Expand Down
29 changes: 29 additions & 0 deletions tests/Types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use function PHPStan\Testing\assertType;
use function WyriHaximus\React\futureFunctionPromise;
use function WyriHaximus\React\futurePromise;
use function WyriHaximus\React\tickingFuturePromise;
use function WyriHaximus\React\tickingPromise;
use function WyriHaximus\React\timedPromise;

assertType('React\Promise\PromiseInterface<bool>', futurePromise(true));
assertType('React\Promise\PromiseInterface<null>', futurePromise());

assertType('React\Promise\PromiseInterface<bool>', timedPromise(1, true));
assertType('React\Promise\PromiseInterface<null>', timedPromise(1));

assertType('React\Promise\PromiseInterface<bool>', tickingPromise(1, static function (): void {
}, true));
assertType('React\Promise\PromiseInterface<null>', tickingPromise(1, static function (): void {
}));

assertType('React\Promise\PromiseInterface<bool>', tickingFuturePromise(static function (): void {
}, true));
assertType('React\Promise\PromiseInterface<null>', tickingFuturePromise(static function (): void {
}));

assertType('React\Promise\PromiseInterface<bool>', futureFunctionPromise(true, static function (): void {
}));

0 comments on commit 4dbeb48

Please sign in to comment.