-
-
Notifications
You must be signed in to change notification settings - Fork 18
Add Fiber-based async
and await
functions
#15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,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(); | ||
} | ||
} |
This file contains hidden or 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,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; | ||
} |
This file contains hidden or 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 implements FiberInterface | ||
{ | ||
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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.