Skip to content

Commit

Permalink
Documentation for ThroughStream
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Apr 24, 2017
1 parent 7c338ad commit e1ddc34
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ descriptor based implementation with an in-memory write buffer.
* [ReadableResourceStream](#readableresourcestream)
* [WritableResourceStream](#writableresourcestream)
* [DuplexResourceStream](#duplexresourcestream)
* [ThroughStream](#throughstream)
* [Usage](#usage)
* [Install](#install)
* [Tests](#tests)
Expand Down Expand Up @@ -924,6 +925,53 @@ $buffer->softLimit = 8192;

See also [`write()`](#write) for more details.

### ThroughStream

The `ThroughStream` implements the
[`DuplexStreamInterface`](#duplexstreaminterface) and will simply pass any data
you write to it through to its readable end.

```php
$through = new ThroughStream();
$through->on('data', $this->expectCallableOnceWith('hello'));

$through->write('hello');
```

Similarly, the [`end()` method](#end) will end the stream and emit an
[`end` event](#end-event) and then [`close()`](#close-1) the stream.
The [`close()` method](#close-1) will close the stream and emit a
[`close` event](#close-event).
Accordingly, this is can also be used in a [`pipe()`](#pipe) context like this:

```php
$through = new ThroughStream();
$source->pipe($through)->pipe($dest);
```

Optionally, its constructor accepts any callable function which will then be
used to *filter* any data written to it. This function receives a single data
argument as passed to the writable side and must return the data as it will be
passed to its readable end:

```php
$through = new ThroughStream('strtoupper');
$source->pipe($through)->pipe($dest);
```

Note that this class makes no assumptions about any data types. This can be
used to convert data, for example for transforming any structured data into
a newline-delimited JSON (NDJSON) stream like this:

```php
$through = new ThroughStream(function ($data) {
return json_encode($data) . PHP_EOL;
});
$through->on('data', $this->expectCallableOnceWith("[2, true]\n"));

$through->write(array(2, true));
```

## Usage
```php
$loop = React\EventLoop\Factory::create();
Expand Down
51 changes: 51 additions & 0 deletions src/ThroughStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,57 @@
use Evenement\EventEmitter;
use InvalidArgumentException;

/**
* The `ThroughStream` implements the
* [`DuplexStreamInterface`](#duplexstreaminterface) and will simply pass any data
* you write to it through to its readable end.
*
* ```php
* $through = new ThroughStream();
* $through->on('data', $this->expectCallableOnceWith('hello'));
*
* $through->write('hello');
* ```
*
* Similarly, the [`end()` method](#end) will end the stream and emit an
* [`end` event](#end-event) and then [`close()`](#close-1) the stream.
* The [`close()` method](#close-1) will close the stream and emit a
* [`close` event](#close-event).
* Accordingly, this is can also be used in a [`pipe()`](#pipe) context like this:
*
* ```php
* $through = new ThroughStream();
* $source->pipe($through)->pipe($dest);
* ```
*
* Optionally, its constructor accepts any callable function which will then be
* used to *filter* any data written to it. This function receives a single data
* argument as passed to the writable side and must return the data as it will be
* passed to its readable end:
*
* ```php
* $through = new ThroughStream('strtoupper');
* $source->pipe($through)->pipe($dest);
* ```
*
* Note that this class makes no assumptions about any data types. This can be
* used to convert data, for example for transforming any structured data into
* a newline-delimited JSON (NDJSON) stream like this:
*
* ```php
* $through = new ThroughStream(function ($data) {
* return json_encode($data) . PHP_EOL;
* });
* $through->on('data', $this->expectCallableOnceWith("[2, true]\n"));
*
* $through->write(array(2, true));
* ```
*
* @see WritableStreamInterface::write()
* @see WritableStreamInterface::end()
* @see DuplexStreamInterface::close()
* @see WritableStreamInterface::pipe()
*/
class ThroughStream extends EventEmitter implements DuplexStreamInterface
{
private $readable = true;
Expand Down

0 comments on commit e1ddc34

Please sign in to comment.