Skip to content

Commit

Permalink
Update usage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jun 30, 2021
1 parent 75a59f1 commit 86b5f82
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ single [`run()`](#run) call that is controlled by the user.
Here is an async HTTP server built with just the event loop.

```php
<?php

use React\EventLoop\Loop;

require __DIR__ . '/vendor/autoload.php';

$server = stream_socket_server('tcp://127.0.0.1:8080');
stream_set_blocking($server, false);

Expand Down Expand Up @@ -81,14 +85,15 @@ See also the [examples](examples).
## Usage

As of `v1.2.0`, typical applications would use the [`Loop` object](#loop)
to use the currently active event loop instance like this:
to use the currently active event loop like this:

```php
use React\EventLoop\Loop;

$timer = Loop::addPeriodicTimer(0.1, function () {
echo "Tick" . PHP_EOL;
echo 'Tick' . PHP_EOL;
});

Loop::addTimer(1.0, function () use ($timer) {
Loop::cancelTimer($timer);
echo 'Done' . PHP_EOL;
Expand All @@ -105,8 +110,9 @@ program like this:
$loop = React\EventLoop\Loop::get(); // or deprecated React\EventLoop\Factory::create();

$timer = $loop->addPeriodicTimer(0.1, function () {
echo "Tick" . PHP_EOL;
echo 'Tick' . PHP_EOL;
});

$loop->addTimer(1.0, function () use ($loop, $timer) {
$loop->cancelTimer($timer);
echo 'Done' . PHP_EOL;
Expand Down Expand Up @@ -163,7 +169,7 @@ like this:
use React\EventLoop\Loop;

$timer = Loop::addPeriodicTimer(0.1, function () {
echo 'tick!' . PHP_EOL;
echo 'Tick' . PHP_EOL;
});

Loop::addTimer(1.0, function () use ($timer) {
Expand Down
2 changes: 1 addition & 1 deletion examples/02-periodic.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require __DIR__ . '/../vendor/autoload.php';

$timer = Loop::addPeriodicTimer(0.1, function () {
echo 'tick!' . PHP_EOL;
echo 'Tick' . PHP_EOL;
});

Loop::addTimer(1.0, function () use ($timer) {
Expand Down

0 comments on commit 86b5f82

Please sign in to comment.