1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00
amp/lib/Internal/Producer.php

178 lines
4.4 KiB
PHP
Raw Normal View History

2016-05-27 01:20:05 +02:00
<?php
namespace Amp\Internal;
use Amp\Coroutine;
2016-05-29 18:35:09 +02:00
use Amp\DisposedException;
use Amp\Future;
use Amp\Observable;
2016-05-27 22:44:01 +02:00
use Amp\Subscriber;
2016-05-29 18:35:09 +02:00
use Amp\Success;
use Interop\Async\Awaitable;
2016-05-27 01:20:05 +02:00
trait Producer {
2016-05-29 18:35:09 +02:00
use Placeholder {
resolve as complete;
}
2016-05-27 01:20:05 +02:00
/**
2016-05-29 18:35:09 +02:00
* @var callable[]
2016-05-27 01:20:05 +02:00
*/
2016-05-29 18:35:09 +02:00
private $subscribers = [];
2016-05-27 01:20:05 +02:00
/**
2016-05-29 18:35:09 +02:00
* @var \Amp\Future[]
2016-05-27 01:20:05 +02:00
*/
2016-05-29 18:35:09 +02:00
private $futures = [];
2016-05-27 01:20:05 +02:00
/**
2016-05-29 18:35:09 +02:00
* @var string
2016-05-27 01:20:05 +02:00
*/
2016-05-29 18:35:09 +02:00
private $nextId = "a";
2016-05-27 01:20:05 +02:00
/**
* @var callable
*/
2016-05-29 18:35:09 +02:00
private $dispose;
2016-05-27 01:20:05 +02:00
/**
2016-05-29 18:35:09 +02:00
* @param callable $onNext
*
* @return \Amp\Subscriber
2016-05-27 01:20:05 +02:00
*/
2016-05-27 22:44:01 +02:00
public function subscribe(callable $onNext) {
2016-05-29 18:35:09 +02:00
if ($this->dispose === null) {
$this->dispose = function ($id, $exception = null) {
$this->dispose($id, $exception);
2016-05-27 22:44:01 +02:00
};
}
2016-05-29 18:35:09 +02:00
if ($this->result !== null) {
return new Subscriber(
$this->nextId++,
$this->result instanceof Awaitable ? $this->result : new Success($this->result),
$this->dispose
);
}
$id = $this->nextId++;
$this->futures[$id] = new Future;
$this->subscribers[$id] = $onNext;
2016-05-27 01:20:05 +02:00
2016-05-29 18:35:09 +02:00
return new Subscriber($id, $this->futures[$id], $this->dispose);
2016-05-27 01:20:05 +02:00
}
/**
2016-05-29 18:35:09 +02:00
* @param string $id
* @param \Throwable|\Exception|null $exception
*/
protected function dispose($id, $exception = null) {
if (!isset($this->futures[$id])) {
throw new \LogicException("Disposable has already been disposed");
}
$future = $this->futures[$id];
unset($this->subscribers[$id], $this->futures[$id]);
$future->fail($exception ?: new DisposedException());
}
/**
* Emits a value from the observable. The returned awaitable is resolved with the emitted value once all subscribers
* have been invoked.
2016-05-27 22:44:01 +02:00
*
* @param mixed $value
*
* @return \Interop\Async\Awaitable
2016-05-29 18:35:09 +02:00
*
* @throws \LogicException If the observable has resolved.
2016-05-27 01:20:05 +02:00
*/
protected function emit($value = null) {
2016-05-29 18:35:09 +02:00
if ($this->resolved) {
throw new \LogicException("The observable has been resolved; cannot emit more values");
2016-05-27 01:20:05 +02:00
}
return new Coroutine($this->push($value));
}
/**
* @coroutine
*
* @param mixed $value
*
* @return \Generator
*
* @throws \InvalidArgumentException
* @throws \Throwable|\Exception
*/
2016-05-29 18:35:09 +02:00
private function push($value) {
2016-05-27 01:20:05 +02:00
try {
if ($value instanceof Observable) {
2016-05-29 18:35:09 +02:00
$disposable = $value->subscribe(function ($value) {
return $this->emit($value);
});
yield Coroutine::result(yield $disposable);
return;
}
if ($value instanceof Awaitable) {
$value = (yield $value);
2016-05-29 18:35:09 +02:00
}
2016-05-27 01:20:05 +02:00
} catch (\Throwable $exception) {
2016-05-29 18:35:09 +02:00
if (!$this->resolved) {
$this->fail($exception);
}
2016-05-27 01:20:05 +02:00
throw $exception;
} catch (\Exception $exception) {
2016-05-29 18:35:09 +02:00
if (!$this->resolved) {
$this->fail($exception);
}
2016-05-27 01:20:05 +02:00
throw $exception;
}
2016-05-29 18:35:09 +02:00
$awaitables = [];
foreach ($this->subscribers as $id => $onNext) {
try {
$result = $onNext($value);
if ($result instanceof Awaitable) {
$awaitables[$id] = $result;
}
} catch (\Throwable $exception) {
$this->dispose($id, $exception);
} catch (\Exception $exception) {
$this->dispose($id, $exception);
}
2016-05-27 01:20:05 +02:00
}
2016-05-29 18:35:09 +02:00
foreach ($awaitables as $id => $awaitable) {
try {
yield $awaitable;
} catch (\Throwable $exception) {
$this->dispose($id, $exception);
} catch (\Exception $exception) {
$this->dispose($id, $exception);
}
}
2016-05-27 01:20:05 +02:00
2016-05-29 18:35:09 +02:00
yield Coroutine::result($value);
2016-05-27 01:20:05 +02:00
}
/**
2016-05-29 18:35:09 +02:00
* Resolves the observable with the given value.
2016-05-27 22:44:01 +02:00
*
2016-05-29 18:35:09 +02:00
* @param mixed $value
2016-05-27 22:44:01 +02:00
*
2016-05-29 18:35:09 +02:00
* @throws \LogicException If the observable has already been resolved.
2016-05-27 01:20:05 +02:00
*/
2016-05-29 18:35:09 +02:00
protected function resolve($value = null) {
$futures = $this->futures;
$this->subscribers = $this->futures = [];
2016-05-27 01:20:05 +02:00
2016-05-29 18:35:09 +02:00
$this->complete($value);
2016-05-27 01:20:05 +02:00
2016-05-29 18:35:09 +02:00
foreach ($futures as $future) {
$future->resolve($value);
}
2016-05-27 01:20:05 +02:00
}
}