Skip to content

Commit 768554c

Browse files
committed
Changed namespace and added README.md
1 parent 564590a commit 768554c

File tree

6 files changed

+47
-24
lines changed

6 files changed

+47
-24
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
React Loop Interface For Async Interop Loop
2+
======
3+
4+
This library allows you use any [async-interop](https://packagist.org/providers/async-interop/event-loop-implementation) loop with any library that requires the [ReactPHP](https://github.com/reactphp/event-loop) loop.
5+
6+
note: When all ReactPHP projects support the interop loop, this library will no longer be necessary.
7+
8+
9+
## Example
10+
11+
```php
12+
//Create your async-interop loop
13+
14+
$loop = new \Voryx\React\AsyncInterop\Loop();
15+
16+
//Then use your ReactPHP loop like you normally would
17+
18+
$client = stream_socket_client('tcp://127.0.0.1:1337');
19+
$conn = new React\Stream\Stream($client, $loop);
20+
$conn->pipe(new React\Stream\Stream(STDOUT, $loop));
21+
$conn->write("Hello World!\n");
22+
23+
$loop->run();
24+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"autoload": {
2525
"psr-4": {
26-
"Voryx\\React\\EventLoop\\": "src/"
26+
"Voryx\\React\\AsyncInterop\\": "src/"
2727
}
2828
}
2929
}

src/ReactAsyncInteropLoop.php renamed to src/Loop.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace Voryx\React\EventLoop;
3+
namespace Voryx\React\AsyncInterop;
44

5-
use Interop\Async\Loop;
5+
use Interop\Async\Loop as InteropLoop;
66
use React\EventLoop\LoopInterface;
77
use React\EventLoop\Timer\TimerInterface;
88

9-
class ReactAsyncInteropLoop implements LoopInterface
9+
final class Loop implements LoopInterface
1010
{
1111
private $readStreams = [];
1212
private $writeStreams = [];
@@ -17,7 +17,7 @@ public function addReadStream($stream, callable $listener)
1717
if (isset($this->readStreams[$key])) {
1818
throw new \Exception('key set twice');
1919
}
20-
$this->readStreams[$key] = Loop::get()->onReadable($stream, function () use ($listener, $stream) {
20+
$this->readStreams[$key] = InteropLoop::get()->onReadable($stream, function () use ($listener, $stream) {
2121
$listener($stream);
2222
});
2323
}
@@ -30,7 +30,7 @@ public function addWriteStream($stream, callable $listener)
3030
throw new \Exception('key set twice');
3131
}
3232

33-
$this->writeStreams[$key] = Loop::get()->onWritable($stream, function () use ($listener, $stream) {
33+
$this->writeStreams[$key] = InteropLoop::get()->onWritable($stream, function () use ($listener, $stream) {
3434
$listener($stream);
3535
});
3636
}
@@ -39,7 +39,7 @@ public function removeReadStream($stream)
3939
{
4040
$key = (int)$stream;
4141
if (isset($this->readStreams[$key])) {
42-
Loop::get()->cancel($this->readStreams[$key]);
42+
InteropLoop::get()->cancel($this->readStreams[$key]);
4343
unset($this->readStreams[$key]);
4444
}
4545
}
@@ -48,7 +48,7 @@ public function removeWriteStream($stream)
4848
{
4949
$key = (int)$stream;
5050
if (isset($this->writeStreams[$key])) {
51-
Loop::get()->cancel($this->writeStreams[$key]);
51+
InteropLoop::get()->cancel($this->writeStreams[$key]);
5252
unset($this->writeStreams[$key]);
5353
}
5454
}
@@ -66,11 +66,11 @@ private function addWrappedTimer($interval, callable $callback, $isPeriodic = fa
6666
};
6767
$millis = $interval * 1000;
6868
if ($isPeriodic) {
69-
$timerKey = Loop::get()->repeat($millis, $wrappedCallback);
69+
$timerKey = InteropLoop::get()->repeat($millis, $wrappedCallback);
7070
} else {
71-
$timerKey = Loop::get()->delay($millis, $wrappedCallback);
71+
$timerKey = InteropLoop::get()->delay($millis, $wrappedCallback);
7272
}
73-
$timer = new ReactAsyncInteropTimer(
73+
$timer = new Timer(
7474
$timerKey,
7575
$interval,
7676
$callback,
@@ -102,7 +102,7 @@ public function isTimerActive(TimerInterface $timer)
102102

103103
public function nextTick(callable $listener)
104104
{
105-
Loop::get()->defer(function () use ($listener) {
105+
InteropLoop::get()->defer(function () use ($listener) {
106106
$listener($this);
107107
});
108108
}
@@ -114,7 +114,7 @@ public function futureTick(callable $listener)
114114

115115
public function tick()
116116
{
117-
$loop = Loop::get();
117+
$loop = InteropLoop::get();
118118

119119
$loop->defer(function () use ($loop) {
120120
$loop->stop();
@@ -125,11 +125,11 @@ public function tick()
125125

126126
public function run()
127127
{
128-
Loop::get()->run();
128+
InteropLoop::get()->run();
129129
}
130130

131131
public function stop()
132132
{
133-
Loop::get()->stop();
133+
InteropLoop::get()->stop();
134134
}
135135
}

src/ReactAsyncInteropTimer.php renamed to src/Timer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace Voryx\React\EventLoop;
3+
namespace Voryx\React\AsyncInterop;
44

55
use Interop\Async\Loop;
66
use React\EventLoop\LoopInterface;
7-
use React\EventLoop\Timer\Timer;
7+
use React\EventLoop\Timer\Timer as ReactTimer;
88

9-
class ReactAsyncInteropTimer extends Timer
9+
final class Timer extends ReactTimer
1010
{
1111
private $timerKey;
1212

tests/ReactAsyncInteropLoopTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Interop\Async\Loop;
66
use React\EventLoop\StreamSelectLoop;
77
use React\Tests\EventLoop\AbstractLoopTest;
8-
use Voryx\React\EventLoop\ReactAsyncInteropLoop;
98
use WyriHaximus\React\AsyncInteropLoop\ReactDriverFactory;
109

1110
class ReactAsyncInteropLoopTest extends AbstractLoopTest
@@ -16,7 +15,7 @@ public function createLoop()
1615
$driver = ReactDriverFactory::createFactoryFromLoop(StreamSelectLoop::class);
1716
Loop::setFactory($driver);
1817

19-
return new ReactAsyncInteropLoop();
18+
return new \Voryx\React\AsyncInterop\Loop();
2019
}
2120

2221
public function testRecursiveNextTick()

tests/ReactAsyncInteropLoopWithAmpTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
namespace Voryx\Tests\React\EventLoop;
44

55
use Amp\Loop\LoopFactory;
6-
use Interop\Async\Loop;
76
use React\Tests\EventLoop\AbstractLoopTest;
8-
use Voryx\React\EventLoop\ReactAsyncInteropLoop;
7+
use Voryx\React\AsyncInterop\Loop;
8+
99

1010
class ReactAsyncInteropLoopWithAmpTest extends AbstractLoopTest
1111
{
1212
public function createLoop()
1313
{
14-
Loop::setFactory(new LoopFactory());
15-
return new ReactAsyncInteropLoop();
14+
\Interop\Async\Loop::setFactory(new LoopFactory());
15+
return new Loop();
1616
}
1717

1818
public function testRecursiveNextTick()

0 commit comments

Comments
 (0)