1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00

Future -> Deferred

This commit is contained in:
Daniel Lowrey 2015-05-19 00:21:33 -04:00
parent 3af013d418
commit 953c4612e6
7 changed files with 29 additions and 29 deletions

9
lib/Deferred.php Normal file
View File

@ -0,0 +1,9 @@
<?php
namespace Amp;
if (!defined("AMP_DEBUG") || \AMP_DEBUG) {
final class Deferred implements Promisor { use PrivatePromisor; }
} else {
final class Deferred implements Promisor, Promise { use PublicPromisor; }
}

View File

@ -1,9 +0,0 @@
<?php
namespace Amp;
if (!defined("AMP_DEBUG") || \AMP_DEBUG) {
final class Future implements Promisor { use PrivatePromisor; }
} else {
final class Future implements Promisor, Promise { use PublicPromisor; }
}

View File

@ -3,7 +3,7 @@
namespace Amp;
/**
* A placeholder value for the future result of an asynchronous computation
* A placeholder value for the deferred result of an asynchronous computation
*/
interface Promise {
/**

View File

@ -16,10 +16,10 @@ class PromiseStream {
*/
public function __construct(Promise $watchedPromise) {
$this->state = self::WAIT;
$this->promisors[] = new Future;
$this->promisors[] = new Deferred;
$watchedPromise->watch(function($data) {
$this->state = self::NOTIFY;
$this->promisors[$this->index + 1] = new Future;
$this->promisors[$this->index + 1] = new Deferred;
$this->promisors[$this->index++]->succeed($data);
});
$watchedPromise->when(function($error, $result) {

View File

@ -7,13 +7,13 @@ namespace Amp;
*
* A Promisor resolves its associated placeholder value (Promise) Promisor::succeed() or
* Promisor::fail(). Promisor::update() may be used to notify watchers of progress resolving
* the future value.
* the deferred value.
*
* Example:
*
* function myAsyncProducer() {
* // Create a new promisor that needs to be resolved
* $promisor = new Amp\Future;
* $promisor = new Amp\Deferred;
*
* // When we eventually finish non-blocking value resolution we
* // simply call the relevant Promise method to notify any code
@ -31,7 +31,7 @@ namespace Amp;
*/
interface Promisor {
/**
* Promise future fulfillment via a temporary placeholder value
* Promise deferred fulfillment via a temporary placeholder value
*
* @return \Amp\Promise
*/

View File

@ -163,7 +163,7 @@ function all(array $promises) {
$results = [];
$remaining = count($promises);
$promisor = new Future;
$promisor = new Deferred;
foreach ($promises as $key => $resolvable) {
if (!$resolvable instanceof Promise) {
@ -215,7 +215,7 @@ function some(array $promises) {
$errors = [];
$results = [];
$remaining = count($promises);
$promisor = new Future;
$promisor = new Deferred;
foreach ($promises as $key => $resolvable) {
if (!$resolvable instanceof Promise) {
@ -258,7 +258,7 @@ function any(array $promises) {
$results = [];
$errors = [];
$remaining = count($promises);
$promisor = new Future;
$promisor = new Deferred;
foreach ($promises as $key => $resolvable) {
if (!$resolvable instanceof Promise) {
@ -294,7 +294,7 @@ function first(array $promises) {
$remaining = count($promises);
$isComplete = false;
$promisor = new Future;
$promisor = new Deferred;
foreach ($promises as $resolvable) {
if (!$resolvable instanceof Promise) {
@ -304,7 +304,7 @@ function first(array $promises) {
$promise->when(function($error, $result) use (&$remaining, &$isComplete, $promisor) {
if ($isComplete) {
// we don't care about Futures that resolve after the first
// we don't care about Deferreds that resolve after the first
return;
} elseif ($error && --$remaining === 0) {
$promisor->fail(new \RuntimeException(
@ -321,7 +321,7 @@ function first(array $promises) {
}
/**
* Map promised future values using the specified functor
* Map promised deferred values using the specified functor
*/
function map(array $promises, callable $functor) {
if (empty($promises)) {
@ -330,7 +330,7 @@ function map(array $promises, callable $functor) {
$results = [];
$remaining = count($promises);
$promisor = new Future;
$promisor = new Deferred;
foreach ($promises as $key => $resolvable) {
$promise = ($resolvable instanceof Promise) ? $resolvable : new Success($resolvable);
@ -361,7 +361,7 @@ function map(array $promises, callable $functor) {
}
/**
* Filter future values using the specified functor
* Filter deferred values using the specified functor
*
* If the functor returns a truthy value the resolved promise result is retained, otherwise it is
* discarded. Array keys are retained for any results not filtered out by the functor.
@ -373,13 +373,13 @@ function filter(array $promises, callable $functor) {
$results = [];
$remaining = count($promises);
$promisor = new Future;
$promisor = new Deferred;
foreach ($promises as $key => $resolvable) {
$promise = ($resolvable instanceof Promise) ? $resolvable : new Success($resolvable);
$promise->when(function($error, $result) use (&$remaining, &$results, $key, $promisor, $functor) {
if (empty($remaining)) {
// If the future result already failed we don't bother.
// If the deferred result already failed we don't bother.
return;
}
if ($error) {
@ -465,7 +465,7 @@ function resolve(\Generator $generator, Reactor $reactor = null, callable $promi
*/
$cs = new \StdClass;
$cs->reactor = $reactor ?: getReactor();
$cs->promisor = new Future;
$cs->promisor = new Deferred;
$cs->generator = $generator;
$cs->promisifier = $promisifier;

View File

@ -5,7 +5,7 @@ namespace Amp\Test;
use Amp\NativeReactor;
use Amp\Success;
use Amp\Failure;
use Amp\Future;
use Amp\Deferred;
use Amp\PromiseStream;
class FunctionsTest extends \PHPUnit_Framework_TestCase {
@ -210,7 +210,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
public function testCoroutineResolutionBuffersYieldedPromiseStream() {
(new NativeReactor)->run(function($reactor) {
$promisor = new Future;
$promisor = new Deferred;
$reactor->repeat(function($reactor, $watcherId) use (&$i, $promisor) {
$i++;
$promisor->update($i);
@ -231,7 +231,7 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
*/
public function testCoroutineResolutionThrowsOnPromiseStreamBufferFailure() {
(new NativeReactor)->run(function($reactor) {
$promisor = new Future;
$promisor = new Deferred;
$reactor->repeat(function($reactor, $watcherId) use (&$i, $promisor) {
$promisor->fail(new \Exception("test"));
}, 10);