Skip to content

Commit

Permalink
Add fiber interoperability support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
WyriHaximus committed Nov 22, 2021
1 parent 5f38c49 commit 715a3ae
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/FiberFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace React\Async;

/**
* This factory its only purpose is interoperability. Where with
* event loops one could simply wrap another event loop. But with fibers
* that has become impossible and as such we provide this factory and the
* FiberInterface.
*
* Usage is not documented and as such not supported and might chang without
* notice. Use at your own risk.
*
* @internal
*/
final class FiberFactory
{
private static ?\Closure $factory = null;

public static function create(): FiberInterface
{
return (self::factory())();
}

public static function factory(\Closure $factory = null): \Closure
{
if ($factory !== null) {
self::$factory = $factory;
}

return self::$factory ?? static fn (): FiberInterface => new SimpleFiber();
}
}
23 changes: 23 additions & 0 deletions src/FiberInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace React\Async;

/**
* This interface its only purpose is interoperability. Where with
* event loops one could simply wrap another event loop. But with fibers
* that has become impossible and as such we provide this interface and the
* FiberFactory.
*
* Usage is not documented and as such not supported and might chang without
* notice. Use at your own risk.
*
* @internal
*/
interface FiberInterface
{
public function resume(mixed $value): void;

public function throw(mixed $throwable): void;

public function suspend(): mixed;
}
2 changes: 1 addition & 1 deletion src/SimpleFiber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @internal
*/
final class SimpleFiber
final class SimpleFiber implements FiberInterface
{
private static ?\Fiber $scheduler = null;
private ?\Fiber $fiber = null;
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function async(callable $coroutine, mixed ...$args): PromiseInterface
*/
function await(PromiseInterface $promise): mixed
{
$fiber = new SimpleFiber();
$fiber = FiberFactory::create();

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

0 comments on commit 715a3ae

Please sign in to comment.