Skip to content
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

Initialize Deferred's state in the constructor #171

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,27 @@ final class Deferred implements PromisorInterface
private $promise;
private $resolveCallback;
private $rejectCallback;
private $canceller;

public function __construct(callable $canceller = null)
{
$this->canceller = $canceller;
$this->promise = new Promise(function ($resolve, $reject): void {
$this->resolveCallback = $resolve;
$this->rejectCallback = $reject;
}, $canceller);
}

public function promise(): PromiseInterface
{
if (null === $this->promise) {
$canceller = $this->canceller;
$this->canceller = null;

$this->promise = new Promise(function ($resolve, $reject): void {
$this->resolveCallback = $resolve;
$this->rejectCallback = $reject;
}, $canceller);
}

return $this->promise;
}

public function resolve($value = null): void
{
$this->promise();

($this->resolveCallback)($value);
}

public function reject(\Throwable $reason): void
{
$this->promise();

($this->rejectCallback)($reason);
}
}