1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 00:49:48 +01:00
amp/lib/Internal/PrivateObservable.php

59 lines
1.3 KiB
PHP
Raw Normal View History

2016-05-27 01:20:05 +02:00
<?php
namespace Amp\Internal;
use Amp\Observable;
/**
* An observable that cannot externally emit values. Used by Postponed in development mode.
2016-06-01 18:37:12 +02:00
*
* @internal
2016-05-27 01:20:05 +02:00
*/
final class PrivateObservable implements Observable {
2016-05-27 22:44:01 +02:00
use Producer;
2016-05-27 01:20:05 +02:00
/**
* @param callable(callable $emit, callable $complete, callable $fail): void $emitter
*/
public function __construct(callable $emitter) {
2016-06-01 06:02:59 +02:00
$this->init();
2016-05-27 01:20:05 +02:00
/**
* Emits a value from the observable.
*
* @param mixed $value
*
* @return \Interop\Async\Awaitable
*/
$emit = function ($value = null) {
return $this->emit($value);
};
/**
* Completes the observable with the given value.
*
* @param mixed $value
*/
2016-05-29 18:35:09 +02:00
$resolve = function ($value = null) {
$this->resolve($value);
2016-05-27 01:20:05 +02:00
};
/**
* Fails the observable with the given exception.
*
* @param \Exception $reason
*/
$fail = function ($reason) {
2016-05-29 18:35:09 +02:00
$this->fail($reason);
2016-05-27 01:20:05 +02:00
};
try {
2016-05-29 18:35:09 +02:00
$emitter($emit, $resolve, $fail);
2016-05-27 01:20:05 +02:00
} catch (\Throwable $exception) {
$this->fail($exception);
} catch (\Exception $exception) {
$this->fail($exception);
}
}
}