2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-24 18:47:14 +02:00
|
|
|
namespace Amp;
|
|
|
|
|
2017-01-07 13:47:45 +01:00
|
|
|
use AsyncInterop\Promise;
|
2016-08-14 16:57:17 +02:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
try {
|
2017-01-04 17:24:51 +01:00
|
|
|
if (!@\assert(false)) {
|
|
|
|
development: // PHP 7 development (zend.assertions=1)
|
|
|
|
/**
|
|
|
|
* Deferred is a container for a stream that can emit values using the emit() method and resolved using the
|
|
|
|
* resolve() and fail() methods of this object. The contained stream may be accessed using the stream() method.
|
|
|
|
* This object should not be part of a public API, but used internally to create and emit values from a stream.
|
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
final class Emitter {
|
|
|
|
/**
|
|
|
|
* @var \Amp\Stream
|
|
|
|
*/
|
|
|
|
private $stream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $emit;
|
2017-01-04 17:24:51 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $resolve;
|
2017-01-04 17:24:51 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $fail;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->stream = new Internal\PrivateStream(
|
|
|
|
function (callable $emit, callable $resolve, callable $fail) {
|
|
|
|
$this->emit = $emit;
|
|
|
|
$this->resolve = $resolve;
|
|
|
|
$this->fail = $fail;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Amp\Stream
|
|
|
|
*/
|
|
|
|
public function stream(): Stream {
|
|
|
|
return $this->stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emits a value from the stream.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2017-01-07 13:47:45 +01:00
|
|
|
* @return \AsyncInterop\Promise
|
2017-01-04 02:10:27 +01:00
|
|
|
*/
|
|
|
|
public function emit($value): Promise {
|
|
|
|
return ($this->emit)($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves the stream with the given value.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function resolve($value = null) {
|
|
|
|
($this->resolve)($value);
|
|
|
|
}
|
2016-06-01 18:19:19 +02:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* Fails the stream with the given reason.
|
|
|
|
*
|
|
|
|
* @param \Throwable $reason
|
|
|
|
*/
|
|
|
|
public function fail(\Throwable $reason) {
|
|
|
|
($this->fail)($reason);
|
|
|
|
}
|
2016-06-01 18:19:19 +02:00
|
|
|
}
|
2017-01-04 17:24:51 +01:00
|
|
|
} else {
|
|
|
|
production: // PHP 7 production environment (zend.assertions=0)
|
|
|
|
/**
|
|
|
|
* An optimized version of Emitter for production environments that is itself the stream.
|
|
|
|
*/
|
|
|
|
final class Emitter implements Stream {
|
|
|
|
use Internal\Producer {
|
|
|
|
emit as public;
|
|
|
|
resolve as public;
|
|
|
|
fail as public;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Amp\Stream
|
|
|
|
*/
|
|
|
|
public function stream(): Stream {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
} catch (\AssertionError $exception) {
|
|
|
|
goto development; // zend.assertions=1 and assert.exception=1, use development definition.
|
|
|
|
} // @codeCoverageIgnoreEnd
|