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; 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 { interface Promise {
/** /**

View File

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

View File

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

View File

@ -163,7 +163,7 @@ function all(array $promises) {
$results = []; $results = [];
$remaining = count($promises); $remaining = count($promises);
$promisor = new Future; $promisor = new Deferred;
foreach ($promises as $key => $resolvable) { foreach ($promises as $key => $resolvable) {
if (!$resolvable instanceof Promise) { if (!$resolvable instanceof Promise) {
@ -215,7 +215,7 @@ function some(array $promises) {
$errors = []; $errors = [];
$results = []; $results = [];
$remaining = count($promises); $remaining = count($promises);
$promisor = new Future; $promisor = new Deferred;
foreach ($promises as $key => $resolvable) { foreach ($promises as $key => $resolvable) {
if (!$resolvable instanceof Promise) { if (!$resolvable instanceof Promise) {
@ -258,7 +258,7 @@ function any(array $promises) {
$results = []; $results = [];
$errors = []; $errors = [];
$remaining = count($promises); $remaining = count($promises);
$promisor = new Future; $promisor = new Deferred;
foreach ($promises as $key => $resolvable) { foreach ($promises as $key => $resolvable) {
if (!$resolvable instanceof Promise) { if (!$resolvable instanceof Promise) {
@ -294,7 +294,7 @@ function first(array $promises) {
$remaining = count($promises); $remaining = count($promises);
$isComplete = false; $isComplete = false;
$promisor = new Future; $promisor = new Deferred;
foreach ($promises as $resolvable) { foreach ($promises as $resolvable) {
if (!$resolvable instanceof Promise) { if (!$resolvable instanceof Promise) {
@ -304,7 +304,7 @@ function first(array $promises) {
$promise->when(function($error, $result) use (&$remaining, &$isComplete, $promisor) { $promise->when(function($error, $result) use (&$remaining, &$isComplete, $promisor) {
if ($isComplete) { 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; return;
} elseif ($error && --$remaining === 0) { } elseif ($error && --$remaining === 0) {
$promisor->fail(new \RuntimeException( $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) { function map(array $promises, callable $functor) {
if (empty($promises)) { if (empty($promises)) {
@ -330,7 +330,7 @@ function map(array $promises, callable $functor) {
$results = []; $results = [];
$remaining = count($promises); $remaining = count($promises);
$promisor = new Future; $promisor = new Deferred;
foreach ($promises as $key => $resolvable) { foreach ($promises as $key => $resolvable) {
$promise = ($resolvable instanceof Promise) ? $resolvable : new Success($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 * 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. * 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 = []; $results = [];
$remaining = count($promises); $remaining = count($promises);
$promisor = new Future; $promisor = new Deferred;
foreach ($promises as $key => $resolvable) { foreach ($promises as $key => $resolvable) {
$promise = ($resolvable instanceof Promise) ? $resolvable : new Success($resolvable); $promise = ($resolvable instanceof Promise) ? $resolvable : new Success($resolvable);
$promise->when(function($error, $result) use (&$remaining, &$results, $key, $promisor, $functor) { $promise->when(function($error, $result) use (&$remaining, &$results, $key, $promisor, $functor) {
if (empty($remaining)) { if (empty($remaining)) {
// If the future result already failed we don't bother. // If the deferred result already failed we don't bother.
return; return;
} }
if ($error) { if ($error) {
@ -465,7 +465,7 @@ function resolve(\Generator $generator, Reactor $reactor = null, callable $promi
*/ */
$cs = new \StdClass; $cs = new \StdClass;
$cs->reactor = $reactor ?: getReactor(); $cs->reactor = $reactor ?: getReactor();
$cs->promisor = new Future; $cs->promisor = new Deferred;
$cs->generator = $generator; $cs->generator = $generator;
$cs->promisifier = $promisifier; $cs->promisifier = $promisifier;

View File

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