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

205 lines
4.9 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\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 Amp\UnsubscribedException;
2016-05-29 18:35:09 +02:00
use Interop\Async\Awaitable;
2016-06-01 06:02:59 +02:00
use Interop\Async\Loop;
2016-05-27 01:20:05 +02:00
2016-06-01 18:37:12 +02:00
/**
* Trait used by Observable implementations. Do not use this trait in your code, instead compose your class from one of
* the available classes implementing \Amp\Observable.
*
* @internal
*/
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-06-01 06:02:59 +02:00
/**
* @var \Amp\Future|null
*/
private $waiting;
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
*/
private $unsubscribe;
2016-05-27 01:20:05 +02:00
2016-06-01 06:02:59 +02:00
/**
* Initializes the trait. Use as constructor or call within using class constructor.
*/
2016-07-19 19:42:58 +02:00
private function init() {
2016-06-01 06:02:59 +02:00
$this->waiting = new Future;
$this->unsubscribe = function ($id, $exception = null) {
$this->unsubscribe($id, $exception);
2016-06-01 06:02:59 +02:00
};
}
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->result !== null) {
return new Subscriber(
$this->nextId++,
$this->unsubscribe
2016-05-29 18:35:09 +02:00
);
}
$id = $this->nextId++;
$this->subscribers[$id] = $onNext;
2016-05-27 01:20:05 +02:00
2016-06-01 06:02:59 +02:00
if ($this->waiting !== null) {
$waiting = $this->waiting;
$this->waiting = null;
$waiting->resolve();
}
return new Subscriber($id, $this->unsubscribe);
2016-05-27 01:20:05 +02:00
}
/**
2016-05-29 18:35:09 +02:00
* @param string $id
* @param \Throwable|\Exception|null $exception
*/
private function unsubscribe($id, $exception = null) {
if (!isset($this->subscribers[$id])) {
return;
2016-05-29 18:35:09 +02:00
}
unset($this->subscribers[$id]);
2016-06-01 06:02:59 +02:00
if (empty($this->subscribers)) {
$this->waiting = new Future;
}
2016-05-29 18:35:09 +02:00
}
/**
* 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
*/
private 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-06-01 06:02:59 +02:00
while ($this->waiting !== null) {
yield $this->waiting;
}
2016-05-27 01:20:05 +02:00
try {
if ($value instanceof Observable) {
$subscriber = $value->subscribe(function ($value) {
2016-05-29 18:35:09 +02:00
return $this->emit($value);
});
yield Coroutine::result(yield $subscriber);
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->unsubscribe($id, $exception);
2016-05-29 18:35:09 +02:00
} catch (\Exception $exception) {
$this->unsubscribe($id, $exception);
2016-05-29 18:35:09 +02:00
}
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->unsubscribe($id, $exception);
2016-05-29 18:35:09 +02:00
} catch (\Exception $exception) {
$this->unsubscribe($id, $exception);
2016-05-29 18:35:09 +02:00
}
}
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
*/
private function resolve($value = null) {
$this->subscribers = [];
2016-05-27 01:20:05 +02:00
2016-06-01 06:02:59 +02:00
if ($this->waiting !== null) {
$waiting = $this->waiting;
$this->waiting = null;
$waiting->resolve();
}
2016-05-29 18:35:09 +02:00
$this->complete($value);
2016-05-27 01:20:05 +02:00
}
}