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 be0ab64
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 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
14 changes: 7 additions & 7 deletions src/Observable.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,25 @@ public function mergeAll(): Observable
/**
* Converts an array to an observable sequence
*
* @param array $array
* @param array<T> $array
* @param SchedulerInterface $scheduler
* @return ArrayObservable
* @return ObservableInterface<T>
*
* @demo fromArray/fromArray.php
* @operator
* @reactivex from
*/
public static function fromArray(array $array, SchedulerInterface $scheduler = null): ArrayObservable
public static function fromArray(array $array, SchedulerInterface $scheduler = null): ObservableInterface
{
return new ArrayObservable($array, $scheduler ?: Scheduler::getDefault());
}

/**
* Converts an Iterator into an observable sequence
*
* @param \Iterator $iterator
* @param \Iterator<T> $iterator
* @param SchedulerInterface $scheduler
* @return IteratorObservable
* @return ObservableInterface<T>
*
* @demo iterator/iterator.php
* @operator
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
1 change: 1 addition & 0 deletions src/Observable/ArrayObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Rx\DisposableInterface;
use Rx\Observable;
use Rx\ObservableInterface;
use Rx\ObserverInterface;
use Rx\SchedulerInterface;

Expand Down
5 changes: 5 additions & 0 deletions src/Observable/IteratorObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

use Rx\DisposableInterface;
use Rx\Observable;
use Rx\ObservableInterface;
use Rx\ObserverInterface;
use Rx\SchedulerInterface;

/**
* @template T
* @template-implements ObservableInterface<T>
*/
class IteratorObservable extends Observable
{
private $items;
Expand Down
19 changes: 11 additions & 8 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,12 +10,16 @@
use Rx\Observable\AnonymousObservable;
use Rx\Subject\AsyncSubject;
use React\Promise\Deferred;
use Throwable;

/**
* @template T
*/
final class Promise
{
/**
* @param mixed $value
* @return ReactPromise A promise resolved to $value
* @param T $value
* @return PromiseInterface<T> A promise resolved to $value
*/
public static function resolved($value): ReactPromise
{
Expand All @@ -27,21 +30,21 @@ public static function resolved($value): ReactPromise

/**
* @param mixed $exception
* @return ReactPromise A promise rejected with $exception
* @return PromiseInterface<never> A promise rejected with $exception
*/
public static function rejected($exception): ReactPromise
{
$d = new Deferred();
$d->reject($exception);
$d->reject($exception instanceof Throwable ? $exception : new RejectedPromiseException($exception));
return $d->promise();
}

/**
* Converts an existing observable sequence to React Promise
*
* @param ObservableInterface $observable
* @param ObservableInterface<T> $observable
* @param Deferred $deferred
* @return ReactPromise
* @return ReactPromise<T>
* @throws \InvalidArgumentException
*/
public static function fromObservable(ObservableInterface $observable, Deferred $deferred = null): ReactPromise
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 be0ab64

Please sign in to comment.