Download the PHP package nick4fake/promise-stream-react without Composer
On this page you can find all versions of the php package nick4fake/promise-stream-react. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package promise-stream-react
clue/promise-stream-react
The missing link between Promise-land and Stream-land, built on top of React PHP.
Table of Contents
- Usage
- buffer()
- first()
- all()
- unwrapReadable()
- unwrapWritable()
- Install
- License
Note: This project is in beta stage! Feel free to report any issues you encounter.
Usage
This lightweight library consists only of a few simple functions.
All functions reside under the Clue\React\Promise\Stream
namespace.
The below examples assume you use an import statement similar to this:
Alternatively, you can also refer to them with their fully-qualified name:
buffer()
The buffer(ReadableStreamInterface $stream)
function can be used to create
a Promise
which resolves with the stream data buffer.
The promise will resolve with all data chunks concatenated once the stream closes.
The promise will resolve with an empty string if the stream is already closed.
The promise will reject if the stream emits an error.
The promise will reject if it is canceled.
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.
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.
The promise will reject once the stream closes – unless you're waiting for the "close" event, in which case it will resolve.
The promise will reject if the stream is already closed.
The promise will reject if it is canceled.
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.
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.
The promise will resolve with an array once the stream closes.
The promise will resolve with an empty array if the stream is already closed.
The promise will reject if the stream emits an error.
The promise will reject if it is canceled.
unwrapReadable()
The unwrapReadable(PromiseInterface $promise)
function can be used to unwrap
a Promise
which resolves with a ReadableStreamInterface
.
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.
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:
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.
unwrapWritable()
The unwrapWritable(PromiseInterface $promise)
function can be used to unwrap
a Promise
which resolves with a WritableStreamInterface
.
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.
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:
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.
Install
The recommended way to install this library is through Composer. New to Composer?
This will install the latest supported version:
See also the CHANGELOG for details about version upgrades.
License
MIT