|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Revolt\EventLoop\React\Internal; |
| 4 | + |
| 5 | +use React\Async\FiberInterface; |
| 6 | +use Revolt\EventLoop; |
| 7 | + |
| 8 | +/** @internal */ |
| 9 | +final class FiberAdapter implements FiberInterface |
| 10 | +{ |
| 11 | + private ?EventLoop\Suspension $suspension = null; |
| 12 | + |
| 13 | + public function resume(mixed $value): void |
| 14 | + { |
| 15 | + if ($this->suspension === null) { |
| 16 | + throw new \Error('Must call suspend() before calling resume()'); |
| 17 | + } |
| 18 | + |
| 19 | + $this->suspension->resume($value); |
| 20 | + |
| 21 | + // Note: resume() above is async in revolt, but sync in react, |
| 22 | + // so let's suspend here until the queued resumption above is executed. |
| 23 | + $suspension = EventLoop::getSuspension(); |
| 24 | + EventLoop::queue($suspension->resume(...)); |
| 25 | + $suspension->suspend(); |
| 26 | + } |
| 27 | + |
| 28 | + public function throw(\Throwable $throwable): void |
| 29 | + { |
| 30 | + if ($this->suspension === null) { |
| 31 | + throw new \Error('Must call suspend() before calling throw()'); |
| 32 | + } |
| 33 | + |
| 34 | + $this->suspension->throw($throwable); |
| 35 | + |
| 36 | + // Note: throw() above is async in revolt, but sync in react, |
| 37 | + // so let's suspend here until the queued throwing above is executed. |
| 38 | + $suspension = EventLoop::getSuspension(); |
| 39 | + EventLoop::queue($suspension->resume(...)); |
| 40 | + $suspension->suspend(); |
| 41 | + } |
| 42 | + |
| 43 | + public function suspend(): mixed |
| 44 | + { |
| 45 | + if ($this->suspension !== null) { |
| 46 | + throw new \Error('Must call resume() or throw() before calling suspend() again'); |
| 47 | + } |
| 48 | + |
| 49 | + $this->suspension = EventLoop::getSuspension(); |
| 50 | + |
| 51 | + return $this->suspension->suspend(); |
| 52 | + } |
| 53 | +} |
0 commit comments