public
Last active

  • Download Gist
deferred.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
<?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();
output.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
.
.
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 ...
.
.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.