Skip to content

Commit

Permalink
Add support for react/promise v3
Browse files Browse the repository at this point in the history
With v3 come a whole list of [changes](https://github.com/reactphp/promise/releases/tag/v3.0.0)
including end of promise chain detection, the removal of the `CancellablePromiseInterface`
interface, and type templating.
  • Loading branch information
WyriHaximus committed Aug 7, 2023
1 parent 2a2c228 commit 7dec0f8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
],
"require": {
"php": ">=7.0.0",
"react/promise": "~2.2"
"react/promise": "^3 || ~2.2"
},
"require-dev": {
"satooshi/php-coveralls": "~1.0",
"phpunit/phpunit": "^8.5 || ^9",
"react/event-loop": "^1.0 || ^0.5 || ^0.4.2"
"react/event-loop": "^1.0 || ^0.5 || ^0.4.2",
"phpstan/phpstan": "^1.10"
},
"suggest": {
"react/event-loop": "Used for scheduling async operations"
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: max

paths:
- test/types/

fileExtensions:
- php
8 changes: 4 additions & 4 deletions src/Observable.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function mergeAll(): Observable
/**
* Converts an array to an observable sequence
*
* @param array $array
* @param array<mixed> $array
* @param SchedulerInterface $scheduler
* @return ArrayObservable
*
Expand All @@ -312,7 +312,7 @@ public static function fromArray(array $array, SchedulerInterface $scheduler = n
/**
* Converts an Iterator into an observable sequence
*
* @param \Iterator $iterator
* @param \Iterator<mixed> $iterator
* @param SchedulerInterface $scheduler
* @return IteratorObservable
*
Expand Down Expand Up @@ -2049,8 +2049,8 @@ public function finally(callable $callback): Observable
/**
* Converts a promise into an observable
*
* @param PromiseInterface $promise
* @return Observable
* @param PromiseInterface<T> $promise
* @return Observable<T>
* @throws \InvalidArgumentException
*
* @demo promise/fromPromise.php
Expand Down
9 changes: 6 additions & 3 deletions src/React/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Rx\React;

use React\Promise\CancellablePromiseInterface;
use React\Promise\Promise as ReactPromise;
use React\Promise\PromiseInterface;
use Rx\Disposable\CallbackDisposable;
Expand All @@ -11,7 +10,11 @@
use Rx\Observable\AnonymousObservable;
use Rx\Subject\AsyncSubject;
use React\Promise\Deferred;
use Throwable;

/**
* @template T
*/
final class Promise
{
/**
Expand All @@ -32,7 +35,7 @@ public static function resolved($value): ReactPromise
public static function rejected($exception): ReactPromise
{
$d = new Deferred();
$d->reject($exception);
$d->reject($exception instanceof Throwable ? $exception : new RejectedPromiseException($exception));
return $d->promise();
}

Expand Down Expand Up @@ -94,7 +97,7 @@ function ($error) use ($subject) {
$disp = $subject->subscribe($observer);
return new CallbackDisposable(function () use ($p, $disp) {
$disp->dispose();
if ($p instanceof CancellablePromiseInterface) {
if (\method_exists($p, 'cancel')) {
$p->cancel();
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/Rx/Functional/Promise/FromPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function () {
*/
public function from_promise_failure()
{
$p = \React\Promise\reject('error');
$p = \React\Promise\reject(new RejectedPromiseException('error'));

$source = Observable::fromPromise($p);

Expand Down

0 comments on commit 7dec0f8

Please sign in to comment.