1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Promise; |
4
|
|
|
|
5
|
|
|
use Error; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class Deferred implements DeferredInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var PromiseInterface|null |
12
|
|
|
*/ |
13
|
|
|
protected $promise; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var callable |
17
|
|
|
*/ |
18
|
|
|
protected $resolveCallback; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var callable |
22
|
|
|
*/ |
23
|
|
|
protected $rejectCallback; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var callable |
27
|
|
|
*/ |
28
|
|
|
protected $cancelCallback; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var callable|null |
32
|
|
|
*/ |
33
|
|
|
protected $canceller; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param callable $canceller |
37
|
|
|
*/ |
38
|
167 |
|
public function __construct($canceller = null) |
39
|
|
|
{ |
40
|
167 |
|
$this->promise = null; |
41
|
|
|
$this->resolveCallback = function($value = null) {}; |
|
|
|
|
42
|
|
|
$this->rejectCallback = function($reason = null) {}; |
|
|
|
|
43
|
|
|
$this->cancelCallback = function($reason = null) {}; |
|
|
|
|
44
|
167 |
|
$this->canceller = $canceller; |
45
|
167 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
*/ |
50
|
167 |
|
public function __destruct() |
51
|
|
|
{ |
52
|
167 |
|
unset($this->promise); |
53
|
167 |
|
unset($this->resolveCallback); |
54
|
167 |
|
unset($this->rejectCallback); |
55
|
167 |
|
unset($this->cancelCallback); |
56
|
167 |
|
unset($this->canceller); |
57
|
167 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @override |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
167 |
|
public function getPromise() |
64
|
|
|
{ |
65
|
167 |
|
if (null === $this->promise) |
66
|
|
|
{ |
67
|
167 |
|
$this->promise = new Promise(function($resolve, $reject, $cancel) { |
68
|
167 |
|
$this->resolveCallback = $resolve; |
69
|
167 |
|
$this->rejectCallback = $reject; |
70
|
167 |
|
$this->cancelCallback = $cancel; |
71
|
167 |
|
}, $this->canceller); |
72
|
|
|
} |
73
|
|
|
|
74
|
167 |
|
return $this->promise; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @override |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
52 |
|
public function resolve($value = null) |
82
|
|
|
{ |
83
|
52 |
|
$this->getPromise(); |
84
|
|
|
|
85
|
52 |
|
return call_user_func($this->resolveCallback, $value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @override |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
50 |
|
public function reject($reason = null) |
93
|
|
|
{ |
94
|
50 |
|
$this->getPromise(); |
95
|
|
|
|
96
|
50 |
|
return call_user_func($this->rejectCallback, $reason); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @override |
101
|
|
|
* @inheritDoc |
102
|
|
|
*/ |
103
|
41 |
|
public function cancel($reason = null) |
104
|
|
|
{ |
105
|
41 |
|
$this->getPromise(); |
106
|
|
|
|
107
|
41 |
|
return call_user_func($this->cancelCallback, $reason); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.