mirror of
https://github.com/danog/loop.git
synced 2024-11-26 11:54:51 +01:00
Refactoring
This commit is contained in:
parent
6fc9888d6d
commit
e246b6b1db
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ vendor
|
||||
.php_cs.cache
|
||||
coverage
|
||||
.phpunit*
|
||||
*.cache
|
||||
|
21
.php-cs-fixer.dist.php
Normal file
21
.php-cs-fixer.dist.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
$config = new class extends Amp\CodeStyle\Config {
|
||||
public function getRules(): array
|
||||
{
|
||||
return array_merge(parent::getRules(), [
|
||||
'void_return' => true,
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
$config->getFinder()
|
||||
->in(__DIR__ . '/lib')
|
||||
->in(__DIR__ . '/test')
|
||||
->in(__DIR__ . '/examples');
|
||||
|
||||
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
|
||||
|
||||
$config->setCacheFile($cacheDir . '/.php_cs.cache');
|
||||
|
||||
return $config;
|
13
.php_cs.dist
13
.php_cs.dist
@ -1,13 +0,0 @@
|
||||
<?php
|
||||
|
||||
$config = new Amp\CodeStyle\Config();
|
||||
$config->getFinder()
|
||||
->in(__DIR__ . '/examples')
|
||||
->in(__DIR__ . '/lib')
|
||||
->in(__DIR__ . '/test');
|
||||
|
||||
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
|
||||
|
||||
$config->setCacheFile($cacheDir . '/.php_cs.cache');
|
||||
|
||||
return $config;
|
@ -17,15 +17,15 @@ for ($x = 0; $x < 10; $x++) {
|
||||
};
|
||||
$loop = new GenericLoop($callable, "Loop number $x");
|
||||
$loop->start();
|
||||
delay(100);
|
||||
delay(0.1);
|
||||
$loops []= $loop;
|
||||
}
|
||||
delay(5000);
|
||||
delay(5);
|
||||
echo "Resuming prematurely all loops!".PHP_EOL;
|
||||
foreach ($loops as $loop) {
|
||||
$loop->resume();
|
||||
}
|
||||
echo "OK done, waiting 5 more seconds!".PHP_EOL;
|
||||
delay(5000);
|
||||
delay(5);
|
||||
echo "Closing all loops!".PHP_EOL;
|
||||
delay(10);
|
||||
delay(0.01);
|
||||
|
@ -17,15 +17,15 @@ for ($x = 0; $x < 10; $x++) {
|
||||
};
|
||||
$loop = new PeriodicLoop($callable, "Loop number $x", 1000);
|
||||
$loop->start();
|
||||
delay(100);
|
||||
delay(0.1);
|
||||
$loops []= $loop;
|
||||
}
|
||||
delay(5000);
|
||||
delay(5);
|
||||
echo "Resuming prematurely all loops!".PHP_EOL;
|
||||
foreach ($loops as $loop) {
|
||||
$loop->resume();
|
||||
}
|
||||
echo "OK done, waiting 5 more seconds!".PHP_EOL;
|
||||
delay(5000);
|
||||
delay(5);
|
||||
echo "Closing all loops!".PHP_EOL;
|
||||
delay(10);
|
||||
delay(0.01);
|
||||
|
@ -77,13 +77,13 @@ class MyLoop extends Loop
|
||||
}
|
||||
|
||||
$function = function (int $number) {
|
||||
delay(1000);
|
||||
delay(1);
|
||||
return $number + 1;
|
||||
};
|
||||
$loops = [];
|
||||
for ($x = 0; $x < 10; $x++) {
|
||||
$loop = new MyLoop($function, "Loop number $x");
|
||||
$loop->start();
|
||||
delay(100);
|
||||
delay(0.1);
|
||||
$loops []= $loop;
|
||||
}
|
||||
|
@ -71,10 +71,10 @@ $loops = [];
|
||||
for ($x = 0; $x < 10; $x++) {
|
||||
$loop = new MyLoop("Loop number $x");
|
||||
$loop->start();
|
||||
delay(100);
|
||||
delay(0.1);
|
||||
$loops []= $loop;
|
||||
}
|
||||
delay(5000);
|
||||
delay(5);
|
||||
echo "Resuming prematurely all loops!".PHP_EOL;
|
||||
foreach ($loops as $loop) {
|
||||
$loop->resume();
|
||||
|
@ -54,16 +54,16 @@ $loops = [];
|
||||
for ($x = 0; $x < 10; $x++) {
|
||||
$loop = new ResSigLoop("Loop number $x");
|
||||
$loop->start();
|
||||
delay(100);
|
||||
delay(0.1);
|
||||
$loops []= $loop;
|
||||
}
|
||||
delay(5000);
|
||||
delay(5);
|
||||
echo "Resuming prematurely all loops!".PHP_EOL;
|
||||
foreach ($loops as $loop) {
|
||||
$loop->resume();
|
||||
}
|
||||
echo "OK done, waiting 5 more seconds!".PHP_EOL;
|
||||
delay(5000);
|
||||
delay(5);
|
||||
echo "Closing all loops!".PHP_EOL;
|
||||
foreach ($loops as $loop) {
|
||||
$loop->signal(true);
|
||||
|
@ -5,6 +5,7 @@ require 'vendor/autoload.php';
|
||||
use Amp\Loop;
|
||||
use danog\Loop\SignalLoop;
|
||||
|
||||
use function Amp\async;
|
||||
use function Amp\delay;
|
||||
|
||||
class SigLoop extends SignalLoop
|
||||
@ -31,7 +32,7 @@ class SigLoop extends SignalLoop
|
||||
{
|
||||
$number = 0;
|
||||
while (true) {
|
||||
if ($this->waitSignal(delay(1000))) {
|
||||
if ($this->waitSignal(async(delay(...), 1))) {
|
||||
echo "Got exit signal in $this!".PHP_EOL;
|
||||
return;
|
||||
}
|
||||
@ -53,10 +54,10 @@ $loops = [];
|
||||
for ($x = 0; $x < 10; $x++) {
|
||||
$loop = new SigLoop("Loop number $x");
|
||||
$loop->start();
|
||||
delay(100);
|
||||
delay(0.1);
|
||||
$loops []= $loop;
|
||||
}
|
||||
delay(5000);
|
||||
delay(5);
|
||||
echo "Closing all loops!".PHP_EOL;
|
||||
foreach ($loops as $loop) {
|
||||
$loop->signal(true);
|
||||
|
@ -51,9 +51,7 @@ class GenericLoop extends ResumableSignalLoop
|
||||
/**
|
||||
* Callable.
|
||||
*
|
||||
* @var callable
|
||||
*
|
||||
* @psalm-var callable():TCallableReturn
|
||||
* @var callable():TCallableReturn
|
||||
*/
|
||||
protected $callable;
|
||||
/**
|
||||
@ -91,7 +89,7 @@ class GenericLoop extends ResumableSignalLoop
|
||||
} elseif ($timeout > 0) {
|
||||
$this->reportPause($timeout);
|
||||
}
|
||||
if ($timeout === self::STOP || $this->waitSignal(async(fn () => $this->pause($timeout)))) {
|
||||
if ($timeout === self::STOP || $this->waitSignal(async($this->pause(...), $timeout))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -34,30 +34,17 @@ class PeriodicLoop extends ResumableSignalLoop
|
||||
* @psalm-var callable():(bool|Future<bool>)
|
||||
*/
|
||||
private $callback;
|
||||
/**
|
||||
* Loop name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
/**
|
||||
* Loop interval.
|
||||
*
|
||||
* @var ?int
|
||||
*/
|
||||
private $interval;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* If possible, the callable will be bound to the current instance of the loop.
|
||||
*
|
||||
* @param callable $callback Callback to call
|
||||
* @param callable():(bool|Future<bool>) $callback Callable to run
|
||||
* @param string $name Loop name
|
||||
* @param ?int $interval Loop interval
|
||||
*
|
||||
* @psalm-param callable():(bool|Future<bool>) $callback Callable to run
|
||||
*/
|
||||
public function __construct(callable $callback, string $name, ?int $interval)
|
||||
public function __construct(callable $callback, private string $name, private ?int $interval)
|
||||
{
|
||||
if ($callback instanceof \Closure) {
|
||||
try {
|
||||
@ -72,7 +59,6 @@ class PeriodicLoop extends ResumableSignalLoop
|
||||
}
|
||||
/**
|
||||
* Loop implementation.
|
||||
*
|
||||
*/
|
||||
public function loop(): void
|
||||
{
|
||||
@ -86,7 +72,7 @@ class PeriodicLoop extends ResumableSignalLoop
|
||||
break;
|
||||
}
|
||||
/** @var ?bool */
|
||||
$result = $this->waitSignal(async(fn () => $this->pause($this->interval)));
|
||||
$result = $this->waitSignal(async($this->pause(...), $this->interval));
|
||||
if ($result === true) {
|
||||
break;
|
||||
}
|
||||
|
@ -36,9 +36,9 @@ class ResumableTest extends Fixtures
|
||||
$this->assertTrue($loop->start());
|
||||
$this->assertAfterStart($loop);
|
||||
|
||||
delay(10);
|
||||
delay(0.010);
|
||||
$this->assertTrue(self::isResolved($paused));
|
||||
delay(100);
|
||||
delay(0.1);
|
||||
|
||||
$this->assertFinal($loop);
|
||||
}
|
||||
@ -59,7 +59,7 @@ class ResumableTest extends Fixtures
|
||||
$this->assertPreStart($loop);
|
||||
$this->assertTrue($loop->start());
|
||||
|
||||
delay(1);
|
||||
delay(0.001);
|
||||
$this->assertFalse(self::isResolved($paused)); // Did not pause
|
||||
|
||||
// Invert the order as the afterTest assertions will begin the test anew
|
||||
@ -81,7 +81,7 @@ class ResumableTest extends Fixtures
|
||||
{
|
||||
$paused = $deferred ? $loop->resumeDefer() : $loop->resume(); // Will resolve on next pause
|
||||
if ($deferred) {
|
||||
delay(1); // Avoid resuming after starting
|
||||
delay(0.001); // Avoid resuming after starting
|
||||
}
|
||||
$loop->setInterval($interval);
|
||||
|
||||
@ -89,17 +89,17 @@ class ResumableTest extends Fixtures
|
||||
$this->assertTrue($loop->start());
|
||||
$this->assertAfterStart($loop);
|
||||
|
||||
delay(1);
|
||||
delay(0.001);
|
||||
$this->assertTrue(self::isResolved($paused)); // Did pause
|
||||
|
||||
$paused = $deferred ? $loop->resumeDefer() : $loop->resume();
|
||||
if ($deferred) {
|
||||
$this->assertAfterStart($loop);
|
||||
delay(1);
|
||||
delay(0.001);
|
||||
}
|
||||
$this->assertFinal($loop);
|
||||
|
||||
delay(1);
|
||||
delay(0.001);
|
||||
$this->assertFalse(self::isResolved($paused)); // Did not pause again
|
||||
}
|
||||
|
||||
@ -116,24 +116,24 @@ class ResumableTest extends Fixtures
|
||||
{
|
||||
$paused1 = $loop->resumeDeferOnce(); // Will resolve on next pause
|
||||
$paused2 = $loop->resumeDeferOnce(); // Will resolve on next pause
|
||||
delay(1); // Avoid resuming after starting
|
||||
delay(0.001); // Avoid resuming after starting
|
||||
$loop->setInterval(10000);
|
||||
|
||||
$this->assertPreStart($loop);
|
||||
$this->assertTrue($loop->start());
|
||||
$this->assertAfterStart($loop);
|
||||
|
||||
delay(1);
|
||||
delay(0.001);
|
||||
$this->assertTrue(self::isResolved($paused1)); // Did pause
|
||||
$this->assertTrue(self::isResolved($paused2)); // Did pause
|
||||
|
||||
$paused1 = $loop->resumeDeferOnce();
|
||||
$paused2 = $loop->resumeDeferOnce();
|
||||
$this->assertAfterStart($loop);
|
||||
delay(1);
|
||||
delay(0.001);
|
||||
$this->assertFinal($loop);
|
||||
|
||||
delay(1);
|
||||
delay(0.001);
|
||||
$this->assertFalse(self::isResolved($paused1)); // Did not pause again
|
||||
$this->assertFalse(self::isResolved($paused2)); // Did not pause again
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ trait Basic
|
||||
public function loop(): void
|
||||
{
|
||||
$this->inited = true;
|
||||
delay(100);
|
||||
delay(0.1);
|
||||
$this->ran = true;
|
||||
}
|
||||
/**
|
||||
|
@ -49,7 +49,7 @@ trait Signal
|
||||
*/
|
||||
private function testGenerator(int $interval): void
|
||||
{
|
||||
delay($interval);
|
||||
delay($interval/1000);
|
||||
}
|
||||
/**
|
||||
* Loop implementation.
|
||||
|
Loading…
Reference in New Issue
Block a user