1
0
mirror of https://github.com/danog/amp.git synced 2024-12-03 09:57:51 +01:00
amp/lib/Emitter.php

89 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2016-08-16 06:46:26 +02:00
2016-05-24 18:47:14 +02:00
namespace Amp;
2017-01-04 02:10:27 +01:00
// @codeCoverageIgnoreStart
try {
if (!@\assert(false)) {
development: // PHP 7 development (zend.assertions=1)
/**
2017-04-27 17:51:06 +02:00
* Deferred is a container for an iterator that can emit values using the emit() method and completed using the
2017-05-01 07:29:23 +02:00
* complete() and fail() methods of this object. The contained iterator may be accessed using the iterate()
2017-04-27 17:51:06 +02:00
* method. This object should not be part of a public API, but used internally to create and emit values to an
* iterator.
*/
2017-01-04 02:10:27 +01:00
final class Emitter {
/** @var \Amp\Iterator */
2017-04-27 17:51:06 +02:00
private $iterator;
2017-01-04 02:10:27 +01:00
/** @var callable */
2017-01-04 02:10:27 +01:00
private $emit;
/** @var callable */
2017-04-26 20:14:10 +02:00
private $complete;
/** @var callable */
2017-01-04 02:10:27 +01:00
private $fail;
public function __construct() {
$this->iterator = new class($this->emit, $this->complete, $this->fail) implements Iterator {
use CallableMaker, Internal\Producer;
public function __construct(&$emit, &$complete, &$fail) {
$emit = $this->callableFromInstanceMethod("emit");
$complete = $this->callableFromInstanceMethod("complete");
$fail = $this->callableFromInstanceMethod("fail");
2017-01-04 02:10:27 +01:00
}
};
2017-01-04 02:10:27 +01:00
}
/**
2017-04-27 17:51:06 +02:00
* @return \Amp\Iterator
2017-01-04 02:10:27 +01:00
*/
2017-05-01 07:29:23 +02:00
public function iterate(): Iterator {
2017-04-27 17:51:06 +02:00
return $this->iterator;
2017-01-04 02:10:27 +01:00
}
/**
2017-04-27 17:51:06 +02:00
* Emits a value to the iterator.
2017-01-04 02:10:27 +01:00
*
* @param mixed $value
*
* @return \Amp\Promise
2017-01-04 02:10:27 +01:00
*/
public function emit($value): Promise {
return ($this->emit)($value);
}
/**
2017-04-27 17:51:06 +02:00
* Completes the iterator.
2017-01-04 02:10:27 +01:00
*/
2017-04-26 20:14:10 +02:00
public function complete() {
($this->complete)();
2017-01-04 02:10:27 +01:00
}
2017-01-04 02:10:27 +01:00
/**
2017-04-27 17:51:06 +02:00
* Fails the iterator with the given reason.
2017-01-04 02:10:27 +01:00
*
* @param \Throwable $reason
*/
public function fail(\Throwable $reason) {
($this->fail)($reason);
}
}
} else {
production: // PHP 7 production environment (zend.assertions=0)
/**
2017-04-27 17:51:06 +02:00
* An optimized version of Emitter for production environments that is itself the iterator. Eval is used to
* prevent IDEs and other tools from reporting multiple definitions.
*/
2017-04-16 00:16:50 +02:00
eval('namespace Amp;
2017-04-27 18:18:25 +02:00
final class Emitter implements Iterator {
2017-04-26 20:14:10 +02:00
use Internal\Producer { emit as public; complete as public; fail as public; }
2017-05-01 07:29:23 +02:00
public function iterate(): Iterator { 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