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

201 lines
4.7 KiB
PHP
Raw Normal View History

2016-05-26 18:20:05 -05:00
<?php
namespace Amp\Internal;
use Amp\Coroutine;
2016-05-29 11:35:09 -05:00
use Amp\Future;
use Amp\Observable;
2016-05-27 15:44:01 -05:00
use Amp\Subscriber;
2016-05-29 11:35:09 -05:00
use Interop\Async\Awaitable;
2016-05-31 23:02:59 -05:00
use Interop\Async\Loop;
2016-05-26 18:20:05 -05:00
2016-06-01 11:37:12 -05: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-26 18:20:05 -05:00
trait Producer {
2016-05-29 11:35:09 -05:00
use Placeholder {
resolve as complete;
}
2016-05-26 18:20:05 -05:00
/**
2016-05-29 11:35:09 -05:00
* @var callable[]
2016-05-26 18:20:05 -05:00
*/
2016-05-29 11:35:09 -05:00
private $subscribers = [];
2016-05-26 18:20:05 -05:00
2016-05-31 23:02:59 -05:00
/**
* @var \Amp\Future|null
*/
private $waiting;
2016-05-26 18:20:05 -05:00
/**
2016-05-29 11:35:09 -05:00
* @var \Amp\Future[]
2016-05-26 18:20:05 -05:00
*/
2016-05-29 11:35:09 -05:00
private $futures = [];
2016-05-26 18:20:05 -05:00
/**
2016-05-29 11:35:09 -05:00
* @var string
2016-05-26 18:20:05 -05:00
*/
2016-05-29 11:35:09 -05:00
private $nextId = "a";
2016-05-26 18:20:05 -05:00
/**
* @var callable
*/
private $unsubscribe;
2016-05-26 18:20:05 -05:00
2016-05-31 23:02:59 -05:00
/**
* Initializes the trait. Use as constructor or call within using class constructor.
*/
2016-07-19 12:42:58 -05:00
private function init() {
2016-05-31 23:02:59 -05:00
$this->waiting = new Future;
$this->unsubscribe = function ($id, $exception = null) {
$this->unsubscribe($id, $exception);
2016-05-31 23:02:59 -05:00
};
}
2016-05-26 18:20:05 -05:00
/**
2016-05-29 11:35:09 -05:00
* @param callable $onNext
*
* @return \Amp\Subscriber
2016-05-26 18:20:05 -05:00
*/
2016-05-27 15:44:01 -05:00
public function subscribe(callable $onNext) {
if ($this->resolved) {
2016-05-29 11:35:09 -05:00
return new Subscriber(
$this->nextId++,
$this->unsubscribe
2016-05-29 11:35:09 -05:00
);
}
$id = $this->nextId++;
$this->subscribers[$id] = $onNext;
2016-05-26 18:20:05 -05:00
2016-05-31 23:02:59 -05:00
if ($this->waiting !== null) {
$waiting = $this->waiting;
$this->waiting = null;
$waiting->resolve();
}
return new Subscriber($id, $this->unsubscribe);
2016-05-26 18:20:05 -05:00
}
/**
2016-05-29 11:35:09 -05:00
* @param string $id
*/
private function unsubscribe($id) {
if (!isset($this->subscribers[$id])) {
return;
2016-05-29 11:35:09 -05:00
}
unset($this->subscribers[$id]);
2016-05-31 23:02:59 -05:00
if (empty($this->subscribers)) {
$this->waiting = new Future;
}
2016-05-29 11:35:09 -05: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 15:44:01 -05:00
*
* @param mixed $value
*
* @return \Interop\Async\Awaitable
2016-05-29 11:35:09 -05:00
*
* @throws \LogicException If the observable has resolved.
2016-05-26 18:20:05 -05:00
*/
2016-07-19 13:10:15 -05:00
private function emit($value) {
2016-05-29 11:35:09 -05:00
if ($this->resolved) {
throw new \LogicException("The observable has been resolved; cannot emit more values");
2016-05-26 18:20:05 -05:00
}
return new Coroutine($this->push($value));
}
/**
* @coroutine
*
* @param mixed $value
*
* @return \Generator
*
* @throws \InvalidArgumentException
* @throws \Throwable|\Exception
*/
2016-05-29 11:35:09 -05:00
private function push($value) {
if ($this->waiting !== null) {
2016-05-31 23:02:59 -05:00
yield $this->waiting;
}
2016-05-26 18:20:05 -05:00
try {
if ($value instanceof Observable) {
$value->subscribe(function ($value) {
2016-05-29 11:35:09 -05:00
return $this->emit($value);
});
yield Coroutine::result(yield $value);
return;
}
if ($value instanceof Awaitable) {
$value = (yield $value);
2016-05-29 11:35:09 -05:00
}
2016-05-26 18:20:05 -05:00
} catch (\Throwable $exception) {
2016-05-29 11:35:09 -05:00
if (!$this->resolved) {
$this->fail($exception);
}
2016-05-26 18:20:05 -05:00
throw $exception;
} catch (\Exception $exception) {
2016-05-29 11:35:09 -05:00
if (!$this->resolved) {
$this->fail($exception);
}
2016-05-26 18:20:05 -05:00
throw $exception;
}
2016-05-29 11:35:09 -05: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 11:35:09 -05:00
} catch (\Exception $exception) {
$this->unsubscribe($id, $exception);
2016-05-29 11:35:09 -05:00
}
2016-05-26 18:20:05 -05:00
}
2016-05-29 11:35:09 -05:00
foreach ($awaitables as $id => $awaitable) {
try {
yield $awaitable;
} catch (\Throwable $exception) {
$this->unsubscribe($id, $exception);
2016-05-29 11:35:09 -05:00
} catch (\Exception $exception) {
$this->unsubscribe($id, $exception);
2016-05-29 11:35:09 -05:00
}
}
2016-05-26 18:20:05 -05:00
2016-05-29 11:35:09 -05:00
yield Coroutine::result($value);
2016-05-26 18:20:05 -05:00
}
/**
2016-05-29 11:35:09 -05:00
* Resolves the observable with the given value.
2016-05-27 15:44:01 -05:00
*
2016-05-29 11:35:09 -05:00
* @param mixed $value
2016-05-27 15:44:01 -05:00
*
2016-05-29 11:35:09 -05:00
* @throws \LogicException If the observable has already been resolved.
2016-05-26 18:20:05 -05:00
*/
private function resolve($value = null) {
$this->complete($value);
$this->subscribers = [];
2016-05-26 18:20:05 -05:00
2016-05-31 23:02:59 -05:00
if ($this->waiting !== null) {
$this->waiting->resolve();
2016-05-31 23:02:59 -05:00
$this->waiting = null;
}
2016-05-26 18:20:05 -05:00
}
}