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:
parent
40aab8eef5
commit
1b30909215
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user