mirror of
https://github.com/danog/loop.git
synced 2024-12-12 09:19:45 +01:00
74 lines
1.3 KiB
PHP
74 lines
1.3 KiB
PHP
|
<?php
|
||
|
|
||
|
require 'vendor/autoload.php';
|
||
|
|
||
|
use Amp\Loop as AmpLoop;
|
||
|
use danog\Loop\Loop;
|
||
|
|
||
|
use function Amp\delay;
|
||
|
|
||
|
class MyLoop extends Loop
|
||
|
{
|
||
|
/**
|
||
|
* Callable.
|
||
|
*
|
||
|
* @var callable
|
||
|
*/
|
||
|
private $callable;
|
||
|
/**
|
||
|
* Loop name.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
private $name;
|
||
|
/**
|
||
|
* Constructor.
|
||
|
*
|
||
|
* @param callable $callable Callable
|
||
|
* @param string $name Loop name
|
||
|
*/
|
||
|
public function __construct(callable $callable, string $name)
|
||
|
{
|
||
|
$this->callable = $callable;
|
||
|
$this->name = $name;
|
||
|
}
|
||
|
/**
|
||
|
* Main loop.
|
||
|
*
|
||
|
* @return \Generator
|
||
|
*/
|
||
|
public function loop(): \Generator
|
||
|
{
|
||
|
$callable = $this->callable;
|
||
|
|
||
|
$number = 0;
|
||
|
while (true) {
|
||
|
$number = yield from $callable($number);
|
||
|
echo "$this: $number".PHP_EOL;
|
||
|
}
|
||
|
}
|
||
|
/**
|
||
|
* Get loop name.
|
||
|
*
|
||
|
* @return string
|
||
|
*/
|
||
|
public function __toString(): string
|
||
|
{
|
||
|
return $this->name;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
AmpLoop::run(function () {
|
||
|
$function = function (int $number): \Generator {
|
||
|
yield delay(1000);
|
||
|
return $number + 1;
|
||
|
};
|
||
|
$loops = [];
|
||
|
for ($x = 0; $x < 10; $x++) {
|
||
|
$loop = new MyLoop($function, "Loop number $x");
|
||
|
$loop->start();
|
||
|
yield delay(100);
|
||
|
$loops []= $loop;
|
||
|
}
|
||
|
});
|