Skip to content

Tick callback receives no arguments, fix cyclic dependency #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,33 @@ schedule a callback to be invoked on a future tick of the event loop.
This works very much similar to timers with an interval of zero seconds,
but does not require the overhead of scheduling a timer queue.

Unlike timers, callbacks are guaranteed to be executed in the order they
are enqueued.
The tick callback function MUST be able to accept zero parameters.

The tick callback function MUST NOT throw an `Exception`.
The return value of the tick callback function will be ignored and has
no effect, so for performance reasons you're recommended to not return
any excessive data structures.

If you want to access any variables within your callback function, you
can bind arbitrary data to a callback closure like this:

```php
function hello(LoopInterface $loop, $name)
{
$loop->futureTick(function () use ($name) {
echo "hello $name\n";
});
}

hello('Tester');
```

Unlike timers, tick callbacks are guaranteed to be executed in the order
they are enqueued.
Also, once a callback is enqueued, there's no way to cancel this operation.

This is often used to break down bigger tasks into smaller steps (a form of
cooperative multitasking).
This is often used to break down bigger tasks into smaller steps (a form
of cooperative multitasking).

```php
$loop->futureTick(function () {
Expand Down
2 changes: 1 addition & 1 deletion examples/91-benchmark-ticks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;

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

$loop->run();
2 changes: 1 addition & 1 deletion examples/93-benchmark-ticks-delay.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
if ($ticks > 0) {
--$ticks;
//$loop->addTimer(0, $tick);
$loop->nextTick($tick);
$loop->futureTick($tick);
} else {
echo 'done';
}
Expand Down
2 changes: 1 addition & 1 deletion examples/94-benchmark-timers-delay.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$tick = function () use (&$tick, &$ticks, $loop) {
if ($ticks > 0) {
--$ticks;
//$loop->nextTick($tick);
//$loop->futureTick($tick);
$loop->addTimer(0, $tick);
} else {
echo 'done';
Expand Down
2 changes: 1 addition & 1 deletion src/ExtEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ExtEventLoop implements LoopInterface
public function __construct(EventBaseConfig $config = null)
{
$this->eventBase = new EventBase($config);
$this->futureTickQueue = new FutureTickQueue($this);
$this->futureTickQueue = new FutureTickQueue();
$this->timerEvents = new SplObjectStorage();

$this->createTimerCallback();
Expand Down
2 changes: 1 addition & 1 deletion src/LibEvLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LibEvLoop implements LoopInterface
public function __construct()
{
$this->loop = new EventLoop();
$this->futureTickQueue = new FutureTickQueue($this);
$this->futureTickQueue = new FutureTickQueue();
$this->timerEvents = new SplObjectStorage();
}

Expand Down
2 changes: 1 addition & 1 deletion src/LibEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LibEventLoop implements LoopInterface
public function __construct()
{
$this->eventBase = event_base_new();
$this->futureTickQueue = new FutureTickQueue($this);
$this->futureTickQueue = new FutureTickQueue();
$this->timerEvents = new SplObjectStorage();

$this->createTimerCallback();
Expand Down
43 changes: 39 additions & 4 deletions src/LoopInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,49 @@ public function isTimerActive(TimerInterface $timer);
* This works very much similar to timers with an interval of zero seconds,
* but does not require the overhead of scheduling a timer queue.
*
* Unlike timers, callbacks are guaranteed to be executed in the order they
* are enqueued.
* The tick callback function MUST be able to accept zero parameters.
*
* The tick callback function MUST NOT throw an `Exception`.
* The return value of the tick callback function will be ignored and has
* no effect, so for performance reasons you're recommended to not return
* any excessive data structures.
*
* If you want to access any variables within your callback function, you
* can bind arbitrary data to a callback closure like this:
*
* ```php
* function hello(LoopInterface $loop, $name)
* {
* $loop->futureTick(function () use ($name) {
* echo "hello $name\n";
* });
* }
*
* hello('Tester');
* ```
*
* Unlike timers, tick callbacks are guaranteed to be executed in the order
* they are enqueued.
* Also, once a callback is enqueued, there's no way to cancel this operation.
*
* This is often used to break down bigger tasks into smaller steps (a form of
* cooperative multitasking).
* This is often used to break down bigger tasks into smaller steps (a form
* of cooperative multitasking).
*
* ```php
* $loop->futureTick(function () {
* echo 'b';
* });
* $loop->futureTick(function () {
* echo 'c';
* });
* echo 'a';
* ```
*
* See also [example #3](examples).
*
* @param callable $listener The callback to invoke.
*
* @return void
*/
public function futureTick(callable $listener);

Expand Down
2 changes: 1 addition & 1 deletion src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StreamSelectLoop implements LoopInterface

public function __construct()
{
$this->futureTickQueue = new FutureTickQueue($this);
$this->futureTickQueue = new FutureTickQueue();
$this->timers = new Timers();
}

Expand Down
21 changes: 11 additions & 10 deletions src/Tick/FutureTickQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

namespace React\EventLoop\Tick;

use React\EventLoop\LoopInterface;
use SplQueue;

class FutureTickQueue
/**
* A tick queue implementation that can hold multiple callback functions
*
* This class should only be used internally, see LoopInterface instead.
*
* @see LoopInterface
* @internal
*/
final class FutureTickQueue
{
private $eventLoop;
private $queue;

/**
* @param LoopInterface $eventLoop The event loop passed as the first parameter to callbacks.
*/
public function __construct(LoopInterface $eventLoop)
public function __construct()
{
$this->eventLoop = $eventLoop;
$this->queue = new SplQueue();
}

Expand All @@ -41,8 +43,7 @@ public function tick()

while ($count--) {
call_user_func(
$this->queue->dequeue(),
$this->eventLoop
$this->queue->dequeue()
);
}
}
Expand Down
3 changes: 1 addition & 2 deletions tests/AbstractLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ public function testFutureTick()
{
$called = false;

$callback = function ($loop) use (&$called) {
$this->assertSame($this->loop, $loop);
$callback = function () use (&$called) {
$called = true;
};

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy