From cba57dd81effee3d171d89aa5a4187d98afdf81a Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 7 Jan 2017 12:24:40 -0600 Subject: [PATCH] Simplify PrivatePromise and PrivateStream --- lib/Internal/PrivatePromise.php | 26 +++++----------------- lib/Internal/PrivateStream.php | 39 ++++++--------------------------- 2 files changed, 13 insertions(+), 52 deletions(-) diff --git a/lib/Internal/PrivatePromise.php b/lib/Internal/PrivatePromise.php index 6710275..468c754 100644 --- a/lib/Internal/PrivatePromise.php +++ b/lib/Internal/PrivatePromise.php @@ -2,6 +2,7 @@ namespace Amp\Internal; +use Amp\CallableMaker; use AsyncInterop\Promise; /** @@ -10,30 +11,15 @@ use AsyncInterop\Promise; * @internal */ final class PrivatePromise implements Promise { - use Placeholder; + use CallableMaker, Placeholder; /** * @param callable(callable $resolve, callable $reject): void $resolver */ public function __construct(callable $resolver) { - /** - * Resolves the promise with the given promise or value. - * - * @param mixed $value - */ - $resolve = function ($value = null) { - $this->resolve($value); - }; - - /** - * Fails the promise with the given exception. - * - * @param \Throwable $reason - */ - $fail = function (\Throwable $reason) { - $this->fail($reason); - }; - - $resolver($resolve, $fail); + $resolver( + $this->callableFromInstanceMethod("resolve"), + $this->callableFromInstanceMethod("fail") + ); } } diff --git a/lib/Internal/PrivateStream.php b/lib/Internal/PrivateStream.php index f4952e6..f51ec83 100644 --- a/lib/Internal/PrivateStream.php +++ b/lib/Internal/PrivateStream.php @@ -2,8 +2,8 @@ namespace Amp\Internal; +use Amp\CallableMaker; use Amp\Stream; -use AsyncInterop\Promise; /** * An stream that cannot externally emit values. Used by Emitter in development mode. @@ -11,41 +11,16 @@ use AsyncInterop\Promise; * @internal */ final class PrivateStream implements Stream { - use Producer; + use CallableMaker, Producer; /** * @param callable(callable $emit, callable $complete, callable $fail): void $producer */ public function __construct(callable $producer) { - /** - * Emits a value from the stream. - * - * @param mixed $value - * - * @return \AsyncInterop\Promise - */ - $emit = function ($value = null): Promise { - return $this->emit($value); - }; - - /** - * Completes the stream with the given value. - * - * @param mixed $value - */ - $resolve = function ($value = null) { - $this->resolve($value); - }; - - /** - * Fails the stream with the given exception. - * - * @param \Throwable $reason - */ - $fail = function (\Throwable $reason) { - $this->fail($reason); - }; - - $producer($emit, $resolve, $fail); + $producer( + $this->callableFromInstanceMethod("emit"), + $this->callableFromInstanceMethod("resolve"), + $this->callableFromInstanceMethod("fail") + ); } }