Skip to content

Commit

Permalink
Merge pull request #171 from Kubo2/oo-deferred
Browse files Browse the repository at this point in the history
Initialize Deferred's state in the constructor
  • Loading branch information
jsor committed Aug 19, 2020
2 parents ee56552 + c06be4d commit 12dcf28
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/Deferred.php
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);
}
}

0 comments on commit 12dcf28

Please sign in to comment.