-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntime.php
More file actions
60 lines (46 loc) · 1.38 KB
/
Runtime.php
File metadata and controls
60 lines (46 loc) · 1.38 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace ReactParallel\Runtime;
use Closure;
use parallel\Future;
use React\Promise\PromiseInterface;
use ReactParallel\EventLoop\EventLoopBridge;
use WyriHaximus\Parallel\Runtime as ParallelRuntime;
use function React\Promise\resolve;
use function var_export;
use const WyriHaximus\Constants\ComposerAutoloader\LOCATION;
final class Runtime
{
private ParallelRuntime $runtime;
private EventLoopBridge $eventLoopBridge;
public static function create(EventLoopBridge $eventLoopBridge): self
{
return new self($eventLoopBridge, LOCATION);
}
public function __construct(EventLoopBridge $eventLoopBridge, string $autoload)
{
$this->eventLoopBridge = $eventLoopBridge;
$this->runtime = new ParallelRuntime($autoload);
}
/**
* @param array<int, mixed> $args
*/
public function run(Closure $callable, array $args = []): PromiseInterface
{
$future = $this->runtime->run($callable, $args);
var_export($future);
if ($future instanceof Future) {
/** @psalm-suppress MissingClosureReturnType */
return $this->eventLoopBridge->await($future);
}
return resolve($future);
}
public function close(): void
{
$this->runtime->close();
}
public function kill(): void
{
$this->runtime->kill();
}
}