1
0
mirror of https://github.com/danog/amp.git synced 2024-12-03 18:07:57 +01:00

Add deprecations

This commit is contained in:
Niklas Keller 2020-09-29 21:25:42 +02:00
parent 55af98a8ba
commit 3219618fd8

View File

@ -2,6 +2,7 @@
namespace Amp namespace Amp
{ {
use React\Promise\PromiseInterface as ReactPromise; use React\Promise\PromiseInterface as ReactPromise;
/** /**
@ -56,13 +57,13 @@ namespace Amp
{ {
$deferred = new Deferred; $deferred = new Deferred;
Loop::defer(static fn () => \Fiber::run(static function () use ($deferred, $callback, $args): void { Loop::defer(static fn() => \Fiber::run(static function () use ($deferred, $callback, $args): void {
try { try {
$deferred->resolve($callback(...$args)); $deferred->resolve($callback(...$args));
} catch (\Throwable $e) { } catch (\Throwable $exception) {
$deferred->fail($e); $deferred->fail($exception);
} }
}, ...$args)); }));
return $deferred->promise(); return $deferred->promise();
} }
@ -73,12 +74,12 @@ namespace Amp
* *
* @param callable $callback Green thread to create each time the function returned is invoked. * @param callable $callback Green thread to create each time the function returned is invoked.
* *
* @return callable(mixed ...$args):Promise Creates a new green thread each time the returned function is invoked. The * @return callable(mixed ...$args):Promise Creates a new green thread each time the returned function is invoked.
* arguments given to the returned function are passed through to the callable. * The arguments given to the returned function are passed through to the callable.
*/ */
function asyncCallable(callable $callback): callable function asyncCallable(callable $callback): callable
{ {
return static fn (mixed ...$args): Promise => async($callback, ...$args); return static fn(mixed ...$args): Promise => async($callback, ...$args);
} }
/** /**
@ -108,11 +109,13 @@ namespace Amp
* @see asyncCoroutine() * @see asyncCoroutine()
* *
* @psalm-suppress InvalidReturnType * @psalm-suppress InvalidReturnType
*
* @deprecated No longer necessary with ext-fiber
*/ */
function coroutine(callable $callback): callable function coroutine(callable $callback): callable
{ {
/** @psalm-suppress InvalidReturnStatement */ /** @psalm-suppress InvalidReturnStatement */
return static fn (...$args): Promise => call($callback, ...$args); return static fn(...$args): Promise => call($callback, ...$args);
} }
/** /**
@ -128,10 +131,12 @@ namespace Amp
* @psalm-return callable(mixed...): void * @psalm-return callable(mixed...): void
* *
* @see coroutine() * @see coroutine()
*
* @deprecated No longer necessary with ext-fiber
*/ */
function asyncCoroutine(callable $callback): callable function asyncCoroutine(callable $callback): callable
{ {
return static fn (...$args) => Promise\rethrow(call($callback, ...$args)); return static fn(...$args) => Promise\rethrow(call($callback, ...$args));
} }
/** /**
@ -155,6 +160,8 @@ namespace Amp
* @psalm-return (T is Promise ? Promise<TPromise> : (T is \Generator ? (TGenerator is Promise ? Promise<TGeneratorPromise> : Promise<TGeneratorReturn>) : Promise<TReturn>)) * @psalm-return (T is Promise ? Promise<TPromise> : (T is \Generator ? (TGenerator is Promise ? Promise<TGeneratorPromise> : Promise<TGeneratorReturn>) : Promise<TReturn>))
* *
* @formatter:on * @formatter:on
*
* @deprecated No longer necessary with ext-fiber
*/ */
function call(callable $callback, ...$args): Promise function call(callable $callback, ...$args): Promise
{ {
@ -187,6 +194,8 @@ namespace Amp
* @param mixed ...$args Arguments to pass to the function. * @param mixed ...$args Arguments to pass to the function.
* *
* @return void * @return void
*
* @deprecated No longer necessary with ext-fiber
*/ */
function asyncCall(callable $callback, ...$args): void function asyncCall(callable $callback, ...$args): void
{ {
@ -222,6 +231,7 @@ namespace Amp
namespace Amp\Promise namespace Amp\Promise
{ {
use Amp\Deferred; use Amp\Deferred;
use Amp\Failure; use Amp\Failure;
use Amp\Loop; use Amp\Loop;
@ -303,11 +313,6 @@ namespace Amp\Promise
} }
/** /**
* @deprecated Use {@see await()} instead.
*
* @template TPromise
* @template T as Promise<TPromise>|ReactPromise
*
* @param Promise|ReactPromise $promise Promise to wait for. * @param Promise|ReactPromise $promise Promise to wait for.
* *
* @return mixed Promise success value. * @return mixed Promise success value.
@ -316,6 +321,11 @@ namespace Amp\Promise
* @psalm-return (T is Promise ? TPromise : mixed) * @psalm-return (T is Promise ? TPromise : mixed)
* *
* @throws \Throwable Promise failure reason. * @throws \Throwable Promise failure reason.
*
* @deprecated Use {@see await()} instead.
*
* @template TPromise
* @template T as Promise<TPromise>|ReactPromise
*/ */
function wait(Promise|ReactPromise $promise): mixed function wait(Promise|ReactPromise $promise): mixed
{ {
@ -654,6 +664,7 @@ namespace Amp\Promise
namespace Amp\Iterator namespace Amp\Iterator
{ {
use Amp\Delayed; use Amp\Delayed;
use Amp\Emitter; use Amp\Emitter;
use Amp\Iterator; use Amp\Iterator;
@ -869,6 +880,7 @@ namespace Amp\Iterator
namespace Amp\Pipeline namespace Amp\Pipeline
{ {
use Amp\AsyncGenerator; use Amp\AsyncGenerator;
use Amp\Pipeline; use Amp\Pipeline;
use Amp\PipelineSource; use Amp\PipelineSource;
@ -877,8 +889,8 @@ namespace Amp\Pipeline
use function Amp\await; use function Amp\await;
use function Amp\call; use function Amp\call;
use function Amp\coroutine; use function Amp\coroutine;
use function Amp\delay;
use function Amp\Internal\createTypeError; use function Amp\Internal\createTypeError;
use function Amp\sleep;
/** /**
* Creates a pipeline from the given iterable, emitting the each value. The iterable may contain promises. If any * Creates a pipeline from the given iterable, emitting the each value. The iterable may contain promises. If any
@ -902,7 +914,7 @@ namespace Amp\Pipeline
return new AsyncGenerator(static function () use ($iterable, $delay): \Generator { return new AsyncGenerator(static function () use ($iterable, $delay): \Generator {
foreach ($iterable as $value) { foreach ($iterable as $value) {
if ($delay) { if ($delay) {
await(delay($delay)); sleep($delay);
} }
if ($value instanceof Promise || $value instanceof ReactPromise) { if ($value instanceof Promise || $value instanceof ReactPromise) {
@ -1039,10 +1051,10 @@ namespace Amp\Pipeline
*/ */
function discard(Pipeline $pipeline): Promise function discard(Pipeline $pipeline): Promise
{ {
return call(static function () use ($stream): \Generator { return call(static function () use ($pipeline): \Generator {
$count = 0; $count = 0;
while (null !== yield $stream->continue()) { while (null !== yield $pipeline->continue()) {
$count++; $count++;
} }