Skip to content

Commit 39ba3e9

Browse files
committed
Simplify usage by supporting new default loop
1 parent dd1c1d7 commit 39ba3e9

File tree

9 files changed

+36
-30
lines changed

9 files changed

+36
-30
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,14 @@ Once [installed](#install), you can use the following code to create a simple
4141
echo server that listens for incoming multicast messages:
4242

4343
```php
44-
$loop = React\EventLoop\Factory::create();
45-
$factory = new Factory($loop);
44+
$factory = new Factory();
4645
$socket = $factory->createReceiver('224.10.20.30:4050');
4746

4847
$socket->on('message', function ($data, $remote) use ($socket) {
4948
echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL;
5049
$socket->send($data, $remote);
5150
});
5251

53-
$loop->run();
5452
```
5553

5654
See also the [examples](examples).
@@ -60,13 +58,17 @@ See also the [examples](examples).
6058
### Factory
6159

6260
The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances.
63-
It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
6461

6562
```php
66-
$loop = React\EventLoop\Factory::create();
67-
$factory = new Factory($loop);
63+
$factory = new Factory();
6864
```
6965

66+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
67+
pass the event loop instance to use for this object. You can use a `null` value
68+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
69+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
70+
given event loop instance.
71+
7072
#### createSender()
7173

7274
The `createSender(): SocketInterface` method can be used to

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"require": {
2020
"php": ">=5.3",
21-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
21+
"react/event-loop": "^1.2",
2222
"react/datagram": "~1.0"
2323
},
2424
"require-dev": {

examples/dump-received.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
$address = $argv[1];
1919
}
2020

21-
$loop = React\EventLoop\Factory::create();
22-
$factory = new Factory($loop);
21+
$factory = new Factory();
2322
$socket = $factory->createReceiver($address);
2423
$hex = new Hexdump();
2524

@@ -28,4 +27,3 @@
2827
echo $hex->dump($data) . PHP_EOL;
2928
});
3029

31-
$loop->run();

examples/echo-received.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
$address = $argv[1];
1818
}
1919

20-
$loop = React\EventLoop\Factory::create();
21-
$factory = new Factory($loop);
20+
$factory = new Factory();
2221
$socket = $factory->createReceiver($address);
2322

2423
$socket->on('message', function ($data, $remote) use ($socket) {
2524
echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL;
2625
$socket->send($data, $remote);
2726
});
28-
29-
$loop->run();

examples/send-once.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
$address = isset($argv[1]) ? $argv[1] : '224.10.20.30:12345';
1313

14-
$loop = React\EventLoop\Factory::create();
15-
$factory = new Factory($loop);
14+
$factory = new Factory();
1615
$sender = $factory->createSender();
1716

1817
// do not wait for incoming messages
@@ -21,5 +20,3 @@
2120
// send a simple message
2221
$message = 'ping 123';
2322
$sender->send($message, $address);
24-
25-
$loop->run();

examples/send-wait.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212

1313
$address = isset($argv[1]) ? $argv[1] : '224.10.20.30:12345';
1414

15-
$loop = React\EventLoop\Factory::create();
16-
$factory = new Factory($loop);
15+
$factory = new Factory();
1716
$sender = $factory->createSender();
1817
$hex = new Hexdump();
1918

@@ -26,5 +25,3 @@
2625
// send a simple message
2726
$message = 'ping 123';
2827
$sender->send($message, $address);
29-
30-
$loop->run();

examples/ssdp.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
$address = '239.255.255.250:1900';
1111

12-
$loop = React\EventLoop\Factory::create();
13-
$factory = new Factory($loop);
12+
$factory = new Factory();
1413
$sender = $factory->createSender();
1514

1615
// dump all incoming messages
@@ -20,7 +19,7 @@
2019
});
2120

2221
// stop waiting for incoming messages after 3.0s (MX is 2s)
23-
$loop->addTimer(3.0, function () use ($sender) {
22+
Loop::addTimer(3.0, function () use ($sender) {
2423
$sender->pause();
2524
});
2625

@@ -32,5 +31,3 @@
3231
$data .= "ST: ssdp:all\r\n";
3332
$data .= "\r\n";
3433
$sender->send($data, $address);
35-
36-
$loop->run();

src/Factory.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22

33
namespace Clue\React\Multicast;
44

5+
use React\EventLoop\Loop;
56
use React\EventLoop\LoopInterface;
67
use React\Datagram\Socket as DatagramSocket;
78
use BadMethodCallException;
89
use RuntimeException;
910

1011
class Factory
1112
{
13+
/** @var LoopInterface */
1214
private $loop;
1315

1416
/**
1517
* The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances.
16-
* It also registers everything with the main [`EventLoop`](https://github.com/reactphp/event-loop#usage).
18+
*
19+
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
20+
* pass the event loop instance to use for this object. You can use a `null` value
21+
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
22+
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
23+
* given event loop instance.
1724
*
1825
* ```php
1926
* $loop = React\EventLoop\Factory::create();
@@ -22,9 +29,9 @@ class Factory
2229
*
2330
* @param LoopInterface $loop
2431
*/
25-
public function __construct(LoopInterface $loop)
32+
public function __construct(LoopInterface $loop = null)
2633
{
27-
$this->loop = $loop;
34+
$this->loop = $loop ?: Loop::get();
2835
}
2936

3037
/**

tests/FunctionalTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,15 @@ public function testMultipleReceivers()
5757

5858
$this->loop->run();
5959
}
60+
61+
public function testConstructWithoutLoopAssignsLoopAutomatically()
62+
{
63+
$factory = new Factory();
64+
65+
$ref = new \ReflectionProperty($factory, 'loop');
66+
$ref->setAccessible(true);
67+
$loop = $ref->getValue($factory);
68+
69+
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
70+
}
6071
}

0 commit comments

Comments
 (0)