mirror of
https://github.com/danog/loop.git
synced 2024-11-30 04:19:04 +01:00
Update docs
This commit is contained in:
parent
3f7cf21547
commit
bdeb4b05dc
@ -184,5 +184,3 @@ class PeriodicLoop extends GenericLoop
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
`PeriodicLoop` runs a callback at a periodic interval.
|
|
||||||
The loop can be stopped from the outside by using `stop()` and from the inside by returning `true`.
|
|
||||||
|
31
examples/GenericLoop.php
Normal file
31
examples/GenericLoop.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
|
use danog\Loop\GenericLoop;
|
||||||
|
|
||||||
|
use function Amp\delay;
|
||||||
|
|
||||||
|
/** @var GenericLoop[] */
|
||||||
|
$loops = [];
|
||||||
|
for ($x = 0; $x < 10; $x++) {
|
||||||
|
$callable = function (GenericLoop $loop): float {
|
||||||
|
static $number = 0;
|
||||||
|
echo "$loop: $number".PHP_EOL;
|
||||||
|
$number++;
|
||||||
|
return $number < 10 ? 1.0 : GenericLoop::STOP;
|
||||||
|
};
|
||||||
|
$loop = new GenericLoop($callable, "Loop number $x");
|
||||||
|
$loop->start();
|
||||||
|
delay(0.1);
|
||||||
|
$loops []= $loop;
|
||||||
|
}
|
||||||
|
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(5);
|
||||||
|
echo "Closing all loops!".PHP_EOL;
|
||||||
|
delay(0.01);
|
47
examples/Loop.php
Normal file
47
examples/Loop.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
|
use danog\Loop\GenericLoop;
|
||||||
|
use danog\Loop\Loop;
|
||||||
|
|
||||||
|
use function Amp\delay;
|
||||||
|
|
||||||
|
class MyLoop extends Loop
|
||||||
|
{
|
||||||
|
private int $number = 0;
|
||||||
|
|
||||||
|
public function __construct(private string $name)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function loop(): ?float
|
||||||
|
{
|
||||||
|
echo "$this: {$this->number}".PHP_EOL;
|
||||||
|
$this->number++;
|
||||||
|
return $this->number < 10 ? 1.0 : GenericLoop::STOP;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var MyLoop[] */
|
||||||
|
$loops = [];
|
||||||
|
for ($x = 0; $x < 10; $x++) {
|
||||||
|
$loop = new MyLoop("Loop number $x");
|
||||||
|
$loop->start();
|
||||||
|
delay(0.1);
|
||||||
|
$loops []= $loop;
|
||||||
|
}
|
||||||
|
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(5);
|
||||||
|
echo "Closing all loops!".PHP_EOL;
|
||||||
|
delay(0.01);
|
31
examples/PeriodicLoop.php
Normal file
31
examples/PeriodicLoop.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
|
use danog\Loop\PeriodicLoop;
|
||||||
|
|
||||||
|
use function Amp\delay;
|
||||||
|
|
||||||
|
/** @var PeriodicLoop[] */
|
||||||
|
$loops = [];
|
||||||
|
for ($x = 0; $x < 10; $x++) {
|
||||||
|
$callable = function (PeriodicLoop $loop): bool {
|
||||||
|
static $number = 0;
|
||||||
|
echo "$loop: $number".PHP_EOL;
|
||||||
|
$number++;
|
||||||
|
return $number == 10;
|
||||||
|
};
|
||||||
|
$loop = new PeriodicLoop($callable, "Loop number $x", 1.0);
|
||||||
|
$loop->start();
|
||||||
|
delay(0.1);
|
||||||
|
$loops []= $loop;
|
||||||
|
}
|
||||||
|
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(5);
|
||||||
|
echo "Closing all loops!".PHP_EOL;
|
||||||
|
delay(0.01);
|
59
lib/GenericLoop.php
Normal file
59
lib/GenericLoop.php
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic loop.
|
||||||
|
*
|
||||||
|
* @author Daniil Gentili <daniil@daniil.it>
|
||||||
|
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace danog\Loop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generic loop, runs single callable.
|
||||||
|
*
|
||||||
|
* @author Daniil Gentili <daniil@daniil.it>
|
||||||
|
*/
|
||||||
|
class GenericLoop extends Loop
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Callable.
|
||||||
|
*
|
||||||
|
* @var callable(static):?float
|
||||||
|
*/
|
||||||
|
private $callable;
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* The return value of the callable can be:
|
||||||
|
* * A number - the loop will be paused for the specified number of seconds
|
||||||
|
* * GenericLoop::STOP - The loop will stop
|
||||||
|
* * GenericLoop::PAUSE - The loop will pause forever (or until loop is `resumed()`
|
||||||
|
* from outside the loop)
|
||||||
|
* * GenericLoop::CONTINUE - Return this if you want to rerun the loop immediately
|
||||||
|
*
|
||||||
|
* If the callable does not return anything,
|
||||||
|
* the loop will behave is if GenericLoop::PAUSE was returned.
|
||||||
|
*
|
||||||
|
* The callable will be passed the instance of the current loop.
|
||||||
|
*
|
||||||
|
* The loop can be stopped from the outside by using stop().
|
||||||
|
*
|
||||||
|
* @param callable(static):?float $callable Callable to run
|
||||||
|
* @param string $name Loop name
|
||||||
|
*/
|
||||||
|
public function __construct(callable $callable, private string $name)
|
||||||
|
{
|
||||||
|
$this->callable = $callable;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function loop(): ?float
|
||||||
|
{
|
||||||
|
return ($this->callable)($this);
|
||||||
|
}
|
||||||
|
public function __toString(): string
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
}
|
44
lib/PeriodicLoop.php
Normal file
44
lib/PeriodicLoop.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
/**
|
||||||
|
* Periodic loop.
|
||||||
|
*
|
||||||
|
* @author Daniil Gentili <daniil@daniil.it>
|
||||||
|
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace danog\Loop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Periodic loop.
|
||||||
|
*
|
||||||
|
* @author Daniil Gentili <daniil@daniil.it>
|
||||||
|
*/
|
||||||
|
class PeriodicLoop extends GenericLoop
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* Runs a callback at a periodic interval.
|
||||||
|
*
|
||||||
|
* The callable will be passed the instance of the current loop.
|
||||||
|
*
|
||||||
|
* The loop can be stopped from the outside by calling stop()
|
||||||
|
* and from the inside by returning `true`.
|
||||||
|
*
|
||||||
|
* @param callable(static):bool $callback Callable to run
|
||||||
|
* @param string $name Loop name
|
||||||
|
* @param ?float $interval Loop interval; if null, pauses indefinitely or until `resume()` is called.
|
||||||
|
*/
|
||||||
|
public function __construct(callable $callback, string $name, ?float $interval)
|
||||||
|
{
|
||||||
|
/** @psalm-suppress ArgumentTypeCoercion */
|
||||||
|
parent::__construct(static function (self $loop) use ($callback, $interval): ?float {
|
||||||
|
/** @psalm-suppress ArgumentTypeCoercion */
|
||||||
|
if ($callback($loop) === true) {
|
||||||
|
return GenericLoop::STOP;
|
||||||
|
}
|
||||||
|
return $interval;
|
||||||
|
}, $name);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user