1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00
amp/lib/Emitter.php

51 lines
996 B
PHP
Raw Normal View History

2016-05-24 18:47:14 +02:00
<?php
namespace Amp;
use Interop\Async\Loop;
class Emitter implements Observable {
/**
* @var callable|null
*/
private $emitter;
/**
* @var \Amp\Internal\EmitQueue
*/
private $queue;
/**
* @param callable $emitter
*/
public function __construct(callable $emitter) {
$this->emitter = $emitter;
$this->queue = new Internal\EmitQueue;
}
/**
* {@inheritdoc}
*/
public function dispose() {
$this->emitter = null;
$this->queue->dispose();
}
/**
* {@inheritdoc}
*/
public function getIterator() {
if ($this->emitter !== null) {
$emitter = $this->emitter;
$this->emitter = null;
// Asynchronously start the emitter.
Loop::defer(function () use ($emitter) {
$this->queue->start($emitter);
});
}
return new Internal\EmitterIterator($this->queue);
}
}