-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
249f9f6
commit 3f96c9d
Showing
6 changed files
with
125 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace React\Async; | ||
|
||
/** | ||
* @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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace React\Async; | ||
|
||
use React\EventLoop\Loop; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface FiberInterface | ||
{ | ||
public function resume(mixed $value): void; | ||
|
||
public function throw(mixed $throwable): void; | ||
|
||
public function suspend(): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace React\Async; | ||
|
||
use React\EventLoop\Loop; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class SimpleFiber implements FiberInterface | ||
{ | ||
private ?\Fiber $fiber = null; | ||
|
||
public function __construct() | ||
{ | ||
$this->fiber = \Fiber::getCurrent(); | ||
} | ||
|
||
public function resume(mixed $value): void | ||
{ | ||
if ($this->fiber === null) { | ||
Loop::futureTick(static fn() => \Fiber::suspend(static fn() => $value)); | ||
return; | ||
} | ||
|
||
Loop::futureTick(fn() => $this->fiber->resume($value)); | ||
} | ||
|
||
public function throw(mixed $throwable): void | ||
{ | ||
if (!$throwable instanceof \Throwable) { | ||
$throwable = new \UnexpectedValueException( | ||
'Promise rejected with unexpected value of type ' . (is_object($throwable) ? get_class($throwable) : gettype($throwable)) | ||
); | ||
} | ||
|
||
if ($this->fiber === null) { | ||
Loop::futureTick(static fn() => \Fiber::suspend(static fn() => throw $throwable)); | ||
return; | ||
} | ||
|
||
Loop::futureTick(fn() => $this->fiber->throw($throwable)); | ||
} | ||
|
||
public function suspend(): mixed | ||
{ | ||
if ($this->fiber === null) { | ||
$fiber = fiber(static fn() => Loop::run()); | ||
return ($fiber->isStarted() ? $fiber->resume() : $fiber->start())(); | ||
} | ||
|
||
return \Fiber::suspend(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters