1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00

Make DriverControl an interface

This commit is contained in:
Aaron Piotrowski 2020-04-16 11:08:31 -05:00
parent ab409bb254
commit 0eb8ef438e
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
2 changed files with 46 additions and 31 deletions

View File

@ -87,21 +87,37 @@ abstract class Driver
*/
public function createControl(): DriverControl
{
return new DriverControl(
function () use (&$running) {
$running = true;
while ($running) {
if ($this->isEmpty()) {
return;
}
$this->tick();
return new class(function () use (&$running) {
$running = true;
while ($running) {
if ($this->isEmpty()) {
return;
}
},
static function () use (&$running) {
$running = false;
$this->tick();
}
);
}, static function () use (&$running) {
$running = false;
}) implements DriverControl {
private $run;
private $stop;
public function __construct(callable $run, callable $stop)
{
$this->run = $run;
$this->stop = $stop;
}
public function run()
{
($this->run)();
}
public function stop()
{
($this->stop)();
}
};
}
/**

View File

@ -2,24 +2,23 @@
namespace Amp\Loop;
final class DriverControl
interface DriverControl
{
private $run;
private $stop;
/**
* Run the driver event loop.
*
* @return void
*
* @see Driver::run()
*/
public function run();
public function __construct(callable $run, callable $stop)
{
$this->run = $run;
$this->stop = $stop;
}
public function run()
{
($this->run)();
}
public function stop()
{
($this->stop)();
}
/**
* Stop the driver event loop.
*
* @return void
*
* @see Driver::stop()
*/
public function stop();
}