Skip to content

Commit 4163e73

Browse files
committed
Add template types
1 parent 4b4bb93 commit 4163e73

File tree

7 files changed

+203
-31
lines changed

7 files changed

+203
-31
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"require": {
1313
"php": "^8.2",
1414
"ext-parallel": "*",
15-
"react-parallel/event-loop": "dev-master as 2.0",
15+
"react-parallel/event-loop": "dev-template-types as 2.0",
1616
"react/event-loop": "^1.1",
1717
"react/promise": "^2.7 || ^3.0",
1818
"wyrihaximus/constants": "^1.6"

etc/qa/phpstan.neon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ parameters:
66
- ReactParallel\EventLoop\CancelledException
77
- ReactParallel\EventLoop\CanceledFuture
88
- ReactParallel\EventLoop\KilledRuntime
9-
# stubFiles:
10-
# - ../../stubs/Event.stub
11-
# - ../../stubs/Future.stub
12-
# - ../../stubs/Channel.stub
9+
stubFiles:
10+
- ../../stubs/Event.stub
11+
- ../../stubs/Future.stub
12+
- ../../stubs/Channel.stub
1313

1414
includes:
1515
- ../../vendor/wyrihaximus/async-test-utilities/rules.neon

src/Runtime.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
use Closure;
88
use parallel\Future;
99
use parallel\Runtime as ParallelRuntime;
10-
use React\Promise\PromiseInterface;
1110
use ReactParallel\EventLoop\EventLoopBridge;
1211

13-
use function React\Promise\resolve;
14-
1512
use const WyriHaximus\Constants\ComposerAutoloader\LOCATION;
1613

1714
final class Runtime
@@ -28,16 +25,23 @@ public function __construct(private EventLoopBridge $eventLoopBridge, string $au
2825
$this->runtime = new ParallelRuntime($autoload);
2926
}
3027

31-
/** @param array<int, mixed> $args */
32-
public function run(Closure $callable, array $args = []): PromiseInterface
28+
/**
29+
* @param (Closure():T) $callable
30+
* @param array<int, mixed> $args
31+
*
32+
* @return ?T
33+
*
34+
* @template T
35+
*/
36+
public function run(Closure $callable, array $args = []): mixed
3337
{
3438
$future = $this->runtime->run($callable, $args);
3539

3640
if ($future instanceof Future) {
37-
return resolve($this->eventLoopBridge->await($future));
41+
return $this->eventLoopBridge->await($future);
3842
}
3943

40-
return resolve($future);
44+
return $future;
4145
}
4246

4347
public function close(): void

stubs/Channel.stub

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace parallel;
4+
5+
/**
6+
* @template T
7+
*/
8+
final class Channel
9+
{
10+
/**
11+
* Constant for Infinitely Buffered
12+
*/
13+
public const Infinite = -1;
14+
15+
/* Anonymous Constructor */
16+
17+
/**
18+
* Shall make an anonymous unbuffered channel
19+
* Shall make an anonymous buffered channel with the given capacity
20+
*
21+
* @param null|int $capacity May be Channel::Infinite or a positive integer
22+
*/
23+
public function __construct(?int $capacity = null) {}
24+
25+
/* Access */
26+
27+
/**
28+
* Shall make an unbuffered channel with the given name
29+
* Shall make a buffered channel with the given name and capacity
30+
*
31+
* @param string $name The name of the channel.
32+
* @param null|int $capacity May be Channel::Infinite or a positive integer
33+
*
34+
* @return Channel<T>
35+
*
36+
* @throws Channel\Error\Existence if channel already exists.
37+
*/
38+
public static function make(string $name, ?int $capacity = null): Channel {}
39+
40+
/**
41+
* Shall open the channel with the given name
42+
*
43+
* @param string $name
44+
* @return Channel<T>
45+
*
46+
* @throws Channel\Error\Existence if channel does not exist.
47+
*/
48+
public static function open(string $name): Channel {}
49+
50+
/* Sharing */
51+
52+
/**
53+
* Shall send the given value on this channel
54+
* @param T $value
55+
*
56+
* @throws Channel\Error\Closed if channel is closed.
57+
* @throws Channel\Error\IllegalValue if value is illegal.
58+
*/
59+
public function send($value): void {}
60+
61+
/**
62+
* Shall recv a value from this channel
63+
* @return T
64+
*
65+
* @throws Channel\Error\Closed if channel is closed.
66+
*/
67+
public function recv() {}
68+
69+
/* Closing */
70+
71+
/**
72+
* Shall close this channel
73+
* @throws Channel\Error\Closed if channel is closed.
74+
*/
75+
public function close(): void {}
76+
77+
/**
78+
* Returns name of channel
79+
* @return string
80+
*/
81+
public function __toString(): string {}
82+
}

stubs/Event.stub

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace parallel\Events;
4+
5+
use parallel\Channel;
6+
use parallel\Future;
7+
8+
/**
9+
* @template T
10+
*/
11+
final class Event
12+
{
13+
/**
14+
* Shall be one of Event\Type constants
15+
* @var int
16+
*/
17+
public $type;
18+
19+
/**
20+
* Shall be the source of the event (target name)
21+
* @var string
22+
*/
23+
public $source;
24+
25+
/**
26+
* Shall be either Future or Channel
27+
* @var Future<T>|Channel<T>
28+
*/
29+
public $object;
30+
31+
/**
32+
* Shall be set for Read/Error events
33+
* @var T
34+
*/
35+
public $value;
36+
}

stubs/Future.stub

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace parallel;
4+
5+
use Throwable;
6+
7+
/**
8+
* @template T
9+
*/
10+
final class Future
11+
{
12+
/* Resolution */
13+
14+
/**
15+
* Shall return (and if necessary wait for) return from task
16+
*
17+
* @return T
18+
*
19+
* @throws Future\Error if waiting failed (internal error).
20+
* @throws Future\Error\Killed if \parallel\Runtime executing task was killed.
21+
* @throws Future\Error\Cancelled if task was cancelled.
22+
* @throws Future\Error\Foreign if task raised an unrecognized uncaught exception.
23+
* @throws Throwable Shall rethrow \Throwable uncaught in task
24+
*/
25+
public function value() {}
26+
27+
/* State */
28+
29+
/**
30+
* Shall indicate if the task is completed
31+
* @return bool
32+
*/
33+
public function done(): bool {}
34+
35+
/**
36+
* Shall indicate if the task was cancelled
37+
* @return bool
38+
*/
39+
public function cancelled(): bool {}
40+
41+
/* Cancellation */
42+
43+
/**
44+
* Shall try to cancel the task
45+
* Note: If task is running, it will be interrupted.
46+
* Warning: Internal function calls in progress cannot be interrupted.
47+
*
48+
* @return bool
49+
*
50+
* @throws Future\Error\Killed if \parallel\Runtime executing task was killed.
51+
* @throws Future\Error\Cancelled if task was already cancelled.
52+
*/
53+
public function cancel(): bool {}
54+
}

tests/RuntimeTest.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ public function convertSuccess(): void
2222
{
2323
$runtime = Runtime::create(new EventLoopBridge());
2424

25-
$promise = $runtime->run(static function (): int {
26-
sleep(3);
25+
try {
26+
$three = $runtime->run(static function (): int {
27+
sleep(3);
2728

28-
return 3;
29-
});
30-
31-
$promise->always(static function () use ($runtime): void {
29+
return 3;
30+
});
31+
} finally {
3232
$runtime->kill();
33-
});
34-
35-
$three = await($promise); /** @phpstan-ignore-line */
33+
}
3634

3735
self::assertSame(3, $three);
3836
}
@@ -45,19 +43,17 @@ public function convertFailure(): void
4543

4644
$runtime = Runtime::create(new EventLoopBridge());
4745

48-
$promise = $runtime->run(static function (): void {
49-
sleep(3);
46+
try {
47+
$three = $runtime->run(static function (): void {
48+
sleep(3);
5049

51-
throw new LatchcombException('Rethrow exception');
52-
});
53-
54-
$promise->always(static function () use ($runtime): void {
50+
throw new LatchcombException('Rethrow exception');
51+
});
52+
} finally {
5553
$runtime->close();
56-
});
57-
58-
$three = await($promise); /** @phpstan-ignore-line */
54+
}
5955

60-
self::assertSame(3, $three);
56+
// self::assertSame(3, $three);
6157
}
6258

6359
/** @test */

0 commit comments

Comments
 (0)