1
0
mirror of https://github.com/danog/amp.git synced 2025-01-23 05:41:25 +01:00
amp/lib/Internal/PrivateObservable.php

56 lines
1.3 KiB
PHP
Raw Normal View History

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