Skip to content

Commit

Permalink
Merge pull request #26 from clue-labs/default-loop
Browse files Browse the repository at this point in the history
Simplify examples by updating to new default loop
  • Loading branch information
clue committed Jul 23, 2021
2 parents 9b403da + 81c0a94 commit e4b9b46
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ as parsed values instead of just chunks of strings:
```

```php
$stdin = new ReadableResourceStream(STDIN, $loop);
$stdin = new ReadableResourceStream(STDIN);

$stream = new Decoder($stdin);

Expand Down Expand Up @@ -234,7 +234,7 @@ and accepts its data through the same interface, but handles any data as complet
JSON elements instead of just chunks of strings:

```php
$stdout = new WritableResourceStream(STDOUT, $loop);
$stdout = new WritableResourceStream(STDOUT);

$stream = new Encoder($stdout);

Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
"email": "christian@clue.engineering"
}
],
"autoload": {
"psr-4": { "Clue\\React\\NDJson\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\NDJson\\": "tests/" }
},
"require": {
"php": ">=5.3",
"react/stream": "^1.0 || ^0.7 || ^0.6"
"react/stream": "^1.2"
},
"require-dev": {
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/event-loop": "^1.2",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
},
"autoload": {
"psr-4": { "Clue\\React\\NDJson\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\NDJson\\": "tests/" }
}
}
13 changes: 5 additions & 8 deletions examples/91-benchmark-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// $ php examples/91-benchmark-count.php < title.ratings.ndjson

use Clue\React\NDJson\Decoder;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -24,24 +24,21 @@
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
}

$loop = Factory::create();
$decoder = new Decoder(new ReadableResourceStream(STDIN, $loop), true);
$decoder = new Decoder(new ReadableResourceStream(STDIN), true);

$count = 0;
$decoder->on('data', function () use (&$count) {
++$count;
});

$start = microtime(true);
$report = $loop->addPeriodicTimer(0.05, function () use (&$count, $start) {
$report = Loop::addPeriodicTimer(0.05, function () use (&$count, $start) {
printf("\r%d records in %0.3fs...", $count, microtime(true) - $start);
});

$decoder->on('close', function () use (&$count, $report, $loop, $start) {
$decoder->on('close', function () use (&$count, $report, $start) {
$now = microtime(true);
$loop->cancelTimer($report);
Loop::cancelTimer($report);

printf("\r%d records in %0.3fs => %d records/s\n", $count, $now - $start, $count / ($now - $start));
});

$loop->run();
16 changes: 7 additions & 9 deletions examples/validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

// $ php examples/validate.php < examples/users.ndjson

use React\EventLoop\Factory;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;
use Clue\React\NDJson\Decoder;
use Clue\React\NDJson\Encoder;
use React\EventLoop\Loop;
use React\Stream\ReadableResourceStream;
use React\Stream\WritableResourceStream;

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

$loop = Factory::create();

$exit = 0;
$in = new ReadableResourceStream(STDIN, $loop);
$out = new WritableResourceStream(STDOUT, $loop);
$info = new WritableResourceStream(STDERR, $loop);
$in = new ReadableResourceStream(STDIN);
$out = new WritableResourceStream(STDOUT);
$info = new WritableResourceStream(STDERR);

$decoder = new Decoder($in);
$encoder = new Encoder($out);
Expand All @@ -30,6 +28,6 @@
$info->write('Valid NDJson will be forwarded to STDOUT' . PHP_EOL);
$info->write('Invalid NDJson will raise an error on STDERR and exit with code 1' . PHP_EOL);

$loop->run();
Loop::run();

exit($exit);

0 comments on commit e4b9b46

Please sign in to comment.