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,8 +87,7 @@ abstract class Driver
*/ */
public function createControl(): DriverControl public function createControl(): DriverControl
{ {
return new DriverControl( return new class(function () use (&$running) {
function () use (&$running) {
$running = true; $running = true;
while ($running) { while ($running) {
if ($this->isEmpty()) { if ($this->isEmpty()) {
@ -97,11 +96,28 @@ abstract class Driver
$this->tick(); $this->tick();
} }
}, }, static function () use (&$running) {
static function () use (&$running) {
$running = false; $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; 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) /**
{ * Stop the driver event loop.
$this->run = $run; *
$this->stop = $stop; * @return void
} *
* @see Driver::stop()
public function run() */
{ public function stop();
($this->run)();
}
public function stop()
{
($this->stop)();
}
} }