Skip to content

Commit 79b0c6e

Browse files
committed
Tick callback receives no arguments, remove cyclic dependency
This can easily be replaced by using closure binding as documented.
1 parent 1e498d9 commit 79b0c6e

11 files changed

+74
-25
lines changed

README.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,33 @@ schedule a callback to be invoked on a future tick of the event loop.
126126
This works very much similar to timers with an interval of zero seconds,
127127
but does not require the overhead of scheduling a timer queue.
128128

129-
Unlike timers, callbacks are guaranteed to be executed in the order they
130-
are enqueued.
129+
The tick callback function MUST be able to accept zero parameters.
130+
131+
The tick callback function MUST NOT throw an `Exception`.
132+
The return value of the tick callback function will be ignored and has
133+
no effect, so for performance reasons you're recommended to not return
134+
any excessive data structures.
135+
136+
If you want to access any variables within your callback function, you
137+
can bind arbitrary data to a callback closure like this:
138+
139+
```php
140+
function hello(LoopInterface $loop, $name)
141+
{
142+
$loop->futureTick(function () use ($name) {
143+
echo "hello $name\n";
144+
});
145+
}
146+
147+
hello('Tester');
148+
```
149+
150+
Unlike timers, tick callbacks are guaranteed to be executed in the order
151+
they are enqueued.
131152
Also, once a callback is enqueued, there's no way to cancel this operation.
132153

133-
This is often used to break down bigger tasks into smaller steps (a form of
134-
cooperative multitasking).
154+
This is often used to break down bigger tasks into smaller steps (a form
155+
of cooperative multitasking).
135156

136157
```php
137158
$loop->futureTick(function () {

examples/91-benchmark-ticks.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
1010

1111
for ($i = 0; $i < $n; ++$i) {
12-
$loop->nextTick(function () { });
12+
$loop->futureTick(function () { });
1313
}
1414

1515
$loop->run();

examples/93-benchmark-ticks-delay.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
if ($ticks > 0) {
1212
--$ticks;
1313
//$loop->addTimer(0, $tick);
14-
$loop->nextTick($tick);
14+
$loop->futureTick($tick);
1515
} else {
1616
echo 'done';
1717
}

examples/94-benchmark-timers-delay.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
$tick = function () use (&$tick, &$ticks, $loop) {
1111
if ($ticks > 0) {
1212
--$ticks;
13-
//$loop->nextTick($tick);
13+
//$loop->futureTick($tick);
1414
$loop->addTimer(0, $tick);
1515
} else {
1616
echo 'done';

src/ExtEventLoop.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ExtEventLoop implements LoopInterface
2929
public function __construct(EventBaseConfig $config = null)
3030
{
3131
$this->eventBase = new EventBase($config);
32-
$this->futureTickQueue = new FutureTickQueue($this);
32+
$this->futureTickQueue = new FutureTickQueue();
3333
$this->timerEvents = new SplObjectStorage();
3434

3535
$this->createTimerCallback();

src/LibEvLoop.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class LibEvLoop implements LoopInterface
2626
public function __construct()
2727
{
2828
$this->loop = new EventLoop();
29-
$this->futureTickQueue = new FutureTickQueue($this);
29+
$this->futureTickQueue = new FutureTickQueue();
3030
$this->timerEvents = new SplObjectStorage();
3131
}
3232

src/LibEventLoop.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LibEventLoop implements LoopInterface
3030
public function __construct()
3131
{
3232
$this->eventBase = event_base_new();
33-
$this->futureTickQueue = new FutureTickQueue($this);
33+
$this->futureTickQueue = new FutureTickQueue();
3434
$this->timerEvents = new SplObjectStorage();
3535

3636
$this->createTimerCallback();

src/LoopInterface.php

+39-4
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,49 @@ public function isTimerActive(TimerInterface $timer);
9191
* This works very much similar to timers with an interval of zero seconds,
9292
* but does not require the overhead of scheduling a timer queue.
9393
*
94-
* Unlike timers, callbacks are guaranteed to be executed in the order they
95-
* are enqueued.
94+
* The tick callback function MUST be able to accept zero parameters.
95+
*
96+
* The tick callback function MUST NOT throw an `Exception`.
97+
* The return value of the tick callback function will be ignored and has
98+
* no effect, so for performance reasons you're recommended to not return
99+
* any excessive data structures.
100+
*
101+
* If you want to access any variables within your callback function, you
102+
* can bind arbitrary data to a callback closure like this:
103+
*
104+
* ```php
105+
* function hello(LoopInterface $loop, $name)
106+
* {
107+
* $loop->futureTick(function () use ($name) {
108+
* echo "hello $name\n";
109+
* });
110+
* }
111+
*
112+
* hello('Tester');
113+
* ```
114+
*
115+
* Unlike timers, tick callbacks are guaranteed to be executed in the order
116+
* they are enqueued.
96117
* Also, once a callback is enqueued, there's no way to cancel this operation.
97118
*
98-
* This is often used to break down bigger tasks into smaller steps (a form of
99-
* cooperative multitasking).
119+
* This is often used to break down bigger tasks into smaller steps (a form
120+
* of cooperative multitasking).
121+
*
122+
* ```php
123+
* $loop->futureTick(function () {
124+
* echo 'b';
125+
* });
126+
* $loop->futureTick(function () {
127+
* echo 'c';
128+
* });
129+
* echo 'a';
130+
* ```
131+
*
132+
* See also [example #3](examples).
100133
*
101134
* @param callable $listener The callback to invoke.
135+
*
136+
* @return void
102137
*/
103138
public function futureTick(callable $listener);
104139

src/StreamSelectLoop.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class StreamSelectLoop implements LoopInterface
2424

2525
public function __construct()
2626
{
27-
$this->futureTickQueue = new FutureTickQueue($this);
27+
$this->futureTickQueue = new FutureTickQueue();
2828
$this->timers = new Timers();
2929
}
3030

src/Tick/FutureTickQueue.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@
22

33
namespace React\EventLoop\Tick;
44

5-
use React\EventLoop\LoopInterface;
65
use SplQueue;
76

87
class FutureTickQueue
98
{
109
private $eventLoop;
1110
private $queue;
1211

13-
/**
14-
* @param LoopInterface $eventLoop The event loop passed as the first parameter to callbacks.
15-
*/
16-
public function __construct(LoopInterface $eventLoop)
12+
public function __construct()
1713
{
18-
$this->eventLoop = $eventLoop;
1914
$this->queue = new SplQueue();
2015
}
2116

@@ -41,8 +36,7 @@ public function tick()
4136

4237
while ($count--) {
4338
call_user_func(
44-
$this->queue->dequeue(),
45-
$this->eventLoop
39+
$this->queue->dequeue()
4640
);
4741
}
4842
}

tests/AbstractLoopTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ public function testFutureTick()
296296
{
297297
$called = false;
298298

299-
$callback = function ($loop) use (&$called) {
300-
$this->assertSame($this->loop, $loop);
299+
$callback = function () use (&$called) {
301300
$called = true;
302301
};
303302

0 commit comments

Comments
 (0)