7 use http\Client\Request
;
8 use http\Client\Response
;
10 class Deferred
extends \React\Promise\Deferred
implements \SplObserver
{
25 * The executed request
26 * @var \http\Client\Request
31 * The promised response
32 * @var \http\Client\Response
37 * Create a deferred promise for the response of $request
39 * @var \seekat\API $api The endpoint of the request
40 * @var \http\Client $client The HTTP client to send the request
41 * @var \http\Client\Request The request to execute
43 function __construct(API
$api, Client
$client, Request
$request) {
45 $this->client
= $client;
46 $this->request
= $request;
48 $client->attach($this);
49 $client->enqueue($request);
51 parent
::__construct(function($resolve, $reject) {
52 return $this->cancel($resolve, $reject);
59 * Import the response's data on success and resolve the promise.
61 * @var \SplSubject $client The observed HTTP client
62 * @var \http\Client\Request The request which generated the update
63 * @var object $progress The progress information
65 function update(\SplSubject
$client, Request
$request = null, $progress = null) {
66 if ($request !== $this->request
) {
70 $this->notify((object) compact("client", "request", "progress"));
72 if ($progress->info
=== "finished") {
73 $this->response
= $this->client
->getResponse();
83 * @param callable $resolve
84 * @param callable $reject
86 private function complete(callable
$resolve, callable
$reject) {
87 $this->client
->detach($this);
89 if ($this->response
) {
91 $resolve($this->api
->import($this->response
));
92 } catch (\Exception
$e) {
96 $reject($this->client
->getTransferInfo($this->request
)["error"]);
99 $this->client
->dequeue($this->request
);
103 * Cancellation callback
104 * @param callable $resolve
105 * @param callable $reject
107 private function cancel(callable
$resolve, callable
$reject) {
108 /* did we finish in the meantime? */
109 if ($this->response
) {
110 $this->complete($resolve, $reject);
112 $this->client
->detach($this);
113 $this->client
->dequeue($this->request
);
114 $reject("Cancelled");