Skip to content
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

Merge promise interfaces #75

Merged
merged 4 commits into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ CHANGELOG for 3.x
`some()`, `map()`, `reduce()`) now require an array of promises or values
as input. Before, arrays and promises which resolve to an array were
supported, other input types resolved to empty arrays or `null`. (#35).
* BC break: The interfaces `PromiseInterface`, `ExtendedPromiseInterface`
and `CancellablePromiseInterface` have been merged into a single
`PromiseInterface`. The `CancellablePromiseInterface` has been kept for
backward compatibility but is marked as deprecated and must not be used
anymore.
50 changes: 9 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ Table of Contents
* [Deferred::reject()](#deferredreject)
* [PromiseInterface](#promiseinterface)
* [PromiseInterface::then()](#promiseinterfacethen)
* [ExtendedPromiseInterface](#extendedpromiseinterface)
* [ExtendedPromiseInterface::done()](#extendedpromiseinterfacedone)
* [ExtendedPromiseInterface::otherwise()](#extendedpromiseinterfaceotherwise)
* [ExtendedPromiseInterface::always()](#extendedpromiseinterfacealways)
* [CancellablePromiseInterface](#cancellablepromiseinterface)
* [CancellablePromiseInterface::cancel()](#cancellablepromiseinterfacecancel)
* [PromiseInterface::done()](#promiseinterfacedone)
* [PromiseInterface::otherwise()](#promiseinterfaceotherwise)
* [PromiseInterface::always()](#promiseinterfacealways)
* [PromiseInterface::cancel()](#promiseinterfacecancel)
* [Promise](#promise-1)
* [FulfilledPromise](#fulfilledpromise)
* [RejectedPromise](#rejectedpromise)
Expand Down Expand Up @@ -193,22 +191,10 @@ the same call to `then()`:

* [resolve()](#resolve) - Creating a resolved promise
* [reject()](#reject) - Creating a rejected promise
* [ExtendedPromiseInterface::done()](#extendedpromiseinterfacedone)
* [PromiseInterface::done()](#promiseinterfacedone)
* [done() vs. then()](#done-vs-then)

### ExtendedPromiseInterface

The ExtendedPromiseInterface extends the PromiseInterface with useful shortcut
and utility methods which are not part of the Promises/A specification.

#### Implementations

* [Promise](#promise-1)
* [FulfilledPromise](#fulfilledpromise)
* [RejectedPromise](#rejectedpromise)
* [LazyPromise](#lazypromise)

#### ExtendedPromiseInterface::done()
#### PromiseInterface::done()

```php
$promise->done(callable $onFulfilled = null, callable $onRejected = null);
Expand All @@ -228,7 +214,7 @@ Since the purpose of `done()` is consumption rather than transformation,
* [PromiseInterface::then()](#promiseinterfacethen)
* [done() vs. then()](#done-vs-then)

#### ExtendedPromiseInterface::otherwise()
#### PromiseInterface::otherwise()

```php
$promise->otherwise(callable $onRejected);
Expand All @@ -254,7 +240,7 @@ $promise
)};
```

#### ExtendedPromiseInterface::always()
#### PromiseInterface::always()

```php
$newPromise = $promise->always(callable $onFulfilledOrRejected);
Expand Down Expand Up @@ -301,13 +287,7 @@ return doSomething()
->always('cleanup');
```

### CancellablePromiseInterface

A cancellable promise provides a mechanism for consumers to notify the creator
of the promise that they are not longer interested in the result of an
operation.

#### CancellablePromiseInterface::cancel()
#### PromiseInterface::cancel()

``` php
$promise->cancel();
Expand All @@ -319,13 +299,6 @@ further interest in the results of the operation.
Once a promise is settled (either fulfilled or rejected), calling `cancel()` on
a promise has no effect.

#### Implementations

* [Promise](#promise-1)
* [FulfilledPromise](#fulfilledpromise)
* [RejectedPromise](#rejectedpromise)
* [LazyPromise](#lazypromise)

### Promise

Creates a promise whose state is controlled by the functions passed to
Expand Down Expand Up @@ -435,11 +408,6 @@ a trusted promise that follows the state of the thenable is returned.

If `$promiseOrValue` is a promise, it will be returned as is.

Note: The promise returned is always a promise implementing
[ExtendedPromiseInterface](#extendedpromiseinterface). If you pass in a custom
promise which only implements [PromiseInterface](#promiseinterface), this
promise will be assimilated to a extended promise following `$promiseOrValue`.

#### reject()

```php
Expand Down
13 changes: 12 additions & 1 deletion src/CancellablePromiseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

namespace React\Promise;

interface CancellablePromiseInterface extends PromiseInterface
/**
* This interface is only kept for backward compatibility and must not be used
* anymore.
*
* @deprecated
*/
interface CancellablePromiseInterface
{
/**
* @return PromiseInterface
*/
public function then(callable $onFulfilled = null, callable $onRejected = null);

/**
* @return void
*/
Expand Down
21 changes: 0 additions & 21 deletions src/ExtendedPromiseInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace React\Promise;

class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
class FulfilledPromise implements PromiseInterface, CancellablePromiseInterface
{
private $value;

Expand Down Expand Up @@ -43,7 +43,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
queue()->enqueue(function () use ($onFulfilled) {
$result = $onFulfilled($this->value);

if ($result instanceof ExtendedPromiseInterface) {
if ($result instanceof PromiseInterface) {
$result->done();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/LazyPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace React\Promise;

class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
class LazyPromise implements PromiseInterface, CancellablePromiseInterface
{
private $factory;
private $promise;
Expand Down
8 changes: 4 additions & 4 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace React\Promise;

class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface
class Promise implements PromiseInterface, CancellablePromiseInterface
{
private $canceller;
private $result;
Expand Down Expand Up @@ -45,7 +45,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
return $this->result()->done($onFulfilled, $onRejected);
}

$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected) {
$this->handlers[] = function (PromiseInterface $promise) use ($onFulfilled, $onRejected) {
$promise
->done($onFulfilled, $onRejected);
};
Expand Down Expand Up @@ -90,7 +90,7 @@ public function cancel()
private function resolver(callable $onFulfilled = null, callable $onRejected = null)
{
return function ($resolve, $reject) use ($onFulfilled, $onRejected) {
$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject) {
$this->handlers[] = function (PromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject) {
$promise
->then($onFulfilled, $onRejected)
->done($resolve, $reject);
Expand All @@ -116,7 +116,7 @@ private function reject($reason = null)
$this->settle(reject($reason));
}

private function settle(ExtendedPromiseInterface $result)
private function settle(PromiseInterface $result)
{
if ($result instanceof LazyPromise) {
$result = $result->promise();
Expand Down
20 changes: 20 additions & 0 deletions src/PromiseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,24 @@ interface PromiseInterface
* @return PromiseInterface
*/
public function then(callable $onFulfilled = null, callable $onRejected = null);

/**
* @return void
*/
public function done(callable $onFulfilled = null, callable $onRejected = null);

/**
* @return PromiseInterface
*/
public function otherwise(callable $onRejected);

/**
* @return PromiseInterface
*/
public function always(callable $onFulfilledOrRejected);

/**
* @return void
*/
public function cancel();
}
4 changes: 2 additions & 2 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace React\Promise;

class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
class RejectedPromise implements PromiseInterface, CancellablePromiseInterface
{
private $reason;

Expand Down Expand Up @@ -47,7 +47,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
throw UnhandledRejectionException::resolve($result->reason);
}

if ($result instanceof ExtendedPromiseInterface) {
if ($result instanceof PromiseInterface) {
$result->done();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

function resolve($promiseOrValue = null)
{
if ($promiseOrValue instanceof ExtendedPromiseInterface) {
if ($promiseOrValue instanceof PromiseInterface) {
return $promiseOrValue;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/CancellationQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function rethrowsExceptionsThrownFromCancel()
$this->setExpectedException('\Exception', 'test');

$mock = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock
->expects($this->once())
Expand Down
6 changes: 3 additions & 3 deletions tests/FunctionAnyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public function shouldNotRelyOnArryIndexesWhenUnwrappingToASingleResolutionValue
public function shouldCancelInputArrayPromises()
{
$mock1 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock1
->expects($this->once())
->method('cancel');

$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock2
->expects($this->once())
Expand All @@ -128,7 +128,7 @@ public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfill
$deferred->resolve();

$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock2
->expects($this->never())
Expand Down
10 changes: 8 additions & 2 deletions tests/FunctionMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,21 @@ public function shouldRejectWhenInputContainsRejection()
public function shouldCancelInputArrayPromises()
{
$mock1 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock1
->method('then')
->will($this->returnSelf());
$mock1
->expects($this->once())
->method('cancel');

$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock2
->method('then')
->will($this->returnSelf());
$mock2
->expects($this->once())
->method('cancel');
Expand Down
8 changes: 4 additions & 4 deletions tests/FunctionRaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ public function shouldRejectIfFirstSettledPromiseRejects()
public function shouldCancelInputArrayPromises()
{
$mock1 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock1
->expects($this->once())
->method('cancel');

$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock2
->expects($this->once())
Expand All @@ -124,7 +124,7 @@ public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfill
$deferred->resolve();

$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock2
->expects($this->never())
Expand All @@ -145,7 +145,7 @@ public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects
$deferred->reject();

$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock2
->expects($this->never())
Expand Down
10 changes: 8 additions & 2 deletions tests/FunctionReduceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,21 @@ public function shouldProvideCorrectBasisValue()
public function shouldCancelInputArrayPromises()
{
$mock1 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock1
->method('then')
->will($this->returnSelf());
$mock1
->expects($this->once())
->method('cancel');

$mock2 = $this
->getMockBuilder('React\Promise\CancellablePromiseInterface')
->getMockBuilder('React\Promise\PromiseInterface')
->getMock();
$mock2
->method('then')
->will($this->returnSelf());
$mock2
->expects($this->once())
->method('cancel');
Expand Down
Loading