1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00

Limit Future::map(), catch(), and finally() to Closure

This commit is contained in:
Aaron Piotrowski 2021-12-02 17:49:21 -06:00
parent cfb527a9fc
commit 18b0348ddc
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB

View File

@ -120,15 +120,15 @@ final class Future
* *
* @template Tr * @template Tr
* *
* @param callable(T):Tr $onComplete * @param \Closure(T):Tr $map
* *
* @return Future * @return Future
*/ */
public function map(callable $onComplete): self public function map(\Closure $map): self
{ {
$state = new FutureState(); $state = new FutureState();
$this->state->subscribe(static function (?\Throwable $error, mixed $value) use ($state, $onComplete): void { $this->state->subscribe(static function (?\Throwable $error, mixed $value) use ($state, $map): void {
if ($error) { if ($error) {
$state->error($error); $state->error($error);
return; return;
@ -136,7 +136,7 @@ final class Future
try { try {
/** @var T $value */ /** @var T $value */
$state->complete($onComplete($value)); $state->complete($map($value));
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$state->error($exception); $state->error($exception);
} }
@ -151,22 +151,22 @@ final class Future
* *
* @template Tr * @template Tr
* *
* @param callable(\Throwable):Tr $onError * @param \Closure(\Throwable):Tr $catch
* *
* @return Future * @return Future
*/ */
public function catch(callable $onError): self public function catch(\Closure $catch): self
{ {
$state = new FutureState(); $state = new FutureState();
$this->state->subscribe(static function (?\Throwable $error, mixed $value) use ($state, $onError): void { $this->state->subscribe(static function (?\Throwable $error, mixed $value) use ($state, $catch): void {
if (!$error) { if (!$error) {
$state->complete($value); $state->complete($value);
return; return;
} }
try { try {
$state->complete($onError($error)); $state->complete($catch($error));
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$state->error($exception); $state->error($exception);
} }
@ -176,21 +176,21 @@ final class Future
} }
/** /**
* Attaches a callback that is always invoked when the future is settled. The returned future resolves with the * Attaches a callback that is always invoked when the future is completed. The returned future resolves with the
* same value as this future once the callback has finished execution. If the callback throws, the returned future * same value as this future once the callback has finished execution. If the callback throws, the returned future
* will error with the thrown exception. * will error with the thrown exception.
* *
* @param callable():void $onSettle * @param \Closure():void $finally
* *
* @return Future<T> * @return Future<T>
*/ */
public function finally(callable $onSettle): self public function finally(\Closure $finally): self
{ {
$state = new FutureState(); $state = new FutureState();
$this->state->subscribe(static function (?\Throwable $error, mixed $value) use ($state, $onSettle): void { $this->state->subscribe(static function (?\Throwable $error, mixed $value) use ($state, $finally): void {
try { try {
$onSettle(); $finally();
if ($error) { if ($error) {
$state->error($error); $state->error($error);