1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 18:38:17 +01:00
amp/lib/Emitter.php

59 lines
1.3 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;
/**
* Emitter is a container for an iterator that can emit values using the emit() method and completed using the
* complete() and fail() methods of this object. The contained iterator may be accessed using the iterate()
* method. This object should not be part of a public API, but used internally to create and emit values to an
* iterator.
*/
final class Emitter {
/** @var \Amp\Iterator */
private $iterator;
public function __construct() {
$this->iterator = new class implements Iterator {
use Internal\Producer {
emit as public;
complete as public;
fail as public;
2017-01-04 02:10:27 +01:00
}
};
}
2017-01-04 02:10:27 +01:00
/**
* @return \Amp\Promise
*/
public function iterate(): Iterator {
return $this->iterator;
}
2017-01-04 02:10:27 +01:00
/**
* Emits a value to the iterator.
*
* @param mixed $value
*
* @return \Amp\Promise
*/
public function emit($value): Promise {
return $this->iterator->emit($value);
}
2017-01-04 02:10:27 +01:00
/**
* Completes the iterator.
*/
public function complete() {
$this->iterator->complete();
}
/**
* Fails the iterator with the given reason.
*
* @param \Throwable $reason
*/
public function fail(\Throwable $reason) {
$this->iterator->fail($reason);
2016-05-24 18:47:14 +02:00
}
}