Exemple #1
0
 public function __construct(float $seconds, Promise $promise)
 {
     $this->promise = $promise;
     $promise->when(function (\Throwable $e = null, ...$val) {
         if ($this->state === self::PENDING) {
             Loop::cancel($this->watcher);
             if ($e) {
                 $this->fail($e);
             } else {
                 $this->resolve(...$val);
             }
         }
     });
     if ($this->state === self::PENDING) {
         $this->watcher = Loop::delay((int) \floor($seconds * 1000), function () use($seconds) {
             $this->watcher = null;
             if ($this->state === self::PENDING) {
                 $e = new TimeoutException(\sprintf('Operation timed out after %.3f seconds', $seconds));
                 if ($this->promise instanceof Awaitable) {
                     $this->state = self::FAILED;
                     try {
                         $this->promise->cancel($e);
                     } finally {
                         $this->state = self::PENDING;
                     }
                 }
                 $this->fail($e);
             }
         });
     }
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  */
 public function cancel(\Throwable $e) : array
 {
     if ($this->state === self::PENDING) {
         $this->disabled = true;
         try {
             if ($this->promise instanceof Awaitable) {
                 return $this->promise->cancel($e);
             }
         } finally {
             $this->fail($e);
         }
     }
     return [];
 }
Exemple #3
0
 public function __construct(Promise $promise, CancellationTokenSource $source = null)
 {
     $this->promise = $promise;
     $this->source = $source;
     $promise->when(function (\Throwable $e = null, ...$val) {
         if ($this->state === self::PENDING) {
             if ($e) {
                 $this->fail($e);
             } else {
                 $this->resolve(...$val);
             }
         }
     });
 }
Exemple #4
0
 /**
  * {@inheritdoc}
  */
 public function when(callable $onResolved)
 {
     if (null === $this->promise) {
         $provider = $this->provider;
         $this->provider = null;
         try {
             $this->promise = $provider();
             if (!$this->promise instanceof Promise) {
                 $this->promise = new Success($this->promise);
             }
         } catch (\Throwable $exception) {
             $this->promise = new Failure($exception);
         }
     }
     $this->promise->when($onResolved);
 }
Exemple #5
0
 public function __construct(Promise $promise, callable $callback, bool $expand = false)
 {
     $this->promise = $promise;
     $promise->when(function (\Throwable $error = null, ...$val) use($callback, $expand) {
         if ($this->state === self::PENDING) {
             if ($error) {
                 $this->fail($error);
             } else {
                 if ($expand) {
                     $this->resolve($callback(...$val[0]));
                 } else {
                     $this->resolve($callback(...$val));
                 }
             }
         }
     });
 }