Skip to content

Commit

Permalink
Merge pull request #53 from WyriHaximus/migrate-to-loop-get
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus authored Jul 24, 2021
2 parents cbaf77e + 3a859fe commit 80aefbd
Show file tree
Hide file tree
Showing 11 changed files with 611 additions and 453 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ else
-v "`pwd`:`pwd`" \
-v "${COMPOSER_CACHE_DIR}:/home/app/.composer/cache" \
-w "`pwd`" \
"wyrihaximusnet/php:7.4-nts-alpine3.12-dev"
"wyrihaximusnet/php:7.4-nts-alpine-dev"
endif

all: ## Runs everything ###
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
],
"require": {
"php": "^8 || ^7.4",
"react/event-loop": "^1.0",
"react/event-loop": "^1.2",
"react/promise": "^2.8"
},
"require-dev": {
"wyrihaximus/async-test-utilities": "^3"
"wyrihaximus/async-test-utilities": "^3.4.24"
},
"config": {
"platform": {
Expand Down
925 changes: 560 additions & 365 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions examples/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

require \dirname(__DIR__) . '/vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

\WyriHaximus\React\futurePromise($loop)->then(function (): void {
\WyriHaximus\React\futurePromise()->then(function (): void {
echo DateTime::createFromFormat('U', \time())->format('r'), \PHP_EOL;
});

$loop->run();
16 changes: 6 additions & 10 deletions examples/combined.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@

require \dirname(__DIR__) . '/vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

\React\Promise\all([
\WyriHaximus\React\futurePromise($loop)->then(function () {
\WyriHaximus\React\futurePromise()->then(function () {
return \time();
}),
\WyriHaximus\React\tickingPromise($loop, 0.001, function () {
\WyriHaximus\React\tickingPromise(0.001, function () {
echo '.';

return \mt_rand(0, 1000) == 13;
}),
])->then(function ($time) use ($loop) {
return \WyriHaximus\React\nextPromise($loop, $time[0]);
})->then(function ($time) use ($loop) {
return \WyriHaximus\React\timedPromise($loop, 3, $time);
])->then(function ($time) {
return \WyriHaximus\React\nextPromise($time[0]);
})->then(function ($time) {
return \WyriHaximus\React\timedPromise(3, $time);
})->then(function ($time): void {
echo \PHP_EOL;
echo DateTime::createFromFormat('U', $time)->format('r'), \PHP_EOL;
echo DateTime::createFromFormat('U', \time())->format('r'), \PHP_EOL;
echo 'Done', \PHP_EOL;
});

$loop->run();
6 changes: 1 addition & 5 deletions examples/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

require \dirname(__DIR__) . '/vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

\WyriHaximus\React\futureFunctionPromise($loop, \json_encode([
\WyriHaximus\React\futureFunctionPromise(\json_encode([
'time' => \time(),
]), 'json_decode')->then(function ($json): void {
echo DateTime::createFromFormat('U', $json->time)->format('r'), \PHP_EOL;
});

$loop->run();
6 changes: 1 addition & 5 deletions examples/ticking.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

require \dirname(__DIR__) . '/vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

const WAIT_COUNT = 1337;
const WAIT_INTERVAL = 0.01;

$start = \time();
$count = 0;
echo 'Wait ' . WAIT_COUNT . ' * ' . WAIT_INTERVAL . ' seconds before resolving:', \PHP_EOL;
\WyriHaximus\React\tickingPromise($loop, WAIT_INTERVAL, function ($waitCount) use (&$count, $start) {
\WyriHaximus\React\tickingPromise(WAIT_INTERVAL, function ($waitCount) use (&$count, $start) {
echo '.';

if (++$count == $waitCount) {
Expand All @@ -21,5 +19,3 @@
}, WAIT_COUNT)->then(function ($start): void {
echo \PHP_EOL, 'That took ' . (\time() - $start) . ' seconds.', \PHP_EOL;
});

$loop->run();
6 changes: 1 addition & 5 deletions examples/ticking_future.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

require \dirname(__DIR__) . '/vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

const WAIT_COUNT = 307200;

$start = \time();
$count = 0;
echo 'Wait ' . WAIT_COUNT . ' ticks before resolving:', \PHP_EOL;
\WyriHaximus\React\tickingFuturePromise($loop, function ($waitCount) use (&$count, $start) {
\WyriHaximus\React\tickingFuturePromise(function ($waitCount) use (&$count, $start) {
echo '.';

if (++$count == $waitCount) {
Expand All @@ -22,5 +20,3 @@
}, function ($exception): void {
echo $exception->getMessage(), \PHP_EOL;
});

$loop->run();
6 changes: 1 addition & 5 deletions examples/timed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

require \dirname(__DIR__) . '/vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

echo 'Reporting back in 12 seconds', \PHP_EOL;
echo DateTime::createFromFormat('U', \time())->format('r'), \PHP_EOL;
\WyriHaximus\React\timedPromise($loop, 12)->then(function (): void {
\WyriHaximus\React\timedPromise(12)->then(function (): void {
echo DateTime::createFromFormat('U', \time())->format('r'), \PHP_EOL;
echo 'Done', \PHP_EOL;
});

$loop->run();
57 changes: 26 additions & 31 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WyriHaximus\React;

use React\EventLoop\LoopInterface;
use React\EventLoop\Loop;
use React\EventLoop\TimerInterface;
use React\Promise\Deferred;
use React\Promise\Promise;
Expand All @@ -13,13 +13,12 @@
/**
* Promise that resolves once future tick is called.
*
* @param LoopInterface $loop ReactPHP event loop.
* @param mixed $value Value to return on resolve.
* @param mixed $value Value to return on resolve.
*/
function futurePromise(LoopInterface $loop, $value = null): PromiseInterface
function futurePromise($value = null): PromiseInterface
{
$deferred = new Deferred();
$loop->futureTick(static function () use ($deferred, $value): void {
Loop::futureTick(static function () use ($deferred, $value): void {
$deferred->resolve($value);
});

Expand All @@ -29,14 +28,13 @@ function futurePromise(LoopInterface $loop, $value = null): PromiseInterface
/**
* Promise that resolves after $interval has passed.
*
* @param LoopInterface $loop ReactPHP event loop.
* @param float $interval The number of seconds to wait before execution.
* @param mixed $value Value to return on resolve.
* @param float $interval The number of seconds to wait before execution.
* @param mixed $value Value to return on resolve.
*/
function timedPromise(LoopInterface $loop, float $interval, $value = null): PromiseInterface
function timedPromise(float $interval, $value = null): PromiseInterface
{
$deferred = new Deferred();
$loop->addTimer($interval, static function () use ($deferred, $value): void {
Loop::addTimer($interval, static function () use ($deferred, $value): void {
$deferred->resolve($value);
});

Expand All @@ -46,21 +44,20 @@ function timedPromise(LoopInterface $loop, float $interval, $value = null): Prom
/**
* Promise that resolves once $check returns something other then false. Runs at periodic $interval.
*
* @param LoopInterface $loop ReactPHP event loop.
* @param float $interval The number of seconds between each interval to run $check.
* @param callable $check Callable to run at the specified $interval.
* @param mixed $value Value to pass into $check on tick.
* @param float $interval The number of seconds between each interval to run $check.
* @param callable $check Callable to run at the specified $interval.
* @param mixed $value Value to pass into $check on tick.
*/
function tickingPromise(LoopInterface $loop, float $interval, callable $check, $value = null): PromiseInterface
function tickingPromise(float $interval, callable $check, $value = null): PromiseInterface
{
$deferred = new Deferred();
$loop->addPeriodicTimer($interval, static function (TimerInterface $timer) use ($deferred, $check, $value, $loop): void {
Loop::addPeriodicTimer($interval, static function (TimerInterface $timer) use ($deferred, $check, $value): void {
$result = $check($value);
if ($result === false) {
return;
}

$loop->cancelTimer($timer);
Loop::cancelTimer($timer);
$deferred->resolve($result);
});

Expand All @@ -70,15 +67,14 @@ function tickingPromise(LoopInterface $loop, float $interval, callable $check, $
/**
* Promise that resolves once $check returns something other then false. Runs at future tick interval.
*
* @param LoopInterface $loop ReactPHP event loop.
* @param callable $check Callable to run at tick.
* @param mixed $value Value to pass into $check on tick.
* @param int $iterations Number of iterations to call $check in one tick.
* @param callable $check Callable to run at tick.
* @param mixed $value Value to pass into $check on tick.
* @param int $iterations Number of iterations to call $check in one tick.
*/
function tickingFuturePromise(LoopInterface $loop, callable $check, $value = null, int $iterations = 1): PromiseInterface
function tickingFuturePromise(callable $check, $value = null, int $iterations = 1): PromiseInterface
{
return new Promise(static function (callable $resolve) use ($loop, $check, $iterations, $value): void {
$runCheck = static function () use ($loop, $check, &$runCheck, $resolve, $iterations, $value): void {
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) {
Expand All @@ -88,22 +84,21 @@ function tickingFuturePromise(LoopInterface $loop, callable $check, $value = nul
}
}

futurePromise($loop)->then($runCheck);
futurePromise()->then($runCheck);
};

futurePromise($loop)->then($runCheck);
futurePromise()->then($runCheck);
});
}

/**
* Sandwich a $function call within two futureTicks.
*
* @param LoopInterface $loop ReactPHP event loop.
* @param mixed $value Value to pass into $function.
* @param callable $function Function to wrap.
* @param mixed $value Value to pass into $function.
* @param callable $function Function to wrap.
*/
function futureFunctionPromise(LoopInterface $loop, $value, callable $function): PromiseInterface
function futureFunctionPromise($value, callable $function): PromiseInterface
{
/** @psalm-suppress MissingClosureParamType */
return futurePromise($loop, $value)->then(static fn ($value): PromiseInterface => futurePromise($loop, $function($value)));
return futurePromise($value)->then(static fn ($value): PromiseInterface => futurePromise($function($value)));
}
30 changes: 11 additions & 19 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace WyriHaximus\Tests\React;

use React\EventLoop\Factory;
use React\EventLoop\Loop;
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;

use function gc_collect_cycles;
Expand All @@ -23,10 +23,8 @@ public function testFuturePromise(): void

$inputData = 'foo.bar';

$loop = Factory::create();

$promise = futurePromise($loop, $inputData);
$data = $this->await($promise, $loop);
$promise = futurePromise($inputData);
$data = $this->await($promise, Loop::get());
self::assertSame($inputData, $data);

unset($promise);
Expand All @@ -40,10 +38,8 @@ public function testTimedPromise(): void

$inputData = 'foo.bar';

$loop = Factory::create();

$promise = timedPromise($loop, 0.23, $inputData);
$data = $this->await($promise, $loop);
$promise = timedPromise(0.23, $inputData);
$data = $this->await($promise, Loop::get());
self::assertSame($inputData, $data);

unset($promise);
Expand All @@ -55,9 +51,8 @@ public function testTickingFuturePromise(): void
{
gc_collect_cycles();

$loop = Factory::create();
$promise = tickingFuturePromise($loop, static fn (): bool => true);
$loop->run();
$promise = tickingFuturePromise(static fn (): bool => true);
Loop::run();
gc_collect_cycles();
unset($promise);

Expand All @@ -68,8 +63,6 @@ public function testTickingPromise(): void
{
gc_collect_cycles();

$loop = Factory::create();

$inputData = 'foo.bar';
$fired = [
false,
Expand All @@ -88,8 +81,8 @@ public function testTickingPromise(): void
return 'foo.bar';
};

$promise = tickingPromise($loop, 1, $callback, $inputData);
$data = $this->await($promise, $loop);
$promise = tickingPromise(1, $callback, $inputData);
$data = $this->await($promise, Loop::get());
self::assertSame($inputData, $data);

foreach ($fired as $fire) {
Expand Down Expand Up @@ -148,10 +141,9 @@ public function testFutureFunctionPromise($inputData, $outputDate, $function): v
{
gc_collect_cycles();

$loop = Factory::create();
$promise = futureFunctionPromise($loop, $inputData, $function);
$promise = futureFunctionPromise($inputData, $function);

$data = $this->await($promise, $loop);
$data = $this->await($promise, Loop::get());
self::assertSame($outputDate, $data);

unset($promise);
Expand Down

0 comments on commit 80aefbd

Please sign in to comment.