-
Notifications
You must be signed in to change notification settings - Fork 53
Minor cleanup #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Minor cleanup #162
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,16 +13,34 @@ | |
*/ | ||
final class Deferred implements Promise | ||
{ | ||
/** | ||
* @var ResponseInterface|null | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* @var Exception|null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be Psr\Http\Client\ClientExceptionInterface ? (i see that for now the type declaration in the method is the httplug exception interface, but psr exception would be future proof... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are correct. Thanks |
||
*/ | ||
private $failure; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $state; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
private $waitCallback; | ||
|
||
/** | ||
* @var callable[] | ||
*/ | ||
private $onFulfilledCallbacks; | ||
|
||
/** | ||
* @var callable[] | ||
*/ | ||
private $onRejectedCallbacks; | ||
|
||
public function __construct(callable $waitCallback) | ||
|
@@ -81,12 +99,12 @@ public function getState(): string | |
*/ | ||
public function resolve(ResponseInterface $response): void | ||
{ | ||
if (self::PENDING !== $this->state) { | ||
if (Promise::PENDING !== $this->state) { | ||
return; | ||
} | ||
|
||
$this->value = $response; | ||
$this->state = self::FULFILLED; | ||
$this->state = Promise::FULFILLED; | ||
|
||
foreach ($this->onFulfilledCallbacks as $onFulfilledCallback) { | ||
$onFulfilledCallback($response); | ||
|
@@ -98,12 +116,12 @@ public function resolve(ResponseInterface $response): void | |
*/ | ||
public function reject(Exception $exception): void | ||
{ | ||
if (self::PENDING !== $this->state) { | ||
if (Promise::PENDING !== $this->state) { | ||
return; | ||
} | ||
|
||
$this->failure = $exception; | ||
$this->state = self::REJECTED; | ||
$this->state = Promise::REJECTED; | ||
|
||
foreach ($this->onRejectedCallbacks as $onRejectedCallback) { | ||
$onRejectedCallback($exception); | ||
|
@@ -115,16 +133,16 @@ public function reject(Exception $exception): void | |
*/ | ||
public function wait($unwrap = true) | ||
{ | ||
if (self::PENDING === $this->state) { | ||
if (Promise::PENDING === $this->state) { | ||
$callback = $this->waitCallback; | ||
$callback(); | ||
} | ||
|
||
if (!$unwrap) { | ||
return; | ||
return null; | ||
} | ||
|
||
if (self::FULFILLED === $this->state) { | ||
if (Promise::FULFILLED === $this->state) { | ||
return $this->value; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imho it would make sense to have phpdoc for the types of all fields here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you