diff --git a/README.md b/README.md index 2108d98..2f2d876 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,11 @@ Table of Contents * [Deferred](#deferred) * [Promise](#promise-1) 3. [API](#api) + * [DeferredInterface](#deferredinterface) + * [DeferredInterface::promise()](#deferredinterfacepromise) + * [DeferredInterface::resolve()](#deferredinterfaceresolve) + * [DeferredInterface::reject()](#deferredinterfacereject) * [Deferred](#deferred-1) - * [Deferred::promise()](#deferredpromise) - * [Deferred::resolve()](#deferredresolve) - * [Deferred::reject()](#deferredreject) * [PromiseInterface](#promiseinterface) * [PromiseInterface::then()](#promiseinterfacethen) * [PromiseInterface::catch()](#promiseinterfacecatch) @@ -75,28 +76,17 @@ a placeholder for its actual result. API --- -### Deferred +### DeferredInterface + +The deferred interface provides the common interface for all deferred +implementations. +See [Deferred](#deferred-1) for the only public implementation exposed by this +package. A deferred represents an operation whose resolution is pending. It has separate promise and resolver parts. -```php -$deferred = new React\Promise\Deferred(); - -$promise = $deferred->promise(); - -$deferred->resolve(mixed $value); -$deferred->reject(\Throwable $reason); -``` - -The `promise` method returns the promise of the deferred. - -The `resolve` and `reject` methods control the state of the deferred. - -The constructor of the `Deferred` accepts an optional `$canceller` argument. -See [Promise](#promise-2) for more information. - -#### Deferred::promise() +#### DeferredInterface::promise() ```php $promise = $deferred->promise(); @@ -105,7 +95,7 @@ $promise = $deferred->promise(); Returns the promise of the deferred, which you can hand out to others while keeping the authority to modify its state to yourself. -#### Deferred::resolve() +#### DeferredInterface::resolve() ```php $deferred->resolve(mixed $value); @@ -120,7 +110,7 @@ this promise once it is resolved. See also the [`resolve()` function](#resolve). -#### Deferred::reject() +#### DeferredInterface::reject() ```php $deferred->reject(\Throwable $reason); @@ -133,6 +123,26 @@ All consumers are notified by having `$onRejected` (which they registered via See also the [`reject()` function](#reject). +### Deferred + +Creates a Deferred to control the state of an associated promise. + +```php +$deferred = new React\Promise\Deferred(); + +$promise = $deferred->promise(); + +$deferred->resolve(mixed $value); +$deferred->reject(\Throwable $reason); +``` + +The `promise` method returns the promise of the deferred. + +The `resolve` and `reject` methods control the state of the deferred. + +The constructor of the `Deferred` accepts an optional `$canceller` argument. +See [Promise](#promise-2) for more information. + ### PromiseInterface The promise interface provides the common interface for all promise diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 33ea968..9fc79e2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -28,5 +28,7 @@ + + diff --git a/src/Deferred.php b/src/Deferred.php index 80b8fcf..5942640 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -4,8 +4,9 @@ /** * @template T + * @template-implements DeferredInterface */ -final class Deferred +final class Deferred implements DeferredInterface { /** * @var PromiseInterface diff --git a/src/DeferredInterface.php b/src/DeferredInterface.php new file mode 100644 index 0000000..94b333c --- /dev/null +++ b/src/DeferredInterface.php @@ -0,0 +1,23 @@ + + */ + public function promise(): PromiseInterface; + + /** + * @param T $value + */ + public function resolve($value): void; + + public function reject(\Throwable $reason): void; + +} \ No newline at end of file diff --git a/tests/FunctionResolveTest.php b/tests/FunctionResolveTest.php index 1eacd34..079c02a 100644 --- a/tests/FunctionResolveTest.php +++ b/tests/FunctionResolveTest.php @@ -128,18 +128,31 @@ function ($val) { /** @test */ public function shouldSupportVeryDeepNestedPromises(): void { - if (PHP_VERSION_ID < 70200 && ini_get('xdebug.max_nesting_level') !== false) { + $stackDepthLimit = ini_get('xdebug.max_nesting_level'); + if (PHP_VERSION_ID < 70200 && $stackDepthLimit !== false) { $this->markTestSkipped('Skip unhandled rejection on legacy PHP 7.1'); } + // Calculate the maximum number of nested promises we can handle + $stackOverhead = 25; // allow for PHPUnit's stack entries + $promiseStackRequirement = 4; // each nested promise requires 4 entries on the stack + + // Normalise stack depth limit and ensure we have enough room on the stack for a meaningful test + $stackDepthLimit = (int) ($stackDepthLimit ?: 512); + if ($stackDepthLimit <= $stackOverhead + ($promiseStackRequirement * 10)) { + $this->markTestSkipped("Skip xdebug.max_nesting_limit set too low"); + } + + $nestingLimit = (int) (($stackDepthLimit - $stackOverhead) / $promiseStackRequirement); + $deferreds = []; - for ($i = 0; $i < 150; $i++) { + for ($i = 0; $i < $nestingLimit; $i++) { $deferreds[] = $d = new Deferred(); $p = $d->promise(); $last = $p; - for ($j = 0; $j < 150; $j++) { + for ($j = 0; $j < $nestingLimit; $j++) { $last = $last->then(function ($result) { return $result; });