Last active
August 30, 2016 21:37
-
-
Save ASergey/8022987 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once './vendor/autoload.php'; | |
abstract class Element | |
{ | |
protected $loop; | |
public $active = false; | |
public function __construct(\React\EventLoop\LoopInterface $loop) | |
{ | |
$this->loop = $loop; | |
} | |
abstract public function synchronize(); | |
} | |
class A extends Element | |
{ | |
public function synchronize() | |
{ | |
$deferred = new \React\Promise\Deferred(); | |
echo 'Processing A ...'.PHP_EOL; | |
$i = 7; | |
$this->loop->addPeriodicTimer(0.5, function ($time) use (&$i, $deferred) { | |
echo date('H:i:s').'| $A: '.$i.PHP_EOL; | |
sleep(1); | |
$i--; | |
if ($i <= 0) { | |
$time->cancel(); | |
$deferred->resolve(); | |
echo 'Finished processing A ...'.PHP_EOL; | |
} | |
}); | |
return $deferred->promise(); | |
} | |
} | |
class B extends Element | |
{ | |
public function synchronize() | |
{ | |
$deferred = new \React\Promise\Deferred(); | |
echo 'Processing B ...'.PHP_EOL; | |
$i = 5; | |
$this->loop->addPeriodicTimer(0.5, function ($time) use (&$i, $deferred) { | |
echo date('H:i:s').'| $B: '.$i.PHP_EOL; | |
sleep(1); | |
$i--; | |
if ($i <= 0) { | |
$time->cancel(); | |
$deferred->resolve(); | |
echo 'Finished processing B ...'.PHP_EOL; | |
} | |
}); | |
return $deferred->promise(); | |
} | |
} | |
class Test | |
{ | |
protected $loop; | |
public $deferred; | |
public $a; | |
public $b; | |
public function __construct() | |
{ | |
$this->loop = React\EventLoop\Factory::create(); | |
$this->deferred = new \React\Promise\Deferred(); | |
$this->promise = $this->deferred->promise(); | |
$this->a = new A($this->loop); | |
$this->b = new B($this->loop); | |
} | |
public function run() | |
{ | |
$that = $this; | |
$this->loop->addPeriodicTimer(1, function () { | |
echo '.'.PHP_EOL; | |
}); | |
$this->loop->addTimer(5, function () use ($that) { | |
echo 'run a'.PHP_EOL; | |
$that->deferred = $that->deferred->then($that->a->synchronize()); | |
}); | |
$this->loop->addTimer(3, function () use ($that) { | |
echo 'run b'.PHP_EOL; | |
$that->deferred = $that->deferred->then($that->b->synchronize()); | |
}); | |
$this->loop->addTimer(6, function () use ($that) { | |
echo 'run b once more'.PHP_EOL; | |
$that->deferred = $that->deferred->then($that->b->synchronize()); | |
}); | |
$this->loop->run(); | |
} | |
} | |
$test = new Test(); | |
$test->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
. | |
run b | |
Processing B ... | |
. | |
14:11:31| $B: 5 | |
. | |
14:11:33| $B: 4 | |
run a | |
Processing A ... | |
14:11:34| $B: 3 | |
14:11:35| $A: 7 | |
run b once more | |
Processing B ... | |
. | |
14:11:37| $B: 2 | |
14:11:38| $B: 5 | |
14:11:39| $A: 6 | |
. | |
14:11:40| $B: 1 | |
Finished processing B ... | |
14:11:41| $A: 5 | |
14:11:42| $B: 4 | |
. | |
14:11:44| $A: 4 | |
14:11:45| $B: 3 | |
. | |
14:11:46| $A: 3 | |
14:11:47| $B: 2 | |
. | |
14:11:49| $A: 2 | |
14:11:50| $B: 1 | |
Finished processing B ... | |
. | |
14:11:51| $A: 1 | |
Finished processing A ... | |
. | |
. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment