Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

BaseFutureTrait.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 3.46 KiB


 001  <?php
 002  namespace GuzzleHttp\Ring\Future;
 003   
 004  use GuzzleHttp\Ring\Exception\CancelledFutureAccessException;
 005  use GuzzleHttp\Ring\Exception\RingException;
 006  use React\Promise\PromiseInterface;
 007   
 008  /**
 009   * Implements common future functionality built on top of promises.
 010   */
 011  trait BaseFutureTrait
 012  {
 013      /** @var callable */
 014      private $waitfn;
 015   
 016      /** @var callable */
 017      private $cancelfn;
 018   
 019      /** @var PromiseInterface */
 020      private $wrappedPromise;
 021   
 022      /** @var \Exception Error encountered. */
 023      private $error;
 024   
 025      /** @var mixed Result of the future */
 026      private $result;
 027   
 028      private $isRealized = false;
 029   
 030      /**
 031       * @param PromiseInterface $promise Promise to shadow with the future.
 032       * @param callable         $wait    Function that blocks until the deferred
 033       *                                  computation has been resolved. This
 034       *                                  function MUST resolve the deferred value
 035       *                                  associated with the supplied promise.
 036       * @param callable         $cancel  If possible and reasonable, provide a
 037       *                                  function that can be used to cancel the
 038       *                                  future from completing.
 039       */
 040      public function __construct(
 041          PromiseInterface $promise,
 042          callable $wait = null,
 043          callable $cancel = null
 044      ) {
 045          $this->wrappedPromise = $promise;
 046          $this->waitfn = $wait;
 047          $this->cancelfn = $cancel;
 048      }
 049   
 050      public function wait()
 051      {
 052          if (!$this->isRealized) {
 053              $this->addShadow();
 054              if (!$this->isRealized && $this->waitfn) {
 055                  $this->invokeWait();
 056              }
 057              if (!$this->isRealized) {
 058                  $this->error = new RingException('Waiting did not resolve future');
 059              }
 060          }
 061   
 062          if ($this->error) {
 063              throw $this->error;
 064          }
 065   
 066          return $this->result;
 067      }
 068   
 069      public function promise()
 070      {
 071          return $this->wrappedPromise;
 072      }
 073   
 074      public function then(
 075          callable $onFulfilled = null,
 076          callable $onRejected = null,
 077          callable $onProgress = null
 078      ) {
 079          return $this->wrappedPromise->then($onFulfilled, $onRejected, $onProgress);
 080      }
 081   
 082      public function cancel()
 083      {
 084          if (!$this->isRealized) {
 085              $cancelfn = $this->cancelfn;
 086              $this->waitfn = $this->cancelfn = null;
 087              $this->isRealized = true;
 088              $this->error = new CancelledFutureAccessException();
 089              if ($cancelfn) {
 090                  $cancelfn($this);
 091              }
 092          }
 093      }
 094   
 095      private function addShadow()
 096      {
 097          // Get the result and error when the promise is resolved. Note that
 098          // calling this function might trigger the resolution immediately.
 099          $this->wrappedPromise->then(
 100              function ($value) {
 101                  $this->isRealized = true;
 102                  $this->result = $value;
 103                  $this->waitfn = $this->cancelfn = null;
 104              },
 105              function ($error) {
 106                  $this->isRealized = true;
 107                  $this->error = $error;
 108                  $this->waitfn = $this->cancelfn = null;
 109              }
 110          );
 111      }
 112   
 113      private function invokeWait()
 114      {
 115          try {
 116              $wait = $this->waitfn;
 117              $this->waitfn = null;
 118              $wait();
 119          } catch (\Exception $e) {
 120              // Defer can throw to reject.
 121              $this->error = $e;
 122              $this->isRealized = true;
 123          }
 124      }
 125  }
 126