| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Promise; |
| 4 |
|
| 5 |
class Promise implements ExtendedPromiseInterface, CancellablePromiseInterface |
| 6 |
{ |
| 7 |
private $canceller; |
| 8 |
private $result; |
| 9 |
|
| 10 |
private $handlers = []; |
| 11 |
private $progressHandlers = []; |
| 12 |
|
| 13 |
private $requiredCancelRequests = 0; |
| 14 |
private $cancelRequests = 0; |
| 15 |
|
| 16 |
public function __construct(callable $resolver, callable $canceller = null) |
| 17 |
{ |
| 18 |
$this->canceller = $canceller; |
| 19 |
$this->call($resolver); |
| 20 |
} |
| 21 |
|
| 22 |
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
| 23 |
{ |
| 24 |
if (null !== $this->result) { |
| 25 |
return $this->result->then($onFulfilled, $onRejected, $onProgress); |
| 26 |
} |
| 27 |
|
| 28 |
if (null === $this->canceller) { |
| 29 |
return new static($this->resolver($onFulfilled, $onRejected, $onProgress)); |
| 30 |
} |
| 31 |
|
| 32 |
$this->requiredCancelRequests++; |
| 33 |
|
| 34 |
return new static($this->resolver($onFulfilled, $onRejected, $onProgress), function () { |
| 35 |
if (++$this->cancelRequests < $this->requiredCancelRequests) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$this->cancel(); |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
| 44 |
{ |
| 45 |
if (null !== $this->result) { |
| 46 |
return $this->result->done($onFulfilled, $onRejected, $onProgress); |
| 47 |
} |
| 48 |
|
| 49 |
$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected) { |
| 50 |
$promise |
| 51 |
->done($onFulfilled, $onRejected); |
| 52 |
}; |
| 53 |
|
| 54 |
if ($onProgress) { |
| 55 |
$this->progressHandlers[] = $onProgress; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function otherwise(callable $onRejected) |
| 60 |
{ |
| 61 |
return $this->then(null, function ($reason) use ($onRejected) { |
| 62 |
if (!_checkTypehint($onRejected, $reason)) { |
| 63 |
return new RejectedPromise($reason); |
| 64 |
} |
| 65 |
|
| 66 |
return $onRejected($reason); |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
public function always(callable $onFulfilledOrRejected) |
| 71 |
{ |
| 72 |
return $this->then(function ($value) use ($onFulfilledOrRejected) { |
| 73 |
return resolve($onFulfilledOrRejected())->then(function () use ($value) { |
| 74 |
return $value; |
| 75 |
}); |
| 76 |
}, function ($reason) use ($onFulfilledOrRejected) { |
| 77 |
return resolve($onFulfilledOrRejected())->then(function () use ($reason) { |
| 78 |
return new RejectedPromise($reason); |
| 79 |
}); |
| 80 |
}); |
| 81 |
} |
| 82 |
|
| 83 |
public function progress(callable $onProgress) |
| 84 |
{ |
| 85 |
return $this->then(null, null, $onProgress); |
| 86 |
} |
| 87 |
|
| 88 |
public function cancel() |
| 89 |
{ |
| 90 |
if (null === $this->canceller || null !== $this->result) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$canceller = $this->canceller; |
| 95 |
$this->canceller = null; |
| 96 |
|
| 97 |
$this->call($canceller); |
| 98 |
} |
| 99 |
|
| 100 |
private function resolver(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
| 101 |
{ |
| 102 |
return function ($resolve, $reject, $notify) use ($onFulfilled, $onRejected, $onProgress) { |
| 103 |
if ($onProgress) { |
| 104 |
$progressHandler = function ($update) use ($notify, $onProgress) { |
| 105 |
try { |
| 106 |
$notify($onProgress($update)); |
| 107 |
} catch (\Throwable $e) { |
| 108 |
$notify($e); |
| 109 |
} catch (\Exception $e) { |
| 110 |
$notify($e); |
| 111 |
} |
| 112 |
}; |
| 113 |
} else { |
| 114 |
$progressHandler = $notify; |
| 115 |
} |
| 116 |
|
| 117 |
$this->handlers[] = function (ExtendedPromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject, $progressHandler) { |
| 118 |
$promise |
| 119 |
->then($onFulfilled, $onRejected) |
| 120 |
->done($resolve, $reject, $progressHandler); |
| 121 |
}; |
| 122 |
|
| 123 |
$this->progressHandlers[] = $progressHandler; |
| 124 |
}; |
| 125 |
} |
| 126 |
|
| 127 |
private function resolve($value = null) |
| 128 |
{ |
| 129 |
if (null !== $this->result) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
$this->settle(resolve($value)); |
| 134 |
} |
| 135 |
|
| 136 |
private function reject($reason = null) |
| 137 |
{ |
| 138 |
if (null !== $this->result) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
$this->settle(reject($reason)); |
| 143 |
} |
| 144 |
|
| 145 |
private function notify($update = null) |
| 146 |
{ |
| 147 |
if (null !== $this->result) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
foreach ($this->progressHandlers as $handler) { |
| 152 |
$handler($update); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
private function settle(ExtendedPromiseInterface $promise) |
| 157 |
{ |
| 158 |
$promise = $this->unwrap($promise); |
| 159 |
|
| 160 |
$handlers = $this->handlers; |
| 161 |
|
| 162 |
$this->progressHandlers = $this->handlers = []; |
| 163 |
$this->result = $promise; |
| 164 |
|
| 165 |
foreach ($handlers as $handler) { |
| 166 |
$handler($promise); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
private function unwrap($promise) |
| 171 |
{ |
| 172 |
$promise = $this->extract($promise); |
| 173 |
|
| 174 |
while ($promise instanceof self && null !== $promise->result) { |
| 175 |
$promise = $this->extract($promise->result); |
| 176 |
} |
| 177 |
|
| 178 |
return $promise; |
| 179 |
} |
| 180 |
|
| 181 |
private function extract($promise) |
| 182 |
{ |
| 183 |
if ($promise instanceof LazyPromise) { |
| 184 |
$promise = $promise->promise(); |
| 185 |
} |
| 186 |
|
| 187 |
if ($promise === $this) { |
| 188 |
return new RejectedPromise( |
| 189 |
new \LogicException('Cannot resolve a promise with itself.') |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
return $promise; |
| 194 |
} |
| 195 |
|
| 196 |
private function call(callable $callback) |
| 197 |
{ |
| 198 |
try { |
| 199 |
$callback( |
| 200 |
function ($value = null) { |
| 201 |
$this->resolve($value); |
| 202 |
}, |
| 203 |
function ($reason = null) { |
| 204 |
$this->reject($reason); |
| 205 |
}, |
| 206 |
function ($update = null) { |
| 207 |
$this->notify($update); |
| 208 |
} |
| 209 |
); |
| 210 |
} catch (\Throwable $e) { |
| 211 |
$this->reject($e); |
| 212 |
} catch (\Exception $e) { |
| 213 |
$this->reject($e); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|