Skip to content
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

Simplify examples by updating to new default loop #26

Merged
merged 1 commit into from
Jul 23, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);