/
ReturnChild.php
31 lines (25 loc) · 1004 Bytes
/
ReturnChild.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
declare(strict_types=1);
namespace WyriHaximus\React\ChildProcess\Messenger;
use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessagesFactory;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
use function React\Promise\resolve;
final class ReturnChild implements ChildInterface
{
/** @phpstan-ignore-next-line */
private function __construct(Messenger $messenger, LoopInterface $loop)
{
$messenger->registerRpc('return', static function (Payload $payload): PromiseInterface {
return resolve($payload->getPayload());
});
$messenger->on('message', static function (Payload $payload) use ($messenger): void {
$messenger->message(MessagesFactory::message($payload->getPayload()));
});
}
public static function create(Messenger $messenger, LoopInterface $loop): void
{
new static($messenger, $loop);
}
}