1
0
mirror of https://github.com/danog/amp.git synced 2024-12-03 09:57:51 +01:00
amp/lib/Internal/PrivateObservable.php

52 lines
1.2 KiB
PHP
Raw Normal View History

<?php
2016-08-16 06:46:26 +02:00
2016-05-27 01:20:05 +02:00
namespace Amp\Internal;
use Amp\Observable;
2016-11-14 20:59:21 +01:00
use Interop\Async\Promise;
2016-05-27 01:20:05 +02:00
/**
* 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) {
/**
* Emits a value from the observable.
*
* @param mixed $value
*
2016-11-14 20:59:21 +01:00
* @return \Interop\Async\Promise
2016-05-27 01:20:05 +02:00
*/
2016-11-14 20:59:21 +01:00
$emit = function ($value = null): Promise {
2016-05-27 01:20:05 +02:00
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.
*
2016-08-11 21:35:58 +02:00
* @param \Throwable $reason
2016-05-27 01:20:05 +02:00
*/
2016-08-11 21:35:58 +02:00
$fail = function (\Throwable $reason) {
2016-05-29 18:35:09 +02:00
$this->fail($reason);
2016-05-27 01:20:05 +02:00
};
$emitter($emit, $resolve, $fail);
2016-05-27 01:20:05 +02:00
}
}