Skip to content

Commit 12f28d2

Browse files
committed
Updates
1 parent c081413 commit 12f28d2

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/Hardware/Remote/RemoteCharacterDevice.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Flat3\RevPi\Constants;
88
use Flat3\RevPi\Interfaces\Hardware\Stream;
9-
use Flat3\RevPi\JsonRpc\Event;
109
use Flat3\RevPi\JsonRpc\JsonRpcDevice;
1110
use Revolt\EventLoop;
1211

@@ -33,10 +32,8 @@ public function fdopen(): mixed
3332
{
3433
$this->peer->request('fdopen')->await();
3534

36-
$this->peer->on(function (Event $event) {
37-
if ($event->type === 'readable') {
38-
fwrite($this->remote, $event->payload);
39-
}
35+
$this->peer->on('readable', function (string $payload) {
36+
fwrite($this->remote, $payload);
4037
});
4138

4239
EventLoop::onReadable($this->remote, function ($callbackId, $stream) {

src/JsonRpc/Event.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
/**
88
* @phpstan-import-type JsonRpcEventT from JsonRpcPeer
9+
* @phpstan-import-type JsonRpcEventTypeT from JsonRpcPeer
910
*/
1011
class Event
1112
{
13+
/**
14+
* @var JsonRpcEventTypeT
15+
*/
1216
public string $type;
1317

1418
public string $payload;

src/JsonRpc/JsonRpcPeer.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Amp\Websocket\WebsocketClient;
1313
use Closure;
1414
use Flat3\RevPi\Exceptions\RemoteDeviceException;
15-
use Flat3\RevPi\Interfaces\Hardware\Device;
1615
use Flat3\RevPi\JsonRpc\Request as JsonRpcRequest;
1716
use Flat3\RevPi\JsonRpc\Response as JsonRpcResponse;
1817
use Throwable;
@@ -22,7 +21,8 @@
2221
/**
2322
* @phpstan-type JsonRpcDeviceMethodT 'open'|'close'|'lseek'|'ioctl'|'read'|'write'|'cfgetispeed'|'cfgetospeed'|'cfsetispeed'|'cfsetospeed'|'tcflush'|'tcdrain'|'tcsendbreak'|'fdopen'
2423
* @phpstan-type JsonRpcMethodT JsonRpcDeviceMethodT
25-
* @phpstan-type JsonRpcEventTypeT 'readable'
24+
* @phpstan-type JsonRpcDeviceEventTypeT 'readable'
25+
* @phpstan-type JsonRpcEventTypeT JsonRpcDeviceEventTypeT
2626
* @phpstan-type JsonRpcRequestParamsT array<string, int|string|null>
2727
* @phpstan-type JsonRpcRequestT array{id: string, method: JsonRpcMethodT, params: JsonRpcRequestParamsT }
2828
* @phpstan-type JsonRpcResponseResultT int|string|array<string, int|string|null>
@@ -33,18 +33,26 @@ abstract class JsonRpcPeer implements WebsocketClientHandler
3333
{
3434
protected WebsocketClient $socket;
3535

36-
protected Device $device;
37-
3836
/**
39-
* @var callable
37+
* @var array{readable: array<callable>}
4038
*/
41-
protected mixed $eventReceiver;
39+
protected array $callbacks = [
40+
'readable' => [],
41+
];
4242

4343
/**
4444
* @var array<DeferredFuture<JsonRpcResponseResultT>>
4545
*/
4646
protected array $pending = [];
4747

48+
/**
49+
* @param JsonRpcDeviceEventTypeT $event
50+
*/
51+
public function on(string $event, Closure $callback): void
52+
{
53+
$this->callbacks[$event][] = $callback;
54+
}
55+
4856
public function withSocket(WebsocketClient $socket): self
4957
{
5058
$this->socket = $socket;
@@ -85,7 +93,7 @@ protected function handleResponse(): void
8593
$response = unserialize($payload);
8694

8795
if ($response instanceof Event) {
88-
call_user_func($this->eventReceiver, $response);
96+
collect($this->callbacks[$response->type])->each(fn ($callback) => $callback($response->payload));
8997

9098
continue;
9199
}
@@ -121,11 +129,6 @@ protected function handleResponse(): void
121129
}
122130
}
123131

124-
public function on(Closure $callback): void
125-
{
126-
$this->eventReceiver = $callback;
127-
}
128-
129132
public function handleClient(WebsocketClient $client, Request $request, Response $response): void
130133
{
131134
$this->socket = $client;

0 commit comments

Comments
 (0)