namespace seekat\API\Call;
-use AsyncInterop\Promise;
-use Exception;
-use http\{
- Client, Client\Request, Client\Response
-};
+use http\{Client, Client\Request, Client\Response};
use Psr\Log\LoggerInterface;
use seekat\API;
-use SplObserver;
-use SplSubject;
-final class Deferred implements SplObserver
-{
+final class Deferred {
/**
* The response importer
*
private $response;
/**
- * @var Promise
+ * @var mixed
*/
private $promise;
*/
private $reject;
- /**
- * @var \Closure
- */
- private $update;
-
/**
* Create a deferred promise for the response of $request
*
/* we did finish in the meantime */
$this->complete();
} else {
- $this->client->detach($this);
$this->client->dequeue($this->request);
($this->reject)("Cancelled");
}
$this->promise = $future->getPromise($context);
$this->resolve = API\Future\resolver($future, $context);
$this->reject = API\Future\rejecter($future, $context);
- $this->update = API\Future\updater($future, $context);
}
- function __invoke() : Promise {
- if ($this->cache->load($this->request, $cached)) {
- $this->logger->info("deferred -> cached", [
- "method" => $this->request->getRequestMethod(),
- "url" => $this->request->getRequestUrl(),
- ]);
+ function __invoke() {
+ if (!$this->cached($cached)) {
+ $this->refresh($cached);
+ }
- $this->response = $cached;
- $this->complete();
+ return $this->promise;
+ }
+
+ /**
+ * Peek into cache
+ *
+ * @param Response $cached
+ * @return bool
+ */
+ private function cached(Response &$cached = null) : bool {
+ $fresh = $this->cache->load($this->request, $cachedResponse);
+
+ if (!$cachedResponse) {
+ return false;
} else {
- $this->client->attach($this);
- $this->client->enqueue($this->request, function(Response $response) use($cached) {
- if ($response->getResponseCode() == 304) {
- $this->response = $cached;
- } else {
- $this->response = $response;
- }
- $this->complete();
- return true;
- });
- $this->logger->info("deferred -> enqueued", [
+ $cached = $cachedResponse;
+
+ $this->logger->info("deferred -> cached", [
"method" => $this->request->getRequestMethod(),
"url" => $this->request->getRequestUrl(),
]);
- /* start off */
- $this->client->once();
+
+
+ if (!$fresh) {
+ $this->logger->info("cached -> stale", [
+ "method" => $this->request->getRequestMethod(),
+ "url" => $this->request->getRequestUrl(),
+ ]);
+ return false;
+ }
}
- return $this->promise;
+ $this->response = $cached;
+ $this->complete("cached");
+ return true;
}
/**
- * Progress observer
+ * Refresh
*
- * Import the response's data on success and resolve the promise.
- *
- * @param SplSubject $client The observed HTTP client
- * @param Request $request The request which generated the update
- * @param object $progress The progress information
+ * @param Response|null $cached
*/
- function update(SplSubject $client, Request $request = null, $progress = null) {
- if ($request !== $this->request) {
- return;
- }
+ private function refresh(Response $cached = null) {
+ $this->client->enqueue($this->request, function(Response $response) use($cached) {
+ $this->response = $response;
+ $this->complete();
+ return true;
+ });
- ($this->update)((object) compact("client", "request", "progress"));
+ $this->logger->info(($cached ? "stale" : "deferred") . " -> enqueued", [
+ "method" => $this->request->getRequestMethod(),
+ "url" => $this->request->getRequestUrl(),
+ ]);
+
+ /* start off */
+ $this->client->once();
}
/**
* Completion callback
- * @param callable $resolve
- * @param callable $reject
*/
- private function complete() {
- $this->client->detach($this);
-
+ private function complete(string $by = "enqueued") {
if ($this->response) {
- try {
- $api = ($this->result)($this->response);
-
- $this->cache->save($this->request, $this->response);
+ $this->logger->info("$by -> response", [
+ "url" => $this->request->getRequestUrl(),
+ "info" => $this->response->getInfo(),
+ ]);
- ($this->resolve)($api);
- } catch (Exception $e) {
+ try {
+ $this->cache->update($this->request, $this->response);
+ ($this->resolve)(($this->result)($this->response));
+ } catch (\Throwable $e) {
($this->reject)($e);
}
} else {
- ($this->reject)($this->client->getTransferInfo($this->request)->error);
+ $info = $this->client->getTransferInfo($this->request);
+
+ $this->logger->warning("$by -> no response", [
+ "url" => $this->request->getRequestUrl(),
+ "info" => $info
+ ]);
+
+ ($this->reject)($info->error);
}
}