Skip to content

Commit 34da49d

Browse files
authored
Merge pull request #27 from clue-labs/docs
2 parents 57b90ea + e5f9033 commit 34da49d

File tree

2 files changed

+140
-128
lines changed

2 files changed

+140
-128
lines changed

README.md

Lines changed: 80 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ for [ReactPHP](https://reactphp.org/).
88
**Table of Contents**
99

1010
* [Usage](#usage)
11-
* [buffer()](#buffer)
12-
* [first()](#first)
13-
* [all()](#all)
14-
* [unwrapReadable()](#unwrapreadable)
15-
* [unwrapWritable()](#unwrapwritable)
11+
* [buffer()](#buffer)
12+
* [first()](#first)
13+
* [all()](#all)
14+
* [unwrapReadable()](#unwrapreadable)
15+
* [unwrapWritable()](#unwrapwritable)
1616
* [Install](#install)
1717
* [Tests](#tests)
1818
* [License](#license)
@@ -22,49 +22,57 @@ for [ReactPHP](https://reactphp.org/).
2222
This lightweight library consists only of a few simple functions.
2323
All functions reside under the `React\Promise\Stream` namespace.
2424

25-
The below examples assume you use an import statement similar to this:
25+
The below examples assume refer to them with their fully-qualified names like this:
2626

2727
```php
28-
use React\Promise\Stream;
28+
React\Promise\Stream\buffer(…);
29+
```
2930

30-
Stream\buffer(…);
31+
As of PHP 5.6+ you can also import each required function into your code like this:
32+
33+
```php
34+
use function React\Promise\Stream\buffer;
35+
36+
buffer(…);
3137
```
3238

33-
Alternatively, you can also refer to them with their fully-qualified name:
39+
Alternatively, you can also use an import statement similar to this:
3440

3541
```php
36-
\React\Promise\Stream\buffer(…);
42+
use React\Promise\Stream;
43+
44+
Stream\buffer(…);
3745
```
3846

3947
### buffer()
4048

41-
The `buffer(ReadableStreamInterface<string> $stream, ?int $maxLength = null): PromiseInterface<string,Exception>` function can be used to
42-
create a `Promise` which resolves with the stream data buffer.
49+
The `buffer(ReadableStreamInterface<string> $stream, ?int $maxLength = null): PromiseInterface<string,RuntimeException>` function can be used to
50+
create a `Promise` which will be fulfilled with the stream data buffer.
4351

4452
```php
4553
$stream = accessSomeJsonStream();
4654

47-
Stream\buffer($stream)->then(function ($contents) {
55+
React\Promise\Stream\buffer($stream)->then(function (string $contents) {
4856
var_dump(json_decode($contents));
4957
});
5058
```
5159

52-
The promise will resolve with all data chunks concatenated once the stream closes.
60+
The promise will be fulfilled with a `string` of all data chunks concatenated once the stream closes.
5361

54-
The promise will resolve with an empty string if the stream is already closed.
62+
The promise will be fulfilled with an empty `string` if the stream is already closed.
5563

56-
The promise will reject if the stream emits an error.
64+
The promise will be rejected with a `RuntimeException` if the stream emits an error.
5765

58-
The promise will reject if it is cancelled.
66+
The promise will be rejected with a `RuntimeException` if it is cancelled.
5967

6068
The optional `$maxLength` argument defaults to no limit. In case the maximum
6169
length is given and the stream emits more data before the end, the promise
62-
will be rejected with an `\OverflowException`.
70+
will be rejected with an `OverflowException`.
6371

6472
```php
6573
$stream = accessSomeToLargeStream();
6674

67-
Stream\buffer($stream, 1024)->then(function ($contents) {
75+
React\Promise\Stream\buffer($stream, 1024)->then(function ($contents) {
6876
var_dump(json_decode($contents));
6977
}, function ($error) {
7078
// Reaching here when the stream buffer goes above the max size,
@@ -75,75 +83,77 @@ Stream\buffer($stream, 1024)->then(function ($contents) {
7583

7684
### first()
7785

78-
The `first(ReadableStreamInterface|WritableStreamInterface $stream, string $event = 'data'): PromiseInterface<mixed,Exception>` function can be used to
79-
create a `Promise` which resolves once the given event triggers for the first time.
86+
The `first(ReadableStreamInterface|WritableStreamInterface $stream, string $event = 'data'): PromiseInterface<mixed,RuntimeException>` function can be used to
87+
create a `Promise` which will be fulfilled once the given event triggers for the first time.
8088

8189
```php
8290
$stream = accessSomeJsonStream();
8391

84-
Stream\first($stream)->then(function ($chunk) {
92+
React\Promise\Stream\first($stream)->then(function (string $chunk) {
8593
echo 'The first chunk arrived: ' . $chunk;
8694
});
8795
```
8896

89-
The promise will resolve with whatever the first event emitted or `null` if the
90-
event does not pass any data.
97+
The promise will be fulfilled with a `mixed` value of whatever the first event
98+
emitted or `null` if the event does not pass any data.
9199
If you do not pass a custom event name, then it will wait for the first "data"
92-
event and resolve with a string containing the first data chunk.
100+
event.
101+
For common streams of type `ReadableStreamInterface<string>`, this means it will be
102+
fulfilled with a `string` containing the first data chunk.
93103

94-
The promise will reject if the stream emits an error – unless you're waiting for
95-
the "error" event, in which case it will resolve.
104+
The promise will be rejected with a `RuntimeException` if the stream emits an error
105+
– unless you're waiting for the "error" event, in which case it will be fulfilled.
96106

97-
The promise will reject once the stream closes – unless you're waiting for the
98-
"close" event, in which case it will resolve.
107+
The promise will be rejected with a `RuntimeException` once the stream closes
108+
– unless you're waiting for the "close" event, in which case it will be fulfilled.
99109

100-
The promise will reject if the stream is already closed.
110+
The promise will be rejected with a `RuntimeException` if the stream is already closed.
101111

102-
The promise will reject if it is cancelled.
112+
The promise will be rejected with a `RuntimeException` if it is cancelled.
103113

104114
### all()
105115

106-
The `all(ReadableStreamInterface|WritableStreamInterface $stream, string $event = 'data'): PromiseInterface<array,Exception>` function can be used to
107-
create a `Promise` which resolves with an array of all the event data.
116+
The `all(ReadableStreamInterface|WritableStreamInterface $stream, string $event = 'data'): PromiseInterface<array,RuntimeException>` function can be used to
117+
create a `Promise` which will be fulfilled with an array of all the event data.
108118

109119
```php
110120
$stream = accessSomeJsonStream();
111121

112-
Stream\all($stream)->then(function ($chunks) {
122+
React\Promise\Stream\all($stream)->then(function (array $chunks) {
113123
echo 'The stream consists of ' . count($chunks) . ' chunk(s)';
114124
});
115125
```
116126

117-
The promise will resolve with an array of whatever all events emitted or `null` if the
118-
events do not pass any data.
127+
The promise will be fulfilled with an `array` once the stream closes. The array
128+
will contain whatever all events emitted or `null` values if the events do not pass any data.
119129
If you do not pass a custom event name, then it will wait for all the "data"
120-
events and resolve with an array containing all the data chunks.
121-
122-
The promise will resolve with an array once the stream closes.
130+
events.
131+
For common streams of type `ReadableStreamInterface<string>`, this means it will be
132+
fulfilled with a `string[]` array containing all the data chunk.
123133

124-
The promise will resolve with an empty array if the stream is already closed.
134+
The promise will be fulfilled with an empty `array` if the stream is already closed.
125135

126-
The promise will reject if the stream emits an error.
136+
The promise will be rejected with a `RuntimeException` if the stream emits an error.
127137

128-
The promise will reject if it is cancelled.
138+
The promise will be rejected with a `RuntimeException` if it is cancelled.
129139

130140
### unwrapReadable()
131141

132-
The `unwrapReadable(PromiseInterface<ReadableStreamInterface,Exception> $promise): ReadableStreamInterface` function can be used to
133-
unwrap a `Promise` which resolves with a `ReadableStreamInterface`.
142+
The `unwrapReadable(PromiseInterface<ReadableStreamInterface<T>,Exception> $promise): ReadableStreamInterface<T>` function can be used to
143+
unwrap a `Promise` which will be fulfilled with a `ReadableStreamInterface<T>`.
134144

135-
This function returns a readable stream instance (implementing `ReadableStreamInterface`)
145+
This function returns a readable stream instance (implementing `ReadableStreamInterface<T>`)
136146
right away which acts as a proxy for the future promise resolution.
137-
Once the given Promise resolves with a `ReadableStreamInterface`, its data will
138-
be piped to the output stream.
147+
Once the given Promise will be fulfilled with a `ReadableStreamInterface<T>`, its
148+
data will be piped to the output stream.
139149

140150
```php
141151
//$promise = someFunctionWhichResolvesWithAStream();
142152
$promise = startDownloadStream($uri);
143153

144-
$stream = Stream\unwrapReadable($promise);
154+
$stream = React\Promise\Stream\unwrapReadable($promise);
145155

146-
$stream->on('data', function ($data) {
156+
$stream->on('data', function (string $data) {
147157
echo $data;
148158
});
149159

@@ -159,7 +169,7 @@ an `error` event and close:
159169
```php
160170
$promise = startDownloadStream($invalidUri);
161171

162-
$stream = Stream\unwrapReadable($promise);
172+
$stream = React\Promise\Stream\unwrapReadable($promise);
163173

164174
$stream->on('error', function (Exception $error) {
165175
echo 'Error: ' . $error->getMessage();
@@ -168,17 +178,16 @@ $stream->on('error', function (Exception $error) {
168178

169179
The given `$promise` SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected
170180
at the time of invoking this function.
171-
If the given promise is already settled and does not resolve with an
172-
instance of `ReadableStreamInterface`, then you will not be able to receive
173-
the `error` event.
181+
If the given promise is already settled and does not fulfill with an instance of
182+
`ReadableStreamInterface`, then you will not be able to receive the `error` event.
174183

175184
You can `close()` the resulting stream at any time, which will either try to
176185
`cancel()` the pending promise or try to `close()` the underlying stream.
177186

178187
```php
179188
$promise = startDownloadStream($uri);
180189

181-
$stream = Stream\unwrapReadable($promise);
190+
$stream = React\Promise\Stream\unwrapReadable($promise);
182191

183192
$loop->addTimer(2.0, function () use ($stream) {
184193
$stream->close();
@@ -187,20 +196,22 @@ $loop->addTimer(2.0, function () use ($stream) {
187196

188197
### unwrapWritable()
189198

190-
The `unwrapWritable(PromiseInterface<WritableStreamInterface,Exception> $promise): WritableStreamInterface` function can be used to
191-
unwrap a `Promise` which resolves with a `WritableStreamInterface`.
199+
The `unwrapWritable(PromiseInterface<WritableStreamInterface<T>,Exception> $promise): WritableStreamInterface<T>` function can be used to
200+
unwrap a `Promise` which will be fulfilled with a `WritableStreamInterface<T>`.
192201

193-
This function returns a writable stream instance (implementing `WritableStreamInterface`)
202+
This function returns a writable stream instance (implementing `WritableStreamInterface<T>`)
194203
right away which acts as a proxy for the future promise resolution.
195-
Any writes to this instance will be buffered in memory for when the promise resolves.
196-
Once the given Promise resolves with a `WritableStreamInterface`, any data you
197-
have written to the proxy will be forwarded transparently to the inner stream.
204+
Any writes to this instance will be buffered in memory for when the promise will
205+
be fulfilled.
206+
Once the given Promise will be fulfilled with a `WritableStreamInterface<T>`, any
207+
data you have written to the proxy will be forwarded transparently to the inner
208+
stream.
198209

199210
```php
200211
//$promise = someFunctionWhichResolvesWithAStream();
201212
$promise = startUploadStream($uri);
202213

203-
$stream = Stream\unwrapWritable($promise);
214+
$stream = React\Promise\Stream\unwrapWritable($promise);
204215

205216
$stream->write('hello');
206217
$stream->end('world');
@@ -217,7 +228,7 @@ an `error` event and close:
217228
```php
218229
$promise = startUploadStream($invalidUri);
219230

220-
$stream = Stream\unwrapWritable($promise);
231+
$stream = React\Promise\Stream\unwrapWritable($promise);
221232

222233
$stream->on('error', function (Exception $error) {
223234
echo 'Error: ' . $error->getMessage();
@@ -226,17 +237,16 @@ $stream->on('error', function (Exception $error) {
226237

227238
The given `$promise` SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected
228239
at the time of invoking this function.
229-
If the given promise is already settled and does not resolve with an
230-
instance of `WritableStreamInterface`, then you will not be able to receive
231-
the `error` event.
240+
If the given promise is already settled and does not fulfill with an instance of
241+
`WritableStreamInterface`, then you will not be able to receive the `error` event.
232242

233243
You can `close()` the resulting stream at any time, which will either try to
234244
`cancel()` the pending promise or try to `close()` the underlying stream.
235245

236246
```php
237247
$promise = startUploadStream($uri);
238248

239-
$stream = Stream\unwrapWritable($promise);
249+
$stream = React\Promise\Stream\unwrapWritable($promise);
240250

241251
$loop->addTimer(2.0, function () use ($stream) {
242252
$stream->close();
@@ -245,7 +255,7 @@ $loop->addTimer(2.0, function () use ($stream) {
245255

246256
## Install
247257

248-
The recommended way to install this library is [through Composer](https://getcomposer.org).
258+
The recommended way to install this library is [through Composer](https://getcomposer.org/).
249259
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
250260

251261
This project follows [SemVer](https://semver.org/).
@@ -260,12 +270,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
260270
This project aims to run on any platform and thus does not require any PHP
261271
extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
262272
HHVM.
263-
It's *highly recommended to use PHP 7+* for this project.
273+
It's *highly recommended to use the latest supported PHP version* for this project.
264274

265275
## Tests
266276

267277
To run the test suite, you first need to clone this repo and then install all
268-
dependencies [through Composer](https://getcomposer.org):
278+
dependencies [through Composer](https://getcomposer.org/):
269279

270280
```bash
271281
$ composer install
@@ -274,7 +284,7 @@ $ composer install
274284
To run the test suite, go to the project root and run:
275285

276286
```bash
277-
$ php vendor/bin/phpunit
287+
$ vendor/bin/phpunit
278288
```
279289

280290
## License

0 commit comments

Comments
 (0)