Deprecation notice
This package has now been migrated over to
react/promise-stream
and only exists for BC reasons., (*1)
$ composer require react/promise-stream
If you've previously used this package, upgrading should take no longer than a few minutes.
All functions have been merged as-is from the latest v0.1.2 release with no other changes,
so you can simply update your code to use the updated namespace like this:, (*2)
// old from clue/promise-stream-react
\Clue\React\Promise\Stream\buffer(…);
// new
\React\Promise\Stream\buffer(…);
See react/promise-stream for more details., (*3)
The below documentation applies to the last release of this package.
Further development will take place in the updated
react/promise-stream,
so you're highly recommended to upgrade as soon as possible., (*4)
Legacy clue/promise-stream-react
The missing link between Promise-land and Stream-land,
built on top of React PHP., (*5)
Table of Contents, (*6)
Note: This project is in beta stage! Feel free to report any issues you encounter., (*7)
Usage
This lightweight library consists only of a few simple functions.
All functions reside under the Clue\React\Promise\Stream
namespace., (*8)
The below examples assume you use an import statement similar to this:, (*9)
use Clue\React\Promise\Stream;
Stream\buffer(…);
Alternatively, you can also refer to them with their fully-qualified name:, (*10)
\Clue\React\Promise\Stream\buffer(…);
buffer()
The buffer(ReadableStreamInterface $stream)
function can be used to create
a Promise
which resolves with the stream data buffer., (*11)
$stream = accessSomeJsonStream();
Stream\buffer($stream)->then(function ($contents) {
var_dump(json_decode($contents));
});
The promise will resolve with all data chunks concatenated once the stream closes., (*12)
The promise will resolve with an empty string if the stream is already closed., (*13)
The promise will reject if the stream emits an error., (*14)
The promise will reject if it is canceled., (*15)
first()
The first(ReadableStreamInterface|WritableStreamInterface $stream, $event = 'data')
function can be used to create a Promise
which resolves once the given event triggers for the first time., (*16)
$stream = accessSomeJsonStream();
Stream\first($stream)->then(function ($chunk) {
echo 'The first chunk arrived: ' . $chunk;
});
The promise will resolve with whatever the first event emitted or null
if the
event does not pass any data.
If you do not pass a custom event name, then it will wait for the first "data"
event and resolve with a string containing the first data chunk., (*17)
The promise will reject once the stream closes – unless you're waiting for the
"close" event, in which case it will resolve., (*18)
The promise will reject if the stream is already closed., (*19)
The promise will reject if it is canceled., (*20)
all()
The all(ReadableStreamInterface|WritableStreamInterface $stream, $event = 'data')
function can be used to create a Promise
which resolves with an array of all the event data., (*21)
$stream = accessSomeJsonStream();
Stream\all($stream)->then(function ($chunks) {
echo 'The stream consists of ' . count($chunks) . ' chunk(s)';
});
The promise will resolve with an array of whatever all events emitted or null
if the
events do not pass any data.
If you do not pass a custom event name, then it will wait for all the "data"
events and resolve with an array containing all the data chunks., (*22)
The promise will resolve with an array once the stream closes., (*23)
The promise will resolve with an empty array if the stream is already closed., (*24)
The promise will reject if the stream emits an error., (*25)
The promise will reject if it is canceled., (*26)
unwrapReadable()
The unwrapReadable(PromiseInterface $promise)
function can be used to unwrap
a Promise
which resolves with a ReadableStreamInterface
., (*27)
This function returns a readable stream instance (implementing ReadableStreamInterface
)
right away which acts as a proxy for the future promise resolution.
Once the given Promise resolves with a ReadableStreamInterface
, its data will
be piped to the output stream., (*28)
//$promise = someFunctionWhichResolvesWithAStream();
$promise = startDownloadStream($uri);
$stream = Stream\unwrapReadable($promise);
$stream->on('data', function ($data) {
echo $data;
});
$stream->on('end', function () {
echo 'DONE';
});
If the given promise is either rejected or fulfilled with anything but an
instance of ReadableStreamInterface
, then the output stream will emit
an error
event and close:, (*29)
$promise = startDownloadStream($invalidUri);
$stream = Stream\unwrapReadable($promise);
$stream->on('error', function (Exception $error) {
echo 'Error: ' . $error->getMessage();
});
The given $promise
SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected
at the time of invoking this function.
If the given promise is already settled and does not resolve with an
instance of ReadableStreamInterface
, then you will not be able to receive
the error
event., (*30)
unwrapWritable()
The unwrapWritable(PromiseInterface $promise)
function can be used to unwrap
a Promise
which resolves with a WritableStreamInterface
., (*31)
This function returns a writable stream instance (implementing WritableStreamInterface
)
right away which acts as a proxy for the future promise resolution.
Once the given Promise resolves with a WritableStreamInterface
, any data you
wrote to the proxy will be piped to the inner stream., (*32)
//$promise = someFunctionWhichResolvesWithAStream();
$promise = startUploadStream($uri);
$stream = Stream\unwrapWritable($promise);
$stream->write('hello');
$stream->end('world');
$stream->on('close', function () {
echo 'DONE';
});
If the given promise is either rejected or fulfilled with anything but an
instance of WritableStreamInterface
, then the output stream will emit
an error
event and close:, (*33)
$promise = startUploadStream($invalidUri);
$stream = Stream\unwrapWritable($promise);
$stream->on('error', function (Exception $error) {
echo 'Error: ' . $error->getMessage();
});
The given $promise
SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected
at the time of invoking this function.
If the given promise is already settled and does not resolve with an
instance of WritableStreamInterface
, then you will not be able to receive
the error
event., (*34)
Install
The recommended way to install this library is through Composer.
New to Composer?, (*35)
This will install the latest supported version:, (*36)
$ composer require clue/promise-stream-react:^0.1.2
See also the CHANGELOG for details about version upgrades., (*37)
License
MIT, (*38)