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

99 lines
2.6 KiB
PHP
Raw Normal View History

2016-05-26 18:20:05 -05:00
<?php
namespace Amp;
try {
if (@assert(false)) {
production: // PHP 7 production environment (zend.assertions=0)
final class Postponed implements Observable {
use Internal\Producer {
2016-07-19 12:42:58 -05:00
init as public __construct;
2016-05-26 18:20:05 -05:00
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
*/
public function getObservable() {
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
*/
public function getObservable() {
return $this->observable;
}
/**
* Emits a value from the observable.
*
* @param mixed $value
*
* @return \Interop\Async\Awaitable
*/
2016-07-20 08:52:19 -05:00
public function emit($value) {
2016-05-26 18:20:05 -05:00
$emit = $this->emit;
return $emit($value);
}
/**
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) {
$resolve = $this->resolve;
$resolve($value);
2016-05-26 18:20:05 -05:00
}
/**
* Fails the observable with the given reason.
*
* @param \Throwable|\Exception $reason
*/
public function fail($reason) {
$fail = $this->fail;
2016-05-29 11:35:09 -05:00
$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.
}