Skip to content

Commit bf6d87f

Browse files
author
ace411
committed
docs: update README.md
1 parent 722677d commit bf6d87f

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

README.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ use ringphp\MrLoop;
153153
$loop = Mrloop::init();
154154

155155
$loop->addReadStream(
156-
\fopen('/path/to/file', 'r'),
156+
$fd = \fopen('/path/to/file', 'r'),
157157
null,
158-
function (string $contents, int $res) {
158+
function (string $contents, int $res) use ($fd) {
159159
if ($res === 0) {
160160
echo \sprintf("%s\n", $contents);
161+
162+
\fclose($fd);
161163
}
162164
},
163165
);
@@ -203,10 +205,12 @@ $loop = MrLoop::init();
203205
$file = '/path/to/file';
204206

205207
$loop->addWriteStream(
206-
\fopen($file, 'w'),
208+
$fd = \fopen($file, 'w'),
207209
"file contents...\n",
208-
function (int $nbytes) use ($file) {
210+
function (int $nbytes) use ($fd, $file) {
209211
echo \sprintf("Wrote %d bytes to %s\n", $nbytes, $file);
212+
213+
\fclose($fd);
210214
},
211215
);
212216

@@ -392,8 +396,10 @@ $loop->addWriteStream(
392396
$loop->addReadStream(
393397
$sock,
394398
null,
395-
function ($data, $res) use ($loop) {
399+
function ($data, $res) use ($sock, $loop) {
396400
var_dump(Mrloop::parseHttpResponse($data));
401+
402+
\fclose($sock);
397403
},
398404
);
399405
},
@@ -542,6 +548,7 @@ Executes a specified action in perpetuity with each successive execution occurri
542548

543549
- **interval** (float) - The interval (in seconds) between successive executions of a specified action.
544550
- **callback** (callable) - The function in which the specified action due for periodical execution is defined.
551+
> A return value of `0` will cancel the timer.
545552
546553
**Return value(s)**
547554

@@ -559,7 +566,7 @@ $loop->addPeriodicTimer(
559566
echo \sprintf("Tick: %d\n", ++$tick);
560567

561568
if ($tick === 5) {
562-
$loop->stop();
569+
$loop->stop(); // return 0;
563570
}
564571
},
565572
);
@@ -600,12 +607,15 @@ use ringphp\Mrloop;
600607

601608
$loop = Mrloop::init();
602609

603-
$loop->freadv(
604-
'/path/to/file',
605-
null,
610+
$loop->addReadStream(
611+
$fd = \fopen('/path/to/file', 'r'),
606612
null,
607-
function (string $contents) {
613+
function (...$args) use ($fd) {
614+
[$contents] = $args;
615+
608616
echo \sprintf("%s\n", $contents);
617+
618+
\fclose($fd);
609619
},
610620
);
611621

@@ -670,11 +680,13 @@ use ringphp\Mrloop;
670680
$loop = Mrloop::init();
671681

672682
$loop->addReadStream(
673-
\fopen('/path/to/file', 'r'),
683+
$fd = \fopen('/path/to/file', 'r'),
674684
'File contents...',
675-
function ($contents, $res) use ($loop) {
685+
function ($contents, $res) use ($fd, $loop) {
676686
echo \sprintf("%s\n", $contents);
677687

688+
\fclose($fd);
689+
678690
$loop->stop();
679691
},
680692
);

0 commit comments

Comments
 (0)