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

172 lines
4.3 KiB
PHP
Raw Normal View History

2016-05-27 01:20:05 +02:00
<?php
2016-08-16 06:46:26 +02:00
declare(strict_types=1);
2016-05-27 01:20:05 +02:00
namespace Amp\Internal;
2016-08-16 09:28:47 +02:00
use Amp\{ Deferred, Observable, Subscriber, Success, function pipe };
use Interop\Async\{ Awaitable, 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.
* Note that it is the responsibility of the user of this trait to ensure that subscribers have a chance to subscribe first
* before emitting values.
2016-06-01 18:37:12 +02:00
*
* @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-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-08-16 09:28:47 +02:00
$this->unsubscribe = function ($id) {
$this->unsubscribe($id);
};
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-08-11 21:35:58 +02:00
public function subscribe(callable $onNext): Subscriber {
if ($this->resolved) {
return new Subscriber($this->nextId++, function() {});
2016-05-29 18:35:09 +02:00
}
$id = $this->nextId++;
$this->subscribers[$id] = $onNext;
2016-05-27 01:20:05 +02:00
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
*/
2016-08-11 21:35:58 +02:00
private function unsubscribe(string $id) {
if (!isset($this->subscribers[$id])) {
return;
2016-05-29 18:35:09 +02:00
}
unset($this->subscribers[$id]);
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
*
2016-08-11 21:35:58 +02:00
* @throws \Error If the observable has resolved.
2016-05-27 01:20:05 +02:00
*/
2016-08-11 21:35:58 +02:00
private function emit($value): Awaitable {
2016-05-29 18:35:09 +02:00
if ($this->resolved) {
2016-08-11 21:35:58 +02:00
throw new \Error("The observable has been resolved; cannot emit more values");
2016-05-27 01:20:05 +02:00
}
2016-08-16 09:28:47 +02:00
if ($value instanceof Awaitable) {
if ($value instanceof Observable) {
$value->subscribe(function ($value) {
return $this->emit($value);
});
$value->when(function ($e) {
if ($e) {
$this->fail($e);
}
});
return $value; // Do not emit observable result.
2016-08-16 09:28:47 +02:00
}
$deferred = new Deferred;
$value->when(function ($e, $v) use ($deferred) {
if ($e) {
$this->fail($e);
$deferred->fail($e);
return;
}
$deferred->resolve($this->emit($v));
2016-08-16 09:28:47 +02:00
});
return $deferred->getAwaitable();
2016-08-16 09:28:47 +02:00
}
$awaitables = [];
foreach ($this->subscribers as $onNext) {
try {
$result = $onNext($value);
if ($result instanceof Awaitable) {
$awaitables[] = $result;
}
} catch (\Throwable $e) {
Loop::defer(static function () use ($e) {
throw $e;
});
}
}
if (!$awaitables) {
return new Success($value);
}
$deferred = new Deferred;
$count = \count($awaitables);
$f = static function ($e) use ($deferred, $value, &$count) {
2016-08-16 09:28:47 +02:00
if ($e) {
Loop::defer(static function () use ($e) {
throw $e;
});
}
if (!--$count) {
$deferred->resolve($value);
}
};
foreach ($awaitables as $awaitable) {
$awaitable->when($f);
}
return $deferred->getAwaitable();
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-08-11 21:35:58 +02:00
* @throws \Error If the observable has already been resolved.
2016-05-27 01:20:05 +02:00
*/
private function resolve($value = null) {
$this->complete($value);
$this->subscribers = [];
2016-08-16 09:28:47 +02:00
$this->unsubscribe = null;
2016-05-27 01:20:05 +02:00
}
}