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;
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")
);
}
}

View File

@ -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")
);
}
}