8000 Documentation for timer API and clean up unneeded timer API by clue · Pull Request #102 · reactphp/event-loop · GitHub
[go: up one dir, main page]

Skip to content

Documentation for timer API and clean up unneeded timer API #102

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 5 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< 8000 /strong>
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,131 @@ All of the loops support these features:
* Periodic timers
* Deferred execution on future loop tick

### addTimer()

The `addTimer(float $interval, callable $callback): TimerInterface` method can be used to
enqueue a callback to be invoked once after the given interval.

The timer callback function MUST be able to accept a single parameter,
the timer instance as also returned by this method or you MAY use a
function which has no parameters at all.

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

Unlike [`addPeriodicTimer()`](#addperiodictimer), this method will ensure
the callback will be invoked only once after the given interval.
You can invoke [`cancelTimer`](#canceltimer) to cancel a pending timer.

```php
$loop->addTimer(0.8, function () {
echo 'world!' . PHP_EOL;
});

$loop->addTimer(0.3, function () {
echo 'hello ';
});
```

See also [example #1](examples).

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->addTimer(1.0, function () use ($name) {
echo "hello $name\n";
});
}

hello('Tester');
```

10000 The execution order of timers scheduled to execute at the same time is
not guaranteed.

### addPeriodicTimer()

The `addPeriodicTimer(float $interval, callable $callback): TimerInterface` method can be used to
enqueue a callback to be invoked repeatedly after the given interval.

The timer callback function MUST be able to accept a single parameter,
the timer instance as also returned by this method or you MAY use a
function which has no parameters at all.

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

Unlike [`addTimer()`](#addtimer), this method will ensure the the
callback will be invoked infinitely after the given interval or until you
invoke [`cancelTimer`](#canceltimer).

```php
$timer = $loop->addPeriodicTimer(0.1, function () {
echo 'tick!' . PHP_EOL;
});

$loop->addTimer(1.0, function () use ($loop, $timer) {
$loop->cancelTimer($timer);
echo 'Done' . PHP_EOL;
});
```

See also [example #2](examples).

If you want to limit the number of executions, you can bind
arbitrary data to a callback closure like this:

```php
function hello(LoopInterface $loop, $name)
{
$n = 3;
$loop->addPeriodicTimer(1.0, function ($timer) use ($name, $loop, &$n) {
if ($n > 0) {
--$n;
echo "hello $name\n";
} else {
$loop->cancelTimer($timer);
}
});
}

hello('Tester');
```

The execution order of timers scheduled to execute at the same time is
not guaranteed.

### cancelTimer()

The `cancelTimer(TimerInterface $timer): void` method can be used to
cancel a pending timer.

See also [`addPeriodicTimer()`](#addperiodictimer) and [example #2](examples).

You can use the [`isTimerActive()`](#istimeractive) method to check if
this timer is still "active". After a timer is successfully canceled,
it is no longer considered "active".

Calling this method on a timer instance that has not been added to this
loop instance or on a timer

### isTimerActive()

The `isTimerActive(TimerInterface $timer): bool` method can be used to
check if a given timer is active.

A timer is considered "active" if it has been added to this loop instance
via [`addTimer()`](#addtimer) or [`addPeriodicTimer()`](#addperiodictimer)
and has not been canceled via [`cancelTimer()`](#canceltimer) and is not
a non-periodic timer that has already been triggered after its interval.

### futureTick()

The `futureTick(callable $listener): void` method can be used to
Expand Down
4 changes: 2 additions & 2 deletions src/ExtEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function removeStream($stream)
*/
public function addTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, false);
$timer = new Timer($interval, $callback, false);

$this->scheduleTimer($timer);

Expand All @@ -124,7 +124,7 @@ public function addTimer($interval, callable $callback)
*/
public function addPeriodicTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, true);
$timer = new Timer($interval, $callback, true);

$this->scheduleTimer($timer);

Expand Down
4 changes: 2 additions & 2 deletions src/LibEvLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function removeStream($stream)
*/
public function addTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, false);
$timer = new Timer( $interval, $callback, false);

$callback = function () use ($timer) {
call_user_func($timer->getCallback(), $timer);
Expand All @@ -130,7 +130,7 @@ public function addTimer($interval, callable $callback)
*/
public function addPeriodicTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, true);
$timer = new Timer($interval, $callback, true);

$callback = function () use ($timer) {
call_user_func($timer->getCallback(), $timer);
Expand Down
4 changes: 2 additions & 2 deletions src/LibEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function removeStream($stream)
*/
public function addTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, false);
$timer = new Timer($interval, $callback, false);

$this->scheduleTimer($timer);

Expand All @@ -128,7 +128,7 @@ public function addTimer($interval, callable $callback)
*/
public function addPeriodicTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, true);
$timer = new Timer($interval, $callback, true);

$this->scheduleTimer($timer);

Expand Down
102 changes: 102 additions & 0 deletions src/LoopInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,45 @@ public function removeStream($stream);
/**
* Enqueue a callback to be invoked once after the given interval.
*
* The timer callback function MUST be able to accept a single parameter,
* the timer instance as also returned by this method or you MAY use a
* function which has no parameters at all.
*
* The timer callback function MUST NOT throw an `Exception`.
* The return value of the timer callback function will be ignored and has
* no effect, so for performance reasons you're recommended to not return
* any excessive data structures.
*
* Unlike [`addPeriodicTimer()`](#addperiodictimer), this method will ensure
* the callback will be invoked only once after the given interval.
* You can invoke [`cancelTimer`](#canceltimer) to cancel a pending timer.
*
* ```php
* $loop->addTimer(0.8, function () {
* echo 'world!' . PHP_EOL;
* });
*
* $loop->addTimer(0.3, function () {
* echo 'hello ';
* });
* ```
*
* See also [example #1](examples).
*
* 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->addTimer(1.0, function () use ($name) {
* echo "hello $name\n";
* });
* }
*
* hello('Tester');
* ```
*
* The execution order of timers scheduled to execute at the same time is
* not guaranteed.
*
Expand All @@ -59,6 +98,52 @@ public function addTimer($interval, callable $callback);
/**
* Enqueue a callback to be invoked repeatedly after the given interval.
*
* The timer callback function MUST be able to accept a single parameter,
* the timer instance as also returned by this method or you MAY use a
* function which has no parameters at all.
*
* The timer callback function MUST NOT throw an `Exception`.
* The return value of the timer callback function will be ignored and has
* no effect, so for performance reasons you're recommended to not return
* any excessive data structures.
*
* Unlike [`addTimer()`](#addtimer), this method will ensure the the
* callback will be invoked infinitely after the given interval or until you
* invoke [`cancelTimer`](#canceltimer).
*
* ```php
* $timer = $loop->addPeriodicTimer(0.1, function () {
* echo 'tick!' . PHP_EOL;
* });
*
* $loop->addTimer(1.0, function () use ($loop, $timer) {
* $loop->cancelTimer($timer);
* echo 'Done' . PHP_EOL;
* });
* ```
*
* See also [example #2](examples).
*
* If you want to limit the number of executions, you can bind
* arbitrary data to a callback closure like this:
*
* ```php
* function hello(LoopInterface $loop, $name)
* {
* $n = 3;
* $loop->addPeriodicTimer(1.0, function ($timer) use ($name, $loop, &$n) {
* if ($n > 0) {
* --$n;
* echo "hello $name\n";
* } else {
* $loop->cancelTimer($timer);
* }
* });
* }
*
* hello('Tester');
* ```
*
* The execution order of timers scheduled to execute at the same time is
* not guaranteed.
*
Expand All @@ -72,13 +157,30 @@ public function addPeriodicTimer($interval, callable $callback);
/**
* Cancel a pending timer.
*
* See also [`addPeriodicTimer()`](#addperiodictimer) and [example #2](examples).
*
* You can use the [`isTimerActive()`](#istimeractive) method to check if
* this timer is still "active". After a timer is successfully canceled,
* it is no longer considered "active".
*
* Calling this method on a timer instance that has not been added to this
* loop instance or on a timer that is not "active" (or has already been
* canceled) has no effect.
*
* @param TimerInterface $timer The timer to cancel.
*
* @return void
*/
public function cancelTimer(TimerInterface $timer);

/**
* Check if a given timer is active.
*
* A timer is considered "active" if it has been added to this loop instance
* via [`addTimer()`](#addtimer) or [`addPeriodicTimer()`](#addperiodictimer)
* and has not been canceled via [`cancelTimer()`](#canceltimer) and is not
* a non-periodic timer that has already been triggered after its interval.
*
* @param TimerInterface $timer The timer to check.
*
* @return boolean True if the timer is still enqueued for execution.
Expand Down
4 changes: 2 additions & 2 deletions src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function removeStream($stream)
*/
public function addTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, false);
$timer = new Timer($interval, $callback, false);

$this->timers->add($timer);

Expand All @@ -106,7 +106,7 @@ public function addTimer($interval, callable $callback)
*/
public function addPeriodicTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, true);
$timer = new Timer($interval, $callback, true);

$this->timers->add($timer);

Expand Down
Loading
0