1
0
mirror of https://github.com/danog/amp.git synced 2024-12-15 19:07:22 +01:00
amp/lib/Promisor.php

73 lines
2.2 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
/**
* 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-06-01 01:33:55 +02: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
*
2015-06-01 01:33:55 +02:00
* Implementations must support variadic argument passing to update
* even though 5.5-compatibility prevents us from specifying it as
* part of the API.
*
* @param mixed $progress1, $progress2, ... $progressN
2014-09-22 22:47:48 +02:00
* @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
*
* The error parameter used to fail a promisor must always be an exception
* instance. However, we cannot typehint this parameter in environments
* where PHP5.x compatibility is required because PHP7 Throwable
* instances will break the typehint.
*
* @param mixed $error An Exception or Throwable in PHP7 environments
2014-09-22 22:47:48 +02:00
* @return void
*/
public function fail($error);
2014-09-22 22:47:48 +02:00
}