Skip to content

Commit 7c7aae9

Browse files
committed
Documentation for stream API
1 parent ed4b051 commit 7c7aae9

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,83 @@ echo 'a';
291291

292292
See also [example #3](examples).
293293

294+
### addReadStream()
295+
296+
The `addReadStream(resource $stream, callable $callback): void` method can be used to
297+
register a listener to be notified when a stream is ready to read.
298+
299+
The listener callback function MUST be able to accept a single parameter,
300+
the stream resource added by this method or you MAY use a function which
301+
has no parameters at all.
302+
303+
The listener callback function MUST NOT throw an `Exception`.
304+
The return value of the listener callback function will be ignored and has
305+
no effect, so for performance reasons you're recommended to not return
306+
any excessive data structures.
307+
308+
If you want to access any variables within your callback function, you
309+
can bind arbitrary data to a callback closure like this:
310+
311+
```php
312+
$loop->addReadStream($stream, function ($stream) use ($name) {
313+
echo $name . ' said: ' . fread($stream);
314+
});
315+
```
316+
317+
See also [example #11](examples).
318+
319+
You can invoke [`removeReadStream()`](#removereadstream) to remove the
320+
read event listener for this stream.
321+
322+
The execution order of listeners when multiple streams become ready at
323+
the same time is not guaranteed.
324+
325+
### addWriteStream()
326+
327+
The `addWriteStream(resource $stream, callable $callback): void` method can be used to
328+
register a listener to be notified when a stream is ready to write.
329+
330+
The listener callback function MUST be able to accept a single parameter,
331+
the stream resource added by this method or you MAY use a function which
332+
has no parameters at all.
333+
334+
The listener callback function MUST NOT throw an `Exception`.
335+
The return value of the listener callback function will be ignored and has
336+
no effect, so for performance reasons you're recommended to not return
337+
any excessive data structures.
338+
339+
If you want to access any variables within your callback function, you
340+
can bind arbitrary data to a callback closure like this:
341+
342+
```php
343+
$loop->addWriteStream($stream, function ($stream) use ($name) {
344+
fwrite($stream, 'Hello ' . $name);
345+
});
346+
```
347+
348+
See also [example #12](examples).
349+
350+
You can invoke [`removeWriteStream()`](#removewritestream) to remove the
351+
write event listener for this stream.
352+
353+
The execution order of listeners when multiple streams become ready at
354+
the same time is not guaranteed.
355+
356+
### removeReadStream()
357+
358+
The `removeReadStream(resource $stream): void` method can be used to
359+
remove the read event listener for the given stream.
360+
361+
### removeWriteStream()
362+
363+
The `removeWriteStream(resource $stream): void` method can be used to
364+
remove the write event listener for the given stream.
365+
366+
### removeStream()
367+
368+
The `removeStream(resource $stream): void` method can be used to
369+
remove all listeners for the given stream.
370+
294371
## Install
295372

296373
The recommended way to install this library is [through Composer](http://getcomposer.org).

src/LoopInterface.php

+52
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@ interface LoopInterface
99
/**
1010
* Register a listener to be notified when a stream is ready to read.
1111
*
12+
* The listener callback function MUST be able to accept a single parameter,
13+
* the stream resource added by this method or you MAY use a function which
14+
* has no parameters at all.
15+
*
16+
* The listener callback function MUST NOT throw an `Exception`.
17+
* The return value of the listener callback function will be ignored and has
18+
* no effect, so for performance reasons you're recommended to not return
19+
* any excessive data structures.
20+
*
21+
* If you want to access any variables within your callback function, you
22+
* can bind arbitrary data to a callback closure like this:
23+
*
24+
* ```php
25+
* $loop->addReadStream($stream, function ($stream) use ($name) {
26+
* echo $name . ' said: ' . fread($stream);
27+
* });
28+
* ```
29+
*
30+
* See also [example #11](examples).
31+
*
32+
* You can invoke [`removeReadStream()`](#removereadstream) to remove the
33+
* read event listener for this stream.
34+
*
35+
* The execution order of listeners when multiple streams become ready at
36+
* the same time is not guaranteed.
37+
*
1238
* @param resource $stream The PHP stream resource to check.
1339
* @param callable $listener Invoked when the stream is ready.
1440
*/
@@ -17,6 +43,32 @@ public function addReadStream($stream, callable $listener);
1743
/**
1844
* Register a listener to be notified when a stream is ready to write.
1945
*
46+
* The listener callback function MUST be able to accept a single parameter,
47+
* the stream resource added by this method or you MAY use a function which
48+
* has no parameters at all.
49+
*
50+
* The listener callback function MUST NOT throw an `Exception`.
51+
* The return value of the listener callback function will be ignored and has
52+
* no effect, so for performance reasons you're recommended to not return
53+
* any excessive data structures.
54+
*
55+
* If you want to access any variables within your callback function, you
56+
* can bind arbitrary data to a callback closure like this:
57+
*
58+
* ```php
59+
* $loop->addWriteStream($stream, function ($stream) use ($name) {
60+
* fwrite($stream, 'Hello ' . $name);
61+
* });
62+
* ```
63+
*
64+
* See also [example #12](examples).
65+
*
66+
* You can invoke [`removeWriteStream()`](#removewritestream) to remove the
67+
* write event listener for this stream.
68+
*
69+
* The execution order of listeners when multiple streams become ready at
70+
* the same time is not guaranteed.
71+
*
2072
* @param resource $stream The PHP stream resource to check.
2173
* @param callable $listener Invoked when the stream is ready.
2274
*/

0 commit comments

Comments
 (0)