Skip to content

Commit 8d50f2c

Browse files
committed
Simplify and restructure examples
1 parent 9fe6d4d commit 8d50f2c

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

examples/01-periodic.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Clue\React\Stdio\Stdio;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = React\EventLoop\Factory::create();
8+
9+
$stdio = new Stdio($loop);
10+
11+
$stdio->writeln('Will print periodic messages until you submit anything');
12+
13+
// add some periodic noise
14+
$timer = $loop->addPeriodicTimer(0.5, function () use ($stdio) {
15+
$stdio->writeln(date('Y-m-d H:i:s') . ' hello');
16+
});
17+
18+
// react to commands the user entered
19+
$stdio->on('line', function ($line) use ($stdio, $timer) {
20+
$stdio->writeln('you just said: ' . $line . ' (' . strlen($line) . ')');
21+
22+
$timer->cancel();
23+
$stdio->end();
24+
});
25+
26+
$loop->run();

examples/periodic.php renamed to examples/02-interactive.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,15 @@
3636
return $offset > 1 ? array() : array('exit', 'quit', 'help', 'echo', 'print', 'printf');
3737
});
3838

39-
$stdio->writeln('Will print periodic messages until you type "quit" or "exit"');
39+
$stdio->writeln('Welcome to this interactive demo');
4040

41-
$stdio->on('line', function ($line) use ($stdio, $loop, &$timer) {
41+
// react to commands the user entered
42+
$stdio->on('line', function ($line) use ($stdio) {
4243
$stdio->writeln('you just said: ' . $line . ' (' . strlen($line) . ')');
4344

4445
if (in_array(trim($line), array('quit', 'exit'))) {
45-
$timer->cancel();
4646
$stdio->end();
4747
}
4848
});
4949

50-
// add some periodic noise
51-
$timer = $loop->addPeriodicTimer(2.0, function () use ($stdio) {
52-
$stdio->writeln('hello');
53-
});
54-
5550
$loop->run();
File renamed without changes.

0 commit comments

Comments
 (0)