-
-
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 6e08372
Showing
6 changed files
with
245 additions
and
60 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
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,64 @@ | ||
<?php | ||
|
||
namespace React\Async; | ||
|
||
use React\EventLoop\Loop; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class SimpleFiber | ||
{ | ||
private static ?\Fiber $scheduler = null; | ||
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) { | ||
if (self::$scheduler === null || self::$scheduler->isTerminated()) { | ||
self::$scheduler = new \Fiber(static fn() => Loop::run()); | ||
// Run event loop to completion on shutdown. | ||
\register_shutdown_function(static function (): void { | ||
if (self::$scheduler->isSuspended()) { | ||
self::$scheduler->resume(); | ||
} | ||
}); | ||
} | ||
|
||
return (self::$scheduler->isStarted() ? self::$scheduler->resume() : self::$scheduler->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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace React\Tests\Async; | ||
|
||
use React; | ||
use React\EventLoop\Loop; | ||
use React\Promise\Promise; | ||
use function React\Async\async; | ||
use function React\Async\await; | ||
use function React\Promise\all; | ||
|
||
class AsyncTest extends TestCase | ||
{ | ||
public function testAsyncReturnsPendingPromise() | ||
{ | ||
$promise = async(function () { | ||
return 42; | ||
}); | ||
|
||
$promise->then($this->expectCallableNever(), $this->expectCallableNever()); | ||
} | ||
|
||
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturns() | ||
{ | ||
$promise = async(function () { | ||
return 42; | ||
}); | ||
|
||
$value = await($promise); | ||
|
||
$this->assertEquals(42, $value); | ||
} | ||
|
||
public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackThrows() | ||
{ | ||
$promise = async(function () { | ||
throw new \RuntimeException('Foo', 42); | ||
}); | ||
|
||
$this->expectException(\RuntimeException::class); | ||
$this->expectExceptionMessage('Foo'); | ||
$this->expectExceptionCode(42); | ||
await($promise); | ||
} | ||
|
||
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsAfterAwaitingPromise() | ||
{ | ||
$promise = async(function () { | ||
$promise = new Promise(function ($resolve) { | ||
Loop::addTimer(0.001, fn () => $resolve(42)); | ||
}); | ||
|
||
return await($promise); | ||
}); | ||
|
||
$value = await($promise); | ||
|
||
$this->assertEquals(42, $value); | ||
} | ||
|
||
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsAfterAwaitingTwoConcurrentPromises() | ||
{ | ||
$promise1 = async(function () { | ||
$promise = new Promise(function ($resolve) { | ||
Loop::addTimer(0.11, fn () => $resolve(21)); | ||
}); | ||
|
||
return await($promise); | ||
}); | ||
|
||
$promise2 = async(function () { | ||
$promise = new Promise(function ($resolve) { | ||
Loop::addTimer(0.11, fn () => $resolve(42)); | ||
}); | ||
|
||
return await($promise); | ||
}); | ||
|
||
$time = microtime(true); | ||
$values = await(all([$promise1, $promise2])); | ||
$time = microtime(true) - $time; | ||
|
||
$this->assertEquals([21, 42], $values); | ||
$this->assertGreaterThan(0.1, $time); | ||
$this->assertLessThan(0.12, $time); | ||
} | ||
} |
Oops, something went wrong.