1
0
mirror of https://github.com/danog/amp.git synced 2025-01-08 05:58:22 +01:00
amp/lib/Future.php

114 lines
2.9 KiB
PHP
Raw Normal View History

2014-09-22 22:47:48 +02:00
<?php
2014-09-23 04:38:32 +02:00
namespace Amp;
2014-09-22 22:47:48 +02:00
class Future implements Promisor, Promise {
private $isResolved = false;
private $watchers = [];
private $whens = [];
private $error;
private $result;
/**
* Retrieve the Promise placeholder for this deferred value
*
* This implementation acts as both Promisor and Promise so we simply return the
* current instance. If users require a Promisor that can only be resolved by code
2014-09-23 04:38:32 +02:00
* holding a reference to the Promisor they may instead use Amp\PrivateFuture.
2015-03-23 16:07:40 +01:00
*
* @return \Amp\Promise
2014-09-22 22:47:48 +02:00
*/
2015-04-30 19:41:14 +02:00
public function promise() {
2014-09-22 22:47:48 +02:00
return $this;
}
/**
2015-03-23 16:07:40 +01:00
* {@inheritDoc}
2014-09-22 22:47:48 +02:00
*/
public function when(callable $func) {
if ($this->isResolved) {
$func($this->error, $this->result);
} else {
$this->whens[] = $func;
}
}
/**
2015-03-23 16:07:40 +01:00
* {@inheritDoc}
2014-09-22 22:47:48 +02:00
*/
public function watch(callable $func) {
if (!$this->isResolved) {
$this->watchers[] = $func;
}
}
/**
2015-03-23 16:07:40 +01:00
* {@inheritDoc}
2015-03-19 16:14:21 +01:00
* @throws \LogicException if the promise has already resolved
2014-09-22 22:47:48 +02:00
*/
2015-04-30 19:41:14 +02:00
public function update($progress) {
2014-09-22 22:47:48 +02:00
if ($this->isResolved) {
throw new \LogicException(
'Cannot update resolved promise'
);
}
foreach ($this->watchers as $watcher) {
2015-04-30 19:41:14 +02:00
$watcher($progress);
2014-09-22 22:47:48 +02:00
}
}
/**
2015-03-23 16:07:40 +01:00
* {@inheritDoc}
2015-03-19 16:14:21 +01:00
* @throws \LogicException if the promise has already resolved or the result is the current instance
2014-09-22 22:47:48 +02:00
*/
public function succeed($result = null) {
if ($this->isResolved) {
throw new \LogicException(
'Promise already resolved'
);
} elseif ($result === $this) {
throw new \LogicException(
'A Promise cannot act as its own resolution result'
);
} elseif ($result instanceof Promise) {
$result->when(function(\Exception $error = null, $result = null) {
if ($error) {
$this->fail($error);
} else {
$this->succeed($result);
}
});
} else {
$this->isResolved = true;
$this->result = $result;
$error = null;
foreach ($this->whens as $when) {
$when($error, $result);
}
$this->whens = $this->watchers = [];
}
}
/**
2015-03-23 16:07:40 +01:00
* {@inheritDoc}
2015-03-19 16:14:21 +01:00
* @throws \LogicException if the promise has already resolved
2014-09-22 22:47:48 +02:00
*/
public function fail(\Exception $error) {
if ($this->isResolved) {
throw new \LogicException(
'Promise already resolved'
);
}
$this->isResolved = true;
$this->error = $error;
$result = null;
foreach ($this->whens as $when) {
$when($error, $result);
}
$this->whens = $this->watchers = [];
}
}