Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace React\EventLoop\Tick;
4
 
5
use React\EventLoop\LoopInterface;
6
use SplQueue;
7
 
8
class NextTickQueue
9
{
10
    private $eventLoop;
11
    private $queue;
12
 
13
    /**
14
     * @param LoopInterface $eventLoop The event loop passed as the first parameter to callbacks.
15
     */
16
    public function __construct(LoopInterface $eventLoop)
17
    {
18
        $this->eventLoop = $eventLoop;
19
        $this->queue = new SplQueue();
20
    }
21
 
22
    /**
23
     * Add a callback to be invoked on the next tick of the event loop.
24
     *
25
     * Callbacks are guaranteed to be executed in the order they are enqueued,
26
     * before any timer or stream events.
27
     *
28
     * @param callable $listener The callback to invoke.
29
     */
30
    public function add(callable $listener)
31
    {
32
        $this->queue->enqueue($listener);
33
    }
34
 
35
    /**
36
     * Flush the callback queue.
37
     */
38
    public function tick()
39
    {
40
        while (!$this->queue->isEmpty()) {
41
            call_user_func(
42
                $this->queue->dequeue(),
43
                $this->eventLoop
44
            );
45
        }
46
    }
47
 
48
    /**
49
     * Check if the next tick queue is empty.
50
     *
51
     * @return boolean
52
     */
53
    public function isEmpty()
54
    {
55
        return $this->queue->isEmpty();
56
    }
57
}