|
4 | 4 |
|
5 | 5 | class Deferred implements PromisorInterface
|
6 | 6 | {
|
7 |
| - private $completed; |
8 | 7 | private $promise;
|
9 |
| - private $handlers = []; |
10 |
| - private $progressHandlers = []; |
11 |
| - |
12 |
| - public function resolve($value = null) |
13 |
| - { |
14 |
| - if (null !== $this->completed) { |
15 |
| - return resolve($value); |
16 |
| - } |
17 |
| - |
18 |
| - $this->completed = resolve($value); |
19 |
| - |
20 |
| - $this->processQueue($this->handlers, $this->completed); |
21 |
| - |
22 |
| - $this->progressHandlers = $this->handlers = []; |
23 |
| - |
24 |
| - return $this->completed; |
25 |
| - } |
26 |
| - |
27 |
| - public function reject($reason = null) |
28 |
| - { |
29 |
| - return $this->resolve(reject($reason)); |
30 |
| - } |
31 |
| - |
32 |
| - public function progress($update = null) |
33 |
| - { |
34 |
| - if (null !== $this->completed) { |
35 |
| - return; |
36 |
| - } |
37 |
| - |
38 |
| - $this->processQueue($this->progressHandlers, $update); |
39 |
| - } |
| 8 | + private $resolveCallback; |
| 9 | + private $rejectCallback; |
| 10 | + private $progressCallback; |
40 | 11 |
|
41 | 12 | public function promise()
|
42 | 13 | {
|
43 | 14 | if (null === $this->promise) {
|
44 |
| - $this->promise = new DeferredPromise($this->getThenCallback()); |
| 15 | + $this->promise = new Promise(function ($resolve, $reject, $progress) { |
| 16 | + $this->resolveCallback = $resolve; |
| 17 | + $this->rejectCallback = $reject; |
| 18 | + $this->progressCallback = $progress; |
| 19 | + }); |
45 | 20 | }
|
46 | 21 |
|
47 | 22 | return $this->promise;
|
48 | 23 | }
|
49 | 24 |
|
50 |
| - protected function getThenCallback() |
| 25 | + public function resolve($value = null) |
51 | 26 | {
|
52 |
| - return function (callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) { |
53 |
| - if (null !== $this->completed) { |
54 |
| - return $this->completed->then($onFulfilled, $onRejected, $onProgress); |
55 |
| - } |
56 |
| - |
57 |
| - $deferred = new static(); |
| 27 | + $this->promise(); |
58 | 28 |
|
59 |
| - if (is_callable($onProgress)) { |
60 |
| - $progHandler = function ($update) use ($deferred, $onProgress) { |
61 |
| - try { |
62 |
| - $deferred->progress(call_user_func($onProgress, $update)); |
63 |
| - } catch (\Exception $e) { |
64 |
| - $deferred->progress($e); |
65 |
| - } |
66 |
| - }; |
67 |
| - } else { |
68 |
| - $progHandler = [$deferred, 'progress']; |
69 |
| - } |
70 |
| - |
71 |
| - $this->handlers[] = function ($promise) use ($onFulfilled, $onRejected, $deferred, $progHandler) { |
72 |
| - $promise |
73 |
| - ->then($onFulfilled, $onRejected) |
74 |
| - ->then( |
75 |
| - [$deferred, 'resolve'], |
76 |
| - [$deferred, 'reject'], |
77 |
| - $progHandler |
78 |
| - ); |
79 |
| - }; |
| 29 | + return call_user_func($this->resolveCallback, $value); |
| 30 | + } |
80 | 31 |
|
81 |
| - $this->progressHandlers[] = $progHandler; |
| 32 | + public function reject($reason = null) |
| 33 | + { |
| 34 | + $this->promise(); |
82 | 35 |
|
83 |
| - return $deferred->promise(); |
84 |
| - }; |
| 36 | + return call_user_func($this->rejectCallback, $reason); |
85 | 37 | }
|
86 | 38 |
|
87 |
| - protected function processQueue($queue, $value) |
| 39 | + public function progress($update = null) |
88 | 40 | {
|
89 |
| - foreach ($queue as $handler) { |
90 |
| - call_user_func($handler, $value); |
91 |
| - } |
| 41 | + $this->promise(); |
| 42 | + |
| 43 | + return call_user_func($this->progressCallback, $update); |
92 | 44 | }
|
93 | 45 | }
|
0 commit comments