Skip to content

Commit 08aaad5

Browse files
authored
Merge pull request #75 from reactphp/merge-interfaces
Merge promise interfaces
2 parents 719b4c9 + f20713c commit 08aaad5

20 files changed

+96
-185
lines changed

CHANGELOG.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,32 @@ CHANGELOG for 3.x
1212
`some()`, `map()`, `reduce()`) now require an array of promises or values
1313
as input. Before, arrays and promises which resolve to an array were
1414
supported, other input types resolved to empty arrays or `null` (#35).
15+
* BC break: The interfaces `PromiseInterface`, `ExtendedPromiseInterface`
16+
and `CancellablePromiseInterface` have been merged into a single
17+
`PromiseInterface` (#75).
18+
19+
Please note, that the following code (which has been commonly used to
20+
conditionally cancel a promise) is not longer possible:
21+
22+
```php
23+
if ($promise instanceof CancellablePromiseInterface) {
24+
$promise->cancel();
25+
}
26+
```
27+
28+
If only supporting react/promise >= 3.0, it can be simply changed to:
29+
30+
```php
31+
if ($promise instanceof PromiseInterface) {
32+
$promise->cancel();
33+
}
34+
```
35+
36+
If also react/promise < 3.0 must be supported, the following code can be
37+
used:
38+
39+
```php
40+
if ($promise instanceof PromiseInterface) {
41+
\React\Promise\resolve($promise)->cancel();
42+
}
43+
```

README.md

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ Table of Contents
2525
* [Deferred::reject()](#deferredreject)
2626
* [PromiseInterface](#promiseinterface)
2727
* [PromiseInterface::then()](#promiseinterfacethen)
28-
* [ExtendedPromiseInterface](#extendedpromiseinterface)
29-
* [ExtendedPromiseInterface::done()](#extendedpromiseinterfacedone)
30-
* [ExtendedPromiseInterface::otherwise()](#extendedpromiseinterfaceotherwise)
31-
* [ExtendedPromiseInterface::always()](#extendedpromiseinterfacealways)
32-
* [CancellablePromiseInterface](#cancellablepromiseinterface)
33-
* [CancellablePromiseInterface::cancel()](#cancellablepromiseinterfacecancel)
28+
* [PromiseInterface::done()](#promiseinterfacedone)
29+
* [PromiseInterface::otherwise()](#promiseinterfaceotherwise)
30+
* [PromiseInterface::always()](#promiseinterfacealways)
31+
* [PromiseInterface::cancel()](#promiseinterfacecancel)
3432
* [Promise](#promise-1)
3533
* [FulfilledPromise](#fulfilledpromise)
3634
* [RejectedPromise](#rejectedpromise)
@@ -193,22 +191,10 @@ the same call to `then()`:
193191

194192
* [resolve()](#resolve) - Creating a resolved promise
195193
* [reject()](#reject) - Creating a rejected promise
196-
* [ExtendedPromiseInterface::done()](#extendedpromiseinterfacedone)
194+
* [PromiseInterface::done()](#promiseinterfacedone)
197195
* [done() vs. then()](#done-vs-then)
198196

199-
### ExtendedPromiseInterface
200-
201-
The ExtendedPromiseInterface extends the PromiseInterface with useful shortcut
202-
and utility methods which are not part of the Promises/A specification.
203-
204-
#### Implementations
205-
206-
* [Promise](#promise-1)
207-
* [FulfilledPromise](#fulfilledpromise)
208-
* [RejectedPromise](#rejectedpromise)
209-
* [LazyPromise](#lazypromise)
210-
211-
#### ExtendedPromiseInterface::done()
197+
#### PromiseInterface::done()
212198

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

231-
#### ExtendedPromiseInterface::otherwise()
217+
#### PromiseInterface::otherwise()
232218

233219
```php
234220
$promise->otherwise(callable $onRejected);
@@ -254,7 +240,7 @@ $promise
254240
)};
255241
```
256242

257-
#### ExtendedPromiseInterface::always()
243+
#### PromiseInterface::always()
258244

259245
```php
260246
$newPromise = $promise->always(callable $onFulfilledOrRejected);
@@ -301,13 +287,7 @@ return doSomething()
301287
->always('cleanup');
302288
```
303289

304-
### CancellablePromiseInterface
305-
306-
A cancellable promise provides a mechanism for consumers to notify the creator
307-
of the promise that they are not longer interested in the result of an
308-
operation.
309-
310-
#### CancellablePromiseInterface::cancel()
290+
#### PromiseInterface::cancel()
311291

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

322-
#### Implementations
323-
324-
* [Promise](#promise-1)
325-
* [FulfilledPromise](#fulfilledpromise)
326-
* [RejectedPromise](#rejectedpromise)
327-
* [LazyPromise](#lazypromise)
328-
329302
### Promise
330303

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

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

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

445413
```php

src/CancellablePromiseInterface.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/ExtendedPromiseInterface.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/FulfilledPromise.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace React\Promise;
44

5-
class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
5+
class FulfilledPromise implements PromiseInterface
66
{
77
private $value;
88

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

46-
if ($result instanceof ExtendedPromiseInterface) {
46+
if ($result instanceof PromiseInterface) {
4747
$result->done();
4848
}
4949
});

src/LazyPromise.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace React\Promise;
44

5-
class LazyPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
5+
class LazyPromise implements PromiseInterface
66
{
77
private $factory;
88
private $promise;

src/Promise.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace React\Promise;
44

5-
class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface
5+
class Promise implements PromiseInterface
66
{
77
private $canceller;
88
private $result;
@@ -45,7 +45,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
4545
return $this->result()->done($onFulfilled, $onRejected);
4646
}
4747

48-
$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected) {
48+
$this->handlers[] = function (PromiseInterface $promise) use ($onFulfilled, $onRejected) {
4949
$promise
5050
->done($onFulfilled, $onRejected);
5151
};
@@ -90,7 +90,7 @@ public function cancel()
9090
private function resolver(callable $onFulfilled = null, callable $onRejected = null)
9191
{
9292
return function ($resolve, $reject) use ($onFulfilled, $onRejected) {
93-
$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject) {
93+
$this->handlers[] = function (PromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject) {
9494
$promise
9595
->then($onFulfilled, $onRejected)
9696
->done($resolve, $reject);
@@ -116,7 +116,7 @@ private function reject($reason = null)
116116
$this->settle(reject($reason));
117117
}
118118

119-
private function settle(ExtendedPromiseInterface $result)
119+
private function settle(PromiseInterface $result)
120120
{
121121
if ($result instanceof LazyPromise) {
122122
$result = $result->promise();

src/PromiseInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,24 @@ interface PromiseInterface
88
* @return PromiseInterface
99
*/
1010
public function then(callable $onFulfilled = null, callable $onRejected = null);
11+
12+
/**
13+
* @return void
14+
*/
15+
public function done(callable $onFulfilled = null, callable $onRejected = null);
16+
17+
/**
18+
* @return PromiseInterface
19+
*/
20+
public function otherwise(callable $onRejected);
21+
22+
/**
23+
* @return PromiseInterface
24+
*/
25+
public function always(callable $onFulfilledOrRejected);
26+
27+
/**
28+
* @return void
29+
*/
30+
public function cancel();
1131
}

src/RejectedPromise.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace React\Promise;
44

5-
class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
5+
class RejectedPromise implements PromiseInterface
66
{
77
private $reason;
88

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

50-
if ($result instanceof ExtendedPromiseInterface) {
50+
if ($result instanceof PromiseInterface) {
5151
$result->done();
5252
}
5353
});

src/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
function resolve($promiseOrValue = null)
66
{
7-
if ($promiseOrValue instanceof ExtendedPromiseInterface) {
7+
if ($promiseOrValue instanceof PromiseInterface) {
88
return $promiseOrValue;
99
}
1010

0 commit comments

Comments
 (0)