1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00
amp/lib/Observer.php

196 lines
5.3 KiB
PHP
Raw Normal View History

2016-08-17 22:25:54 -05:00
<?php declare(strict_types = 1);
2016-08-15 23:46:26 -05:00
2016-05-24 11:47:14 -05:00
namespace Amp;
2016-08-11 14:35:58 -05:00
use Interop\Async\Awaitable;
2016-06-01 11:37:12 -05:00
/**
* Asynchronous iterator that can be used within a coroutine to iterate over the emitted values from an Observable.
*
* Example:
* $observer = new Observer($observable); // $observable is an instance of \Amp\Observable
* while (yield $observer->next()) {
* $emitted = $observer->getCurrent();
* }
* $result = $observer->getResult();
*/
class Observer {
2016-08-17 22:25:54 -05:00
/** @var mixed[] */
2016-05-29 23:49:44 -05:00
private $values = [];
2016-08-17 22:25:54 -05:00
/** @var \Amp\Deferred[] */
private $deferreds = [];
2016-05-29 11:35:09 -05:00
2016-08-17 22:25:54 -05:00
/** @var int */
2016-05-29 23:49:44 -05:00
private $position = -1;
2016-08-17 22:25:54 -05:00
/** @var \Amp\Deferred|null */
2016-05-29 23:49:44 -05:00
private $deferred;
2016-05-29 11:35:09 -05:00
2016-08-17 22:25:54 -05:00
/** @var bool */
2016-05-30 10:24:03 -05:00
private $resolved = false;
2016-05-29 11:35:09 -05:00
2016-08-17 22:25:54 -05:00
/** @var mixed */
2016-05-29 23:49:44 -05:00
private $result;
2016-05-29 11:35:09 -05:00
2016-08-17 22:25:54 -05:00
/** @var \Throwable|null */
2016-05-29 11:35:09 -05:00
private $exception;
2016-05-27 15:44:01 -05:00
/**
* @param \Amp\Observable $observable
*/
public function __construct(Observable $observable) {
$deferred = &$this->deferred;
$values = &$this->values;
$deferreds = &$this->deferreds;
$resolved = &$this->resolved;
2016-05-29 11:35:09 -05:00
2016-08-17 22:25:54 -05:00
$observable->subscribe(static function ($value) use (&$deferred, &$values, &$deferreds, &$resolved) {
2016-05-29 23:49:44 -05:00
$values[] = $value;
2016-08-17 22:25:54 -05:00
$deferreds[] = $pressure = new Deferred;
2016-05-29 11:35:09 -05:00
2016-05-29 23:49:44 -05:00
if ($deferred !== null) {
$temp = $deferred;
$deferred = null;
$temp->resolve(true);
2016-05-29 23:49:44 -05:00
}
2016-05-29 11:35:09 -05:00
if ($resolved) {
return null;
}
2016-08-17 22:25:54 -05:00
return $pressure->getAwaitable();
2016-05-29 11:35:09 -05:00
});
2016-08-17 22:25:54 -05:00
$result = &$this->result;
$error = &$this->exception;
2016-05-29 11:35:09 -05:00
$observable->when(static function ($exception, $value) use (&$deferred, &$result, &$error, &$resolved) {
2016-05-30 10:24:03 -05:00
$resolved = true;
2016-05-29 11:35:09 -05:00
if ($exception) {
2016-05-29 23:49:44 -05:00
$result = null;
2016-05-29 11:35:09 -05:00
$error = $exception;
2016-05-29 23:49:44 -05:00
if ($deferred !== null) {
2016-05-29 11:35:09 -05:00
$deferred->fail($exception);
}
return;
}
2016-05-29 23:49:44 -05:00
$result = $value;
if ($deferred !== null) {
2016-05-29 11:35:09 -05:00
$deferred->resolve(false);
}
});
2016-05-27 15:44:01 -05:00
}
/**
2016-08-17 22:25:54 -05:00
* Marks the observer as resolved to relieve back-pressure on the observable.
2016-05-27 15:44:01 -05:00
*/
public function __destruct() {
2016-08-17 21:11:03 -05:00
$this->resolved = true;
2016-08-17 22:25:54 -05:00
foreach ($this->deferreds as $deferred) {
$deferred->resolve();
2016-05-29 11:35:09 -05:00
}
2016-05-27 15:44:01 -05:00
}
2016-05-24 11:47:14 -05:00
/**
2016-05-30 10:24:03 -05:00
* Succeeds with true if an emitted value is available by calling getCurrent() or false if the observable has
* resolved. If the observable fails, the returned awaitable will fail with the same exception.
2016-05-24 11:47:14 -05:00
*
2016-08-17 22:25:54 -05:00
* @return \Interop\Async\Awaitable<bool>
2016-05-24 11:47:14 -05:00
*/
2016-08-11 14:35:58 -05:00
public function next(): Awaitable {
2016-08-17 22:25:54 -05:00
if (isset($this->deferreds[$this->position])) {
$future = $this->deferreds[$this->position];
unset($this->values[$this->position], $this->deferreds[$this->position]);
2016-05-29 23:49:44 -05:00
$future->resolve();
}
++$this->position;
if (isset($this->values[$this->position])) {
return new Success(true);
}
2016-05-30 10:24:03 -05:00
if ($this->resolved) {
2016-08-23 16:21:05 +02:00
--$this->position;
2016-05-29 11:35:09 -05:00
if ($this->exception) {
return new Failure($this->exception);
}
return new Success(false);
}
2016-05-29 23:49:44 -05:00
$this->deferred = new Deferred;
2016-05-29 11:35:09 -05:00
return $this->deferred->getAwaitable();
2016-05-27 15:44:01 -05:00
}
2016-05-24 11:47:14 -05:00
/**
* Gets the last emitted value or throws an exception if the observable has completed.
*
* @return mixed Value emitted from observable.
*
* @throws \Error If the observable has resolved or next() was not called before calling this method.
2016-05-24 11:47:14 -05:00
*/
2016-05-27 15:44:01 -05:00
public function getCurrent() {
2016-05-30 10:24:03 -05:00
if (empty($this->values) && $this->resolved) {
2016-08-17 21:11:03 -05:00
throw new \Error("The observable has resolved");
2016-05-29 11:35:09 -05:00
}
2016-05-29 23:49:44 -05:00
if (!isset($this->values[$this->position])) {
throw new \Error("Awaitable returned from next() must resolve before calling this method");
2016-05-29 11:35:09 -05:00
}
2016-05-29 23:49:44 -05:00
return $this->values[$this->position];
2016-05-27 15:44:01 -05:00
}
2016-05-24 11:47:14 -05:00
/**
2016-05-30 10:24:03 -05:00
* Gets the result of the observable or throws the failure reason. Also throws an exception if the observable has
* not completed.
2016-05-24 11:47:14 -05:00
*
* @return mixed Final return value of the observable.
*
* @throws \Error If the observable has not completed.
2016-08-11 14:35:58 -05:00
* @throws \Throwable The exception used to fail the observable.
2016-05-24 11:47:14 -05:00
*/
2016-05-30 10:24:03 -05:00
public function getResult() {
if (!$this->resolved) {
throw new \Error("The observable has not resolved");
2016-05-29 11:35:09 -05:00
}
if ($this->exception) {
throw $this->exception;
}
2016-05-29 23:49:44 -05:00
return $this->result;
2016-05-27 15:44:01 -05:00
}
2016-08-23 08:50:04 -05:00
/**
* Returns an array of values that were not consumed by the Observer before the Observable completed.
*
* @return array Unconsumed emitted values.
*
* @throws \Error If the observable has not completed.
*/
2016-08-23 16:21:05 +02:00
public function drain(): array {
2016-08-23 08:50:04 -05:00
if (!$this->resolved) {
throw new \Error("The observable has not resolved");
}
$values = $this->values;
$this->values = [];
$this->position = -1;
$deferreds = $this->deferreds;
$this->deferreds = [];
foreach ($deferreds as $deferred) {
$deferred->resolve();
}
return $values;
}
2016-05-24 11:47:14 -05:00
}