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
|
|
|
|
|
|
|
/**
|
|
|
|
* A Promisor represents a contract to resolve a deferred value at some point in the future
|
|
|
|
*
|
|
|
|
* A Promisor resolves its associated placeholder value (Promise) Promisor::succeed() or
|
|
|
|
* Promisor::fail(). Promisor::update() may be used to notify watchers of progress resolving
|
2015-05-19 06:21:33 +02:00
|
|
|
* the deferred value.
|
2014-09-22 22:47:48 +02:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* function myAsyncProducer() {
|
2015-03-19 16:14:21 +01:00
|
|
|
* // Create a new promisor that needs to be resolved
|
2015-05-19 06:21:33 +02:00
|
|
|
* $promisor = new Amp\Deferred;
|
2014-09-22 22:47:48 +02:00
|
|
|
*
|
|
|
|
* // When we eventually finish non-blocking value resolution we
|
|
|
|
* // simply call the relevant Promise method to notify any code
|
|
|
|
* // with references to the Promisor's associated Promise:
|
2015-03-19 16:14:21 +01:00
|
|
|
* // $promisor->succeed($value) -or- $promisor->fail($exceptionObj)
|
2014-09-22 22:47:48 +02:00
|
|
|
*
|
2015-03-19 16:14:21 +01:00
|
|
|
* return $promisor->promise();
|
2014-09-22 22:47:48 +02:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* The following correlations exist between Promisor and Promise methods:
|
|
|
|
*
|
|
|
|
* - Promisor::update | Promise::watch
|
|
|
|
* - Promisor::succeed | Promise::when
|
|
|
|
* - Promisor::fail | Promise::when
|
|
|
|
*/
|
|
|
|
interface Promisor {
|
|
|
|
/**
|
2015-05-19 06:21:33 +02:00
|
|
|
* Promise deferred fulfillment via a temporary placeholder value
|
2015-03-19 16:14:21 +01:00
|
|
|
*
|
2014-09-23 04:38:32 +02: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
|
|
|
|
|
|
|
/**
|
|
|
|
* Update watchers of progress resolving the promised value
|
|
|
|
*
|
|
|
|
* @param mixed $progress
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-04-30 19:41:14 +02:00
|
|
|
public function update($progress);
|
2014-09-22 22:47:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve the promised value as a success
|
|
|
|
*
|
|
|
|
* @param mixed $result
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function succeed($result = null);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve the promised value as a failure
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function fail(\Exception $error);
|
|
|
|
}
|