Skip to content

Commit efd5b79

Browse files
authored
Add compatibility with Revolt (#15)
1 parent dd32e92 commit efd5b79

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
![Stable](https://img.shields.io/badge/stability-stable-green.svg?style=flat-square)
66
![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)
77

8-
`amphp/react-adapter` makes any [ReactPHP](https://reactphp.org/) library compatible with [Amp](https://github.com/amphp/amp).
8+
`amphp/react-adapter` makes any [ReactPHP](https://reactphp.org/) library compatible with [Revolt's event loop](https://revolt.run) and v3 of [Amp](https://github.com/amphp/amp).
99

1010
## Installation
1111

@@ -15,17 +15,17 @@ composer require amphp/react-adapter
1515

1616
## Usage
1717

18-
Everywhere where a ReactPHP library requires an instance of `LoopInterface`, you just pass `ReactAdapter::get()` to run the ReactPHP library on Amp's event loop.
18+
Everywhere where a ReactPHP library requires an instance of `LoopInterface`, you just pass `ReactAdapter::get()` to run the ReactPHP library on [Revolt](https://revolt.run/) event loop.
1919

2020
```php
2121
<?php
2222

2323
require 'vendor/autoload.php';
2424

25-
use Amp\Loop;
25+
use Revolt\EventLoop;
2626
use Amp\ReactAdapter\ReactAdapter;
2727

28-
Loop::run(function () {
28+
EventLoop::defer(function () {
2929
$app = function ($request, $response) {
3030
$response->writeHead(200, array('Content-Type' => 'text/plain'));
3131
$response->end("Hello World\n");
@@ -41,10 +41,10 @@ Loop::run(function () {
4141
});
4242
```
4343

44-
You can also use the adapter to run ReactPHP apps on an Amp event loop implementation without relying on Amp's global event loop.
44+
You can also use the adapter to run ReactPHP apps on an [Revolt](https://revolt.run/) event loop implementation without relying on Revolt's global event loop.
4545

4646
```php
47-
$loop = new Amp\ReactAdapter\ReactAdapter((new Amp\Loop\DriverFactory)->create());
47+
$loop = new Amp\ReactAdapter\ReactAdapter((new Revolt\EventLoop\DriverFactory)->create());
4848
```
4949

5050
## Documentation

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
"irc": "irc://irc.freenode.org/amphp"
1414
},
1515
"require": {
16-
"amphp/amp": "^2",
17-
"react/event-loop": "^0.5 || ^1"
16+
"amphp/amp": "v3.x-dev",
17+
"react/event-loop": "^0.5 || ^1",
18+
"revolt/event-loop": "^0.2.1"
1819
},
1920
"require-dev": {
2021
"phpunit/phpunit": "^7 || ^6.4",

src/ReactAdapter.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Amp\ReactAdapter;
44

5-
use Amp\Loop;
6-
use Amp\Loop\Driver;
5+
use Revolt\EventLoop;
6+
use Revolt\EventLoop\Driver;
77
use React\EventLoop\LoopInterface;
88
use React\EventLoop\TimerInterface;
99

1010
class ReactAdapter implements LoopInterface
1111
{
12+
private static ReactAdapter $_instance;
1213
private $driver;
1314

1415
private $readWatchers = [];
@@ -18,18 +19,17 @@ class ReactAdapter implements LoopInterface
1819

1920
public static function get(): LoopInterface
2021
{
21-
if ($loop = Loop::getState(self::class)) {
22-
return $loop;
22+
if (isset(self::$_instance)) {
23+
return self::$_instance;
2324
}
24-
25-
Loop::setState(self::class, $loop = new self(Loop::get()));
26-
27-
return $loop;
25+
return new self();
2826
}
2927

30-
public function __construct(Driver $driver)
28+
public function __construct(Driver $driver = null)
3129
{
32-
$this->driver = $driver;
30+
$this->driver = $driver ? $driver : EventLoop::getDriver();
31+
32+
self::$_instance = $this;
3333
}
3434

3535
/** @inheritdoc */
@@ -155,7 +155,7 @@ public function addSignal($signal, $listener)
155155
});
156156

157157
$this->signals[$signal][$watcherId] = $listener;
158-
} catch (Loop\UnsupportedFeatureException $e) {
158+
} catch (EventLoop\UnsupportedFeatureException $e) {
159159
throw new \BadMethodCallException("Signals aren't available in the current environment.");
160160
}
161161
}

0 commit comments

Comments
 (0)