1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 13:21:16 +01:00
amp/lib/Postponed.php

98 lines
2.6 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;
2016-11-14 13:59:21 -06:00
use Interop\Async\Promise;
2016-08-11 14:35:58 -05:00
// @codeCoverageIgnoreStart
2016-05-26 18:20:05 -05:00
try {
2016-08-17 22:25:54 -05:00
if (@\assert(false)) {
2016-05-26 18:20:05 -05:00
production: // PHP 7 production environment (zend.assertions=0)
final class Postponed implements Observable {
use Internal\Producer {
emit as public;
2016-05-29 11:35:09 -05:00
resolve as public;
2016-05-26 18:20:05 -05:00
fail as public;
}
/**
* @return \Amp\Observable
*/
2016-11-14 13:59:21 -06:00
public function observe(): Observable {
2016-05-26 18:20:05 -05:00
return $this;
}
}
} else {
development: // PHP 7 development (zend.assertions=1) or PHP 5.x
final class Postponed {
/**
* @var \Amp\Observable
*/
private $observable;
/**
* @var callable
*/
private $emit;
/**
* @var callable
*/
2016-05-29 11:35:09 -05:00
private $resolve;
2016-05-26 18:20:05 -05:00
/**
* @var callable
*/
private $fail;
public function __construct() {
$this->observable = new Internal\PrivateObservable(
2016-05-29 11:35:09 -05:00
function (callable $emit, callable $resolve, callable $fail) {
2016-05-26 18:20:05 -05:00
$this->emit = $emit;
2016-05-29 11:35:09 -05:00
$this->resolve = $resolve;
2016-05-26 18:20:05 -05:00
$this->fail = $fail;
}
);
}
/**
* @return \Amp\Observable
*/
2016-11-14 13:59:21 -06:00
public function observe(): Observable {
2016-05-26 18:20:05 -05:00
return $this->observable;
}
/**
* Emits a value from the observable.
*
* @param mixed $value
*
2016-11-14 13:59:21 -06:00
* @return \Interop\Async\Promise
2016-05-26 18:20:05 -05:00
*/
2016-11-14 13:59:21 -06:00
public function emit($value): Promise {
2016-08-11 14:35:58 -05:00
return ($this->emit)($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-26 18:20:05 -05:00
*
* @param mixed $value
*/
2016-05-29 11:35:09 -05:00
public function resolve($value = null) {
2016-08-11 14:35:58 -05:00
($this->resolve)($value);
2016-05-26 18:20:05 -05:00
}
/**
* Fails the observable with the given reason.
*
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
public function fail(\Throwable $reason) {
($this->fail)($reason);
2016-05-26 18:20:05 -05:00
}
}
}
} catch (\AssertionError $exception) {
goto development; // zend.assertions=1 and assert.exception=1, use development definition.
2016-08-11 14:35:58 -05:00
} // @codeCoverageIgnoreEnd