-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from WyriHaximus/renovate/react-promise-3.x
Update dependency react/promise to v3
- Loading branch information
Showing
4 changed files
with
82 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference
|
||
Loop::futureTick(static function () use ($deferred, $value): void { | ||
$deferred->resolve($value); | ||
|
@@ -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 GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference
|
||
Loop::addTimer($interval, static function () use ($deferred, $value): void { | ||
$deferred->resolve($value); | ||
|
@@ -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 GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference
|
||
Loop::addPeriodicTimer($interval, static function (TimerInterface $timer) use ($deferred, $check, $value): void { | ||
/** @psalm-suppress MixedAssignment */ | ||
|
@@ -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 GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference
|
||
$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 GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 with lowest dependency preference
|
||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
})); |