8000 Add fiber interoperability support · reactphp/async@145ed6a · GitHub
[go: up one dir, main page]

Skip to content

Commit 145ed6a

Browse files
committed
Add fiber interoperability support
While there is no technical need to add this for this, we also don't want to block other projects creating adapters. However, this interoperability support is undocumented and as such unsupported. Use at your own risk.
1 parent 984382f commit 145ed6a

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/FiberFactory.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace React\Async;
4+
5+
/**
6+
* This factory its only purpose is interoperability. Where with
7+
* event loops one could simply wrap another event loop. But with fibers
8+
* that has become impossible and as such we provide this factory and the
9+
* FiberInterface.
10+
*
11+
* Usage is not documented and as such not supported and might chang without
12+
* notice. Use at your own risk.
13+
*
14+
* @internal
15+
*/
16+
final class FiberFactory
17+
{
18+
private static ?\Closure $factory = null;
19+
20+
public static function create(): FiberInterface
21+
{
22+
return (self::factory())();
23+
}
24+
25+
public static function factory(\Closure $factory = null): \Closure
26+
{
27+
if ($factory !== null) {
28+
self::$factory = $factory;
29+
}
30+
31+
return self::$factory ?? static fn (): FiberInterface => new SimpleFiber();
32+
}
33+
}

src/FiberInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace React\Async;
4+
5+
/**
6+
* This interface its only purpose is interoperability. Where with
7+
* event loops one could simply wrap another event loop. But with fibers
8+
* that has become impossible and as such we provide this interface and the
9+
* FiberFactory.
10+
*
11+
* Usage is not documented and as such not supported and might chang without
12+
* notice. Use at your own risk.
13+
*
14+
* @internal
15+
*/
16+
interface FiberInterface
17+
{
18+
public function resume(mixed $value): void;
19+
20+
public function throw(mixed $throwable): void;
21+
22+
public function suspend(): mixed;
23+
}

src/SimpleFiber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @internal
99
*/
10-
final class SimpleFiber
10+
final class SimpleFiber implements FiberInterface
1111
{
1212
private static ?\Fiber $scheduler = null;
1313
private ?\Fiber $fiber = null;

src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function async(callable $function, mixed ...$args): PromiseInterface
7878
*/
7979
function await(PromiseInterface $promise): mixed
8080
{
81-
$fiber = new SimpleFiber();
81+
$fiber = FiberFactory::create();
8282

8383
$promise->then(
8484
function (mixed $value) use (&$resolved, $fiber): void {

0 commit comments

Comments
 (0)
0