Chris@0: waitFn = $waitFn; Chris@0: $this->cancelFn = $cancelFn; Chris@0: } Chris@0: Chris@0: public function then( Chris@0: callable $onFulfilled = null, Chris@0: callable $onRejected = null Chris@0: ) { Chris@0: if ($this->state === self::PENDING) { Chris@0: $p = new Promise(null, [$this, 'cancel']); Chris@0: $this->handlers[] = [$p, $onFulfilled, $onRejected]; Chris@0: $p->waitList = $this->waitList; Chris@0: $p->waitList[] = $this; Chris@0: return $p; Chris@0: } Chris@0: Chris@0: // Return a fulfilled promise and immediately invoke any callbacks. Chris@0: if ($this->state === self::FULFILLED) { Chris@0: return $onFulfilled Chris@0: ? promise_for($this->result)->then($onFulfilled) Chris@0: : promise_for($this->result); Chris@0: } Chris@0: Chris@0: // It's either cancelled or rejected, so return a rejected promise Chris@0: // and immediately invoke any callbacks. Chris@0: $rejection = rejection_for($this->result); Chris@0: return $onRejected ? $rejection->then(null, $onRejected) : $rejection; Chris@0: } Chris@0: Chris@0: public function otherwise(callable $onRejected) Chris@0: { Chris@0: return $this->then(null, $onRejected); Chris@0: } Chris@0: Chris@0: public function wait($unwrap = true) Chris@0: { Chris@0: $this->waitIfPending(); Chris@0: Chris@0: $inner = $this->result instanceof PromiseInterface Chris@0: ? $this->result->wait($unwrap) Chris@0: : $this->result; Chris@0: Chris@0: if ($unwrap) { Chris@0: if ($this->result instanceof PromiseInterface Chris@0: || $this->state === self::FULFILLED Chris@0: ) { Chris@0: return $inner; Chris@0: } else { Chris@0: // It's rejected so "unwrap" and throw an exception. Chris@0: throw exception_for($inner); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: public function getState() Chris@0: { Chris@0: return $this->state; Chris@0: } Chris@0: Chris@0: public function cancel() Chris@0: { Chris@0: if ($this->state !== self::PENDING) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $this->waitFn = $this->waitList = null; Chris@0: Chris@0: if ($this->cancelFn) { Chris@0: $fn = $this->cancelFn; Chris@0: $this->cancelFn = null; Chris@0: try { Chris@0: $fn(); Chris@0: } catch (\Throwable $e) { Chris@0: $this->reject($e); Chris@0: } catch (\Exception $e) { Chris@0: $this->reject($e); Chris@0: } Chris@0: } Chris@0: Chris@0: // Reject the promise only if it wasn't rejected in a then callback. Chris@0: if ($this->state === self::PENDING) { Chris@0: $this->reject(new CancellationException('Promise has been cancelled')); Chris@0: } Chris@0: } Chris@0: Chris@0: public function resolve($value) Chris@0: { Chris@0: $this->settle(self::FULFILLED, $value); Chris@0: } Chris@0: Chris@0: public function reject($reason) Chris@0: { Chris@0: $this->settle(self::REJECTED, $reason); Chris@0: } Chris@0: Chris@0: private function settle($state, $value) Chris@0: { Chris@0: if ($this->state !== self::PENDING) { Chris@0: // Ignore calls with the same resolution. Chris@0: if ($state === $this->state && $value === $this->result) { Chris@0: return; Chris@0: } Chris@0: throw $this->state === $state Chris@0: ? new \LogicException("The promise is already {$state}.") Chris@0: : new \LogicException("Cannot change a {$this->state} promise to {$state}"); Chris@0: } Chris@0: Chris@0: if ($value === $this) { Chris@0: throw new \LogicException('Cannot fulfill or reject a promise with itself'); Chris@0: } Chris@0: Chris@0: // Clear out the state of the promise but stash the handlers. Chris@0: $this->state = $state; Chris@0: $this->result = $value; Chris@0: $handlers = $this->handlers; Chris@0: $this->handlers = null; Chris@0: $this->waitList = $this->waitFn = null; Chris@0: $this->cancelFn = null; Chris@0: Chris@0: if (!$handlers) { Chris@0: return; Chris@0: } Chris@0: Chris@0: // If the value was not a settled promise or a thenable, then resolve Chris@0: // it in the task queue using the correct ID. Chris@0: if (!method_exists($value, 'then')) { Chris@0: $id = $state === self::FULFILLED ? 1 : 2; Chris@0: // It's a success, so resolve the handlers in the queue. Chris@0: queue()->add(static function () use ($id, $value, $handlers) { Chris@0: foreach ($handlers as $handler) { Chris@0: self::callHandler($id, $value, $handler); Chris@0: } Chris@0: }); Chris@0: } elseif ($value instanceof Promise Chris@0: && $value->getState() === self::PENDING Chris@0: ) { Chris@0: // We can just merge our handlers onto the next promise. Chris@0: $value->handlers = array_merge($value->handlers, $handlers); Chris@0: } else { Chris@0: // Resolve the handlers when the forwarded promise is resolved. Chris@0: $value->then( Chris@0: static function ($value) use ($handlers) { Chris@0: foreach ($handlers as $handler) { Chris@0: self::callHandler(1, $value, $handler); Chris@0: } Chris@0: }, Chris@0: static function ($reason) use ($handlers) { Chris@0: foreach ($handlers as $handler) { Chris@0: self::callHandler(2, $reason, $handler); Chris@0: } Chris@0: } Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Call a stack of handlers using a specific callback index and value. Chris@0: * Chris@0: * @param int $index 1 (resolve) or 2 (reject). Chris@0: * @param mixed $value Value to pass to the callback. Chris@0: * @param array $handler Array of handler data (promise and callbacks). Chris@0: * Chris@0: * @return array Returns the next group to resolve. Chris@0: */ Chris@0: private static function callHandler($index, $value, array $handler) Chris@0: { Chris@0: /** @var PromiseInterface $promise */ Chris@0: $promise = $handler[0]; Chris@0: Chris@0: // The promise may have been cancelled or resolved before placing Chris@0: // this thunk in the queue. Chris@0: if ($promise->getState() !== self::PENDING) { Chris@0: return; Chris@0: } Chris@0: Chris@0: try { Chris@0: if (isset($handler[$index])) { Chris@0: $promise->resolve($handler[$index]($value)); Chris@0: } elseif ($index === 1) { Chris@0: // Forward resolution values as-is. Chris@0: $promise->resolve($value); Chris@0: } else { Chris@0: // Forward rejections down the chain. Chris@0: $promise->reject($value); Chris@0: } Chris@0: } catch (\Throwable $reason) { Chris@0: $promise->reject($reason); Chris@0: } catch (\Exception $reason) { Chris@0: $promise->reject($reason); Chris@0: } Chris@0: } Chris@0: Chris@0: private function waitIfPending() Chris@0: { Chris@0: if ($this->state !== self::PENDING) { Chris@0: return; Chris@0: } elseif ($this->waitFn) { Chris@0: $this->invokeWaitFn(); Chris@0: } elseif ($this->waitList) { Chris@0: $this->invokeWaitList(); Chris@0: } else { Chris@0: // If there's not wait function, then reject the promise. Chris@0: $this->reject('Cannot wait on a promise that has ' Chris@0: . 'no internal wait function. You must provide a wait ' Chris@0: . 'function when constructing the promise to be able to ' Chris@0: . 'wait on a promise.'); Chris@0: } Chris@0: Chris@0: queue()->run(); Chris@0: Chris@0: if ($this->state === self::PENDING) { Chris@0: $this->reject('Invoking the wait callback did not resolve the promise'); Chris@0: } Chris@0: } Chris@0: Chris@0: private function invokeWaitFn() Chris@0: { Chris@0: try { Chris@0: $wfn = $this->waitFn; Chris@0: $this->waitFn = null; Chris@0: $wfn(true); Chris@0: } catch (\Exception $reason) { Chris@0: if ($this->state === self::PENDING) { Chris@0: // The promise has not been resolved yet, so reject the promise Chris@0: // with the exception. Chris@0: $this->reject($reason); Chris@0: } else { Chris@0: // The promise was already resolved, so there's a problem in Chris@0: // the application. Chris@0: throw $reason; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: private function invokeWaitList() Chris@0: { Chris@0: $waitList = $this->waitList; Chris@0: $this->waitList = null; Chris@0: Chris@0: foreach ($waitList as $result) { Chris@0: while (true) { Chris@0: $result->waitIfPending(); Chris@0: Chris@0: if ($result->result instanceof Promise) { Chris@0: $result = $result->result; Chris@0: } else { Chris@0: if ($result->result instanceof PromiseInterface) { Chris@0: $result->result->wait(false); Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: }