| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\EventLoop\Tick; |
| 4 |
|
| 5 |
use SplQueue; |
| 6 |
|
| 7 |
/** |
| 8 |
* A tick queue implementation that can hold multiple callback functions |
| 9 |
* |
| 10 |
* This class should only be used internally, see LoopInterface instead. |
| 11 |
* |
| 12 |
* @see LoopInterface |
| 13 |
* @internal |
| 14 |
*/ |
| 15 |
final class FutureTickQueue |
| 16 |
{ |
| 17 |
private $queue; |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$this->queue = new SplQueue(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Add a callback to be invoked on a future tick of the event loop. |
| 26 |
* |
| 27 |
* Callbacks are guaranteed to be executed in the order they are enqueued. |
| 28 |
* |
| 29 |
* @param callable $listener The callback to invoke. |
| 30 |
*/ |
| 31 |
public function add($listener) |
| 32 |
{ |
| 33 |
$this->queue->enqueue($listener); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Flush the callback queue. |
| 38 |
*/ |
| 39 |
public function tick() |
| 40 |
{ |
| 41 |
// Only invoke as many callbacks as were on the queue when tick() was called. |
| 42 |
$count = $this->queue->count(); |
| 43 |
|
| 44 |
while ($count--) { |
| 45 |
\call_user_func( |
| 46 |
$this->queue->dequeue() |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Check if the next tick queue is empty. |
| 53 |
* |
| 54 |
* @return boolean |
| 55 |
*/ |
| 56 |
public function isEmpty() |
| 57 |
{ |
| 58 |
return $this->queue->isEmpty(); |
| 59 |
} |
| 60 |
} |