Skip to content

Commit e0d7c07

Browse files
authored
Merge pull request #126 from clue-labs/docs
Minor documentation improvements
2 parents e96eac4 + d431fa7 commit e0d7c07

7 files changed

Lines changed: 79 additions & 53 deletions

File tree

README.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Here is an async HTTP server built with just the event loop.
3232
$loop = React\EventLoop\Factory::create();
3333

3434
$server = stream_socket_server('tcp://127.0.0.1:8080');
35-
stream_set_blocking($server, 0);
35+
stream_set_blocking($server, false);
3636

3737
$loop->addReadStream($server, function ($server) use ($loop) {
3838
$conn = stream_socket_accept($server);
@@ -97,7 +97,7 @@ $loop->run();
9797
## Factory
9898

9999
The `Factory` class exists as a convenient way to pick the best available
100-
[loop implementation)(#loop-implementation).
100+
[loop implementation](#loop-implementations).
101101

102102
The `create(): LoopInterface` method can be used to create a new loop
103103
instance:
@@ -170,14 +170,14 @@ If you want to access any variables within your callback function, you
170170
can bind arbitrary data to a callback closure like this:
171171

172172
```php
173-
function hello(LoopInterface $loop, $name)
173+
function hello($name, LoopInterface $loop)
174174
{
175175
$loop->addTimer(1.0, function () use ($name) {
176176
echo "hello $name\n";
177177
});
178178
}
179179

180-
hello('Tester');
180+
hello('Tester', $loop);
181181
```
182182

183183
The execution order of timers scheduled to execute at the same time is
@@ -218,7 +218,7 @@ If you want to limit the number of executions, you can bind
218218
arbitrary data to a callback closure like this:
219219

220220
```php
221-
function hello(LoopInterface $loop, $name)
221+
function hello($name, LoopInterface $loop)
222222
{
223223
$n = 3;
224224
$loop->addPeriodicTimer(1.0, function ($timer) use ($name, $loop, &$n) {
@@ -231,7 +231,7 @@ function hello(LoopInterface $loop, $name)
231231
});
232232
}
233233

234-
hello('Tester');
234+
hello('Tester', $loop);
235235
```
236236

237237
The execution order of timers scheduled to execute at the same time is
@@ -245,11 +245,12 @@ cancel a pending timer.
245245
See also [`addPeriodicTimer()`](#addperiodictimer) and [example #2](examples).
246246

247247
You can use the [`isTimerActive()`](#istimeractive) method to check if
248-
this timer is still "active". After a timer is successfully canceled,
248+
this timer is still "active". After a timer is successfully cancelled,
249249
it is no longer considered "active".
250250

251251
Calling this method on a timer instance that has not been added to this
252-
loop instance or on a timer
252+
loop instance or on a timer that is not "active" (or has already been
253+
cancelled) has no effect.
253254

254255
### isTimerActive()
255256

@@ -258,7 +259,7 @@ check if a given timer is active.
258259

259260
A timer is considered "active" if it has been added to this loop instance
260261
via [`addTimer()`](#addtimer) or [`addPeriodicTimer()`](#addperiodictimer)
261-
and has not been canceled via [`cancelTimer()`](#canceltimer) and is not
262+
and has not been cancelled via [`cancelTimer()`](#canceltimer) and is not
262263
a non-periodic timer that has already been triggered after its interval.
263264

264265
### futureTick()
@@ -280,14 +281,14 @@ If you want to access any variables within your callback function, you
280281
can bind arbitrary data to a callback closure like this:
281282

282283
```php
283-
function hello(LoopInterface $loop, $name)
284+
function hello($name, LoopInterface $loop)
284285
{
285286
$loop->futureTick(function () use ($name) {
286287
echo "hello $name\n";
287288
});
288289
}
289290

290-
hello('Tester');
291+
hello('Tester', $loop);
291292
```
292293

293294
Unlike timers, tick callbacks are guaranteed to be executed in the order
@@ -312,8 +313,10 @@ See also [example #3](examples).
312313
### addSignal()
313314

314315
The `addSignal(int $signal, callable $listener): void` method can be used to
315-
be notified about OS signals. This is useful to catch user interrupt signals or
316-
shutdown signals from tools like `supervisor` or `systemd`.
316+
register a listener to be notified when a signal has been caught by this process.
317+
318+
This is useful to catch user interrupt signals or shutdown signals from
319+
tools like `supervisor` or `systemd`.
317320

318321
The listener callback function MUST be able to accept a single parameter,
319322
the signal added by this method or you MAY use a function which
@@ -325,32 +328,32 @@ no effect, so for performance reasons you're recommended to not return
325328
any excessive data structures.
326329

327330
```php
328-
$listener = function (int $signal) {
329-
echo 'Caught user iterrupt signal', PHP_EOL;
330-
};
331-
$loop->addSignal(SIGINT, $listener);
331+
$loop->addSignal(SIGINT, function (int $signal) {
332+
echo 'Caught user interrupt signal' . PHP_EOL;
333+
});
332334
```
333335

334336
See also [example #4](examples).
335337

336-
**Note: A listener can only be added once to the same signal, any attempts to add it
337-
more then once will be ignored.**
338+
Signaling is only available on Unix-like platform, Windows isn't
339+
supported due to operating system limitations.
340+
This method may throw a `BadMethodCallException` if signals aren't
341+
supported on this platform, for example when required extensions are
342+
missing.
338343

339-
**Note: Signaling is only available on Unix-like platform, Windows isn't supported due
340-
to limitations from underlying signal handlers.**
344+
**Note: A listener can only be added once to the same signal, any
345+
attempts to add it more then once will be ignored.**
341346

342347
### removeSignal()
343348

344-
The `removeSignal(int $signal, callable $listener): void` removes a previously added
345-
signal listener.
346-
347-
Any attempts to remove listeners that aren't registerred will be ignored.
349+
The `removeSignal(int $signal, callable $listener): void` method can be used to
350+
remove a previously added signal listener.
348351

349352
```php
350353
$loop->removeSignal(SIGINT, $listener);
351354
```
352355

353-
See also [example #4](examples).
356+
Any attempts to remove listeners that aren't registered will be ignored.
354357

355358
### addReadStream()
356359

src/ExtEventLoop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function ($signal) {
4242
$this->signals->call($signal);
4343
// Ensure there are two copies of the callable around until it has been executed.
4444
// For more information see: https://bugs.php.net/bug.php?id=62452
45-
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
45+
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
4646
$g = $f;
4747
$f = $g;
4848
});

src/LibEvLoop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function ($signal) {
3939
$this->signals->call($signal);
4040
// Ensure there are two copies of the callable around until it has been executed.
4141
// For more information see: https://bugs.php.net/bug.php?id=62452
42-
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
42+
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
4343
$g = $f;
4444
$f = $g;
4545
}, $signal);

src/LibEventLoop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function ($signal) {
4343
$this->signals->call($signal);
4444
// Ensure there are two copies of the callable around until it has been executed.
4545
// For more information see: https://bugs.php.net/bug.php?id=62452
46-
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
46+
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
4747
$g = $f;
4848
$f = $g;
4949
});

src/LoopInterface.php

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ public function removeWriteStream($stream);
152152
* can bind arbitrary data to a callback closure like this:
153153
*
154154
* ```php
155-
* function hello(LoopInterface $loop, $name)
155+
* function hello($name, LoopInterface $loop)
156156
* {
157157
* $loop->addTimer(1.0, function () use ($name) {
158158
* echo "hello $name\n";
159159
* });
160160
* }
161161
*
162-
* hello('Tester');
162+
* hello('Tester', $loop);
163163
* ```
164164
*
165165
* The execution order of timers scheduled to execute at the same time is
@@ -205,7 +205,7 @@ public function addTimer($interval, callable $callback);
205205
* arbitrary data to a callback closure like this:
206206
*
207207
* ```php
208-
* function hello(LoopInterface $loop, $name)
208+
* function hello($name, LoopInterface $loop)
209209
* {
210210
* $n = 3;
211211
* $loop->addPeriodicTimer(1.0, function ($timer) use ($name, $loop, &$n) {
@@ -218,7 +218,7 @@ public function addTimer($interval, callable $callback);
218218
* });
219219
* }
220220
*
221-
* hello('Tester');
221+
* hello('Tester', $loop);
222222
* ```
223223
*
224224
* The execution order of timers scheduled to execute at the same time is
@@ -237,12 +237,12 @@ public function addPeriodicTimer($interval, callable $callback);
237237
* See also [`addPeriodicTimer()`](#addperiodictimer) and [example #2](examples).
238238
*
239239
* You can use the [`isTimerActive()`](#istimeractive) method to check if
240-
* this timer is still "active". After a timer is successfully canceled,
240+
* this timer is still "active". After a timer is successfully cancelled,
241241
* it is no longer considered "active".
242242
*
243243
* Calling this method on a timer instance that has not been added to this
244244
* loop instance or on a timer that is not "active" (or has already been
245-
* canceled) has no effect.
245+
* cancelled) has no effect.
246246
*
247247
* @param TimerInterface $timer The timer to cancel.
248248
*
@@ -255,7 +255,7 @@ public function cancelTimer(TimerInterface $timer);
255255
*
256256
* A timer is considered "active" if it has been added to this loop instance
257257
* via [`addTimer()`](#addtimer) or [`addPeriodicTimer()`](#addperiodictimer)
258-
* and has not been canceled via [`cancelTimer()`](#canceltimer) and is not
258+
* and has not been cancelled via [`cancelTimer()`](#canceltimer) and is not
259259
* a non-periodic timer that has already been triggered after its interval.
260260
*
261261
* @param TimerInterface $timer The timer to check.
@@ -281,14 +281,14 @@ public function isTimerActive(TimerInterface $timer);
281281
* can bind arbitrary data to a callback closure like this:
282282
*
283283
* ```php
284-
* function hello(LoopInterface $loop, $name)
284+
* function hello($name, LoopInterface $loop)
285285
* {
286286
* $loop->futureTick(function () use ($name) {
287287
* echo "hello $name\n";
288288
* });
289289
* }
290290
*
291-
* hello('Tester');
291+
* hello('Tester', $loop);
292292
* ```
293293
*
294294
* Unlike timers, tick callbacks are guaranteed to be executed in the order
@@ -317,32 +317,55 @@ public function isTimerActive(TimerInterface $timer);
317317
public function futureTick(callable $listener);
318318

319319
/**
320-
* Registers a signal listener with the loop, which
321-
* on it's turn registers it with a signal handler
322-
* suitable for the loop implementation.
320+
* Register a listener to be notified when a signal has been caught by this process.
323321
*
324-
* A listener can only be added once, any attempts
325-
* to add it again will be ignored.
322+
* This is useful to catch user interrupt signals or shutdown signals from
323+
* tools like `supervisor` or `systemd`.
324+
*
325+
* The listener callback function MUST be able to accept a single parameter,
326+
* the signal added by this method or you MAY use a function which
327+
* has no parameters at all.
328+
*
329+
* The listener callback function MUST NOT throw an `Exception`.
330+
* The return value of the listener callback function will be ignored and has
331+
* no effect, so for performance reasons you're recommended to not return
332+
* any excessive data structures.
333+
*
334+
* ```php
335+
* $loop->addSignal(SIGINT, function (int $signal) {
336+
* echo 'Caught user interrupt signal' . PHP_EOL;
337+
* });
338+
* ```
326339
*
327340
* See also [example #4](examples).
328341
*
342+
* Signaling is only available on Unix-like platform, Windows isn't
343+
* supported due to operating system limitations.
344+
* This method may throw a `BadMethodCallException` if signals aren't
345+
* supported on this platform, for example when required extensions are
346+
* missing.
347+
*
348+
* **Note: A listener can only be added once to the same signal, any
349+
* attempts to add it more then once will be ignored.**
350+
*
329351
* @param int $signal
330352
* @param callable $listener
331353
*
332-
* @throws \BadMethodCallException when signals
333-
* aren't supported by the loop, e.g. when required
334-
* extensions are missing.
354+
* @throws \BadMethodCallException when signals aren't supported on this
355+
* platform, for example when required extensions are missing.
335356
*
336357
* @return void
337358
*/
338359
public function addSignal($signal, callable $listener);
339360

340361
/**
341-
* Removed previous registered signal listener from
342-
* the loop, which on it's turn removes it from the
343-
* underlying signal handler.
362+
* Removes a previously added signal listener.
344363
*
345-
* See also [example #4](examples).
364+
* ```php
365+
* $loop->removeSignal(SIGINT, $listener);
366+
* ```
367+
*
368+
* Any attempts to remove listeners that aren't registered will be ignored.
346369
*
347370
* @param int $signal
348371
* @param callable $listener

src/StreamSelectLoop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function ($signal) {
3737
$this->signals->call($signal);
3838
// Ensure there are two copies of the callable around until it has been executed.
3939
// For more information see: https://bugs.php.net/bug.php?id=62452
40-
// Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
40+
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
4141
$g = $f;
4242
$f = $g;
4343
});

tests/AbstractLoopTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function testAddReadStreamReceivesDataFromStreamReference()
7272
}
7373

7474
/**
75-
* Telper for above test. This happens in another helper method to verify
76-
* the loop keep track of assigned stream resources (refcount).
75+
* Helper for above test. This happens in another helper method to verify
76+
* the loop keeps track of assigned stream resources (refcount).
7777
*/
7878
private function subAddReadStreamReceivesDataFromStreamReference()
7979
{

0 commit comments

Comments
 (0)