1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 13:21:16 +01:00

Remove succeed() and fail()

This commit is contained in:
Aaron Piotrowski 2020-10-04 10:22:51 -05:00
parent 40aab8eef5
commit 1b30909215
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
4 changed files with 11 additions and 83 deletions

View File

@ -4,9 +4,11 @@ namespace Amp\Internal;
use Amp\Deferred;
use Amp\DisposedException;
use Amp\Failure;
use Amp\Loop;
use Amp\Pipeline;
use Amp\Promise;
use Amp\Success;
use React\Promise\PromiseInterface as ReactPromise;
use function Amp\await;
@ -50,7 +52,7 @@ final class EmitSource
*/
public function continue(): mixed
{
return $this->next(Promise\succeed());
return $this->next(new Success);
}
/**
@ -64,7 +66,7 @@ final class EmitSource
throw new \Error("Must initialize async generator by calling continue() first");
}
return $this->next(Promise\succeed($value));
return $this->next(new Success($value));
}
/**
@ -76,7 +78,7 @@ final class EmitSource
throw new \Error("Must initialize async generator by calling continue() first");
}
return $this->next(Promise\fail($exception));
return $this->next(new Failure($exception));
}
/**
@ -101,7 +103,8 @@ final class EmitSource
$value = $this->emittedValues[$position];
unset($this->emittedValues[$position]);
return await(Promise\succeed($value));
// Defer next value to avoid creating a blocking loop.
return await(new Success($value));
}
if ($this->result) {
@ -146,7 +149,7 @@ final class EmitSource
return; // Pipeline already completed or failed.
}
$this->finalize(Promise\fail(new DisposedException), true);
$this->finalize(new Failure(new DisposedException), true);
} finally {
if ($this->disposed && $cancelPending) {
$this->triggerDisposal();
@ -235,7 +238,7 @@ final class EmitSource
$this->triggerDisposal();
}
return Promise\succeed();
return new Success;
}
$this->backPressure[$position] = $deferred = new Deferred;
@ -268,7 +271,7 @@ final class EmitSource
*/
public function complete(): void
{
$this->finalize(Promise\succeed());
$this->finalize(new Success);
}
/**
@ -284,7 +287,7 @@ final class EmitSource
throw new \Error("Cannot fail a pipeline with an instance of " . DisposedException::class);
}
$this->finalize(Promise\fail($exception));
$this->finalize(new Failure($exception));
}
/**

View File

@ -274,49 +274,6 @@ namespace Amp\Promise
});
}
/**
* Returns a successful promise using the given value, which can be anything other than a promise. This function
* optimizes the case where null is used as the value, always returning the same object.
*
* @template TValue
*
* @param mixed $value Anything other than a Promise object.
*
* @psalm-param TValue $value
*
* @return Promise
*
* @psalm-return Promise<TValue>
*
* @throws \Error If a promise is given as the value.
*/
function succeed(mixed $value = null): Promise
{
static $empty;
if ($value === null) {
return $empty ?? ($empty = new Success);
}
return new Success($value);
}
/**
* Returns a failed promise using the given exception.
*
* @template TValue
*
* @param \Throwable $exception
*
* @return Promise
*
* @psalm-return Promise<TValue>
*/
function fail(\Throwable $exception): Promise
{
return new Failure($exception);
}
/**
* @param Promise|ReactPromise $promise Promise to wait for.
*

View File

@ -69,20 +69,4 @@ class FailureTest extends AsyncTestCase
$this->fail("Promise was not failed");
}
public function testFailFunction(): void
{
$exception = new \Exception;
$failure = Promise\fail($exception);
try {
await($failure);
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
return;
}
$this->fail("Promise was not failed");
}
}

View File

@ -86,20 +86,4 @@ class SuccessTest extends AsyncTestCase
sleep(0); // Tick event loop to execute coroutine
$this->assertTrue($invoked);
}
public function testSucceedFunction(): void
{
$value = 1;
$success = Promise\succeed($value);
$this->assertSame($value, await($success));
}
public function testSucceedFunctionWithNull(): void
{
$success = Promise\succeed();
$this->assertSame($success, Promise\succeed());
$this->assertNull(await($success));
}
}