1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 05:11:42 +01:00

Simplify PrivatePromise and PrivateStream

This commit is contained in:
Aaron Piotrowski 2017-01-07 12:24:40 -06:00
parent 7e40a05dc7
commit cba57dd81e
2 changed files with 13 additions and 52 deletions

View File

@ -2,6 +2,7 @@
namespace Amp\Internal; namespace Amp\Internal;
use Amp\CallableMaker;
use AsyncInterop\Promise; use AsyncInterop\Promise;
/** /**
@ -10,30 +11,15 @@ use AsyncInterop\Promise;
* @internal * @internal
*/ */
final class PrivatePromise implements Promise { final class PrivatePromise implements Promise {
use Placeholder; use CallableMaker, Placeholder;
/** /**
* @param callable(callable $resolve, callable $reject): void $resolver * @param callable(callable $resolve, callable $reject): void $resolver
*/ */
public function __construct(callable $resolver) { public function __construct(callable $resolver) {
/** $resolver(
* Resolves the promise with the given promise or value. $this->callableFromInstanceMethod("resolve"),
* $this->callableFromInstanceMethod("fail")
* @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);
} }
} }

View File

@ -2,8 +2,8 @@
namespace Amp\Internal; namespace Amp\Internal;
use Amp\CallableMaker;
use Amp\Stream; use Amp\Stream;
use AsyncInterop\Promise;
/** /**
* An stream that cannot externally emit values. Used by Emitter in development mode. * An stream that cannot externally emit values. Used by Emitter in development mode.
@ -11,41 +11,16 @@ use AsyncInterop\Promise;
* @internal * @internal
*/ */
final class PrivateStream implements Stream { final class PrivateStream implements Stream {
use Producer; use CallableMaker, Producer;
/** /**
* @param callable(callable $emit, callable $complete, callable $fail): void $producer * @param callable(callable $emit, callable $complete, callable $fail): void $producer
*/ */
public function __construct(callable $producer) { public function __construct(callable $producer) {
/** $producer(
* Emits a value from the stream. $this->callableFromInstanceMethod("emit"),
* $this->callableFromInstanceMethod("resolve"),
* @param mixed $value $this->callableFromInstanceMethod("fail")
* );
* @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);
} }
} }