|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace React\Async; |
| 4 | + |
| 5 | +use React\EventLoop\Loop; |
| 6 | + |
| 7 | +/** |
| 8 | + * @internal |
| 9 | + */ |
| 10 | +final class SimpleFiber |
| 11 | +{ |
| 12 | + private static ?\Fiber $scheduler = null; |
| 13 | + private ?\Fiber $fiber = null; |
| 14 | + |
| 15 | + public function __construct() |
| 16 | + { |
| 17 | + $this->fiber = \Fiber::getCurrent(); |
| 18 | + } |
| 19 | + |
| 20 | + public function resume(mixed $value): void |
| 21 | + { |
| 22 | + if ($this->fiber === null) { |
| 23 | + Loop::futureTick(static fn() => \Fiber::suspend(static fn() => $value)); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + Loop::futureTick(fn() => $this->fiber->resume($value)); |
| 28 | + } |
| 29 | + |
| 30 | + public function throw(mixed $throwable): void |
| 31 | + { |
| 32 | + if (!$throwable instanceof \Throwable) { |
| 33 | + $throwable = new \UnexpectedValueException( |
| 34 | + 'Promise rejected with unexpected value of type ' . (is_object($throwable) ? get_class($throwable) : gettype($throwable)) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + if ($this->fiber === null) { |
| 39 | + Loop::futureTick(static fn() => \Fiber::suspend(static fn() => throw $throwable)); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + Loop::futureTick(fn() => $this->fiber->throw($throwable)); |
| 44 | + } |
| 45 | + |
| 46 | + public function suspend(): mixed |
| 47 | + { |
| 48 | + if ($this->fiber === null) { |
| 49 | + if (self::$scheduler === null || self::$scheduler->isTerminated()) { |
| 50 | + self::$scheduler = new \Fiber(static fn() => Loop::run()); |
| 51 | + // Run event loop to completion on shutdown. |
| 52 | + \register_shutdown_function(static function (): void { |
| 53 | + if (self::$scheduler->isSuspended()) { |
| 54 | + self::$scheduler->resume(); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + return (self::$scheduler->isStarted() ? self::$scheduler->resume() : self::$scheduler->start())(); |
| 60 | + } |
| 61 | + |
| 62 | + return \Fiber::suspend(); |
| 63 | + } |
| 64 | +} |
0 commit comments