Skip to content

Commit 67d0682

Browse files
committed
Initial commit, build 6030
0 parents  commit 67d0682

8 files changed

+1621
-0
lines changed

Deferred.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Kraken\Promise;
4+
5+
use Exception;
6+
7+
class Deferred implements DeferredInterface
8+
{
9+
/**
10+
* @var PromiseInterface|null
11+
*/
12+
protected $promise;
13+
14+
/**
15+
* @var callable
16+
*/
17+
protected $resolveCallback;
18+
19+
/**
20+
* @var callable
21+
*/
22+
protected $rejectCallback;
23+
24+
/**
25+
* @var callable
26+
*/
27+
protected $cancelCallback;
28+
29+
/**
30+
* @var callable
31+
*/
32+
protected $notifyCallback;
33+
34+
/**
35+
*
36+
*/
37+
public function __construct()
38+
{
39+
$this->promise = null;
40+
$this->resolveCallback = function($value = null) {};
41+
$this->rejectCallback = function($reason = null) {};
42+
$this->cancelCallback = function($reason = null) {};
43+
$this->notifyCallback = function($update = null) {};
44+
}
45+
46+
/**
47+
*
48+
*/
49+
public function __destruct()
50+
{
51+
unset($this->promise);
52+
unset($this->resolveCallback);
53+
unset($this->rejectCallback);
54+
unset($this->cancelCallback);
55+
unset($this->notifyCallback);
56+
}
57+
58+
/**
59+
* @return PromiseInterface
60+
*/
61+
public function promise()
62+
{
63+
if (null === $this->promise)
64+
{
65+
$this->promise = new Promise(function($resolve, $reject, $cancel, $notify) {
66+
$this->resolveCallback = $resolve;
67+
$this->rejectCallback = $reject;
68+
$this->cancelCallback = $cancel;
69+
$this->notifyCallback = $notify;
70+
});
71+
}
72+
73+
return $this->promise;
74+
}
75+
76+
/**
77+
* @param mixed|null $value
78+
* @return PromiseInterface
79+
*/
80+
public function resolve($value = null)
81+
{
82+
$this->promise();
83+
84+
return call_user_func($this->resolveCallback, $value);
85+
}
86+
87+
/**
88+
* @param Exception|string|null $reason
89+
* @return PromiseInterface
90+
*/
91+
public function reject($reason = null)
92+
{
93+
$this->promise();
94+
95+
return call_user_func($this->rejectCallback, $reason);
96+
}
97+
98+
/**
99+
* @param Exception|string|null $reason
100+
* @return PromiseInterface
101+
*/
102+
public function cancel($reason = null)
103+
{
104+
$this->promise();
105+
106+
return call_user_func($this->cancelCallback, $reason);
107+
}
108+
109+
/**
110+
* @param mixed|null $update
111+
* @return PromiseInterface
112+
*/
113+
public function notify($update = null)
114+
{
115+
$this->promise();
116+
117+
return call_user_func($this->notifyCallback, $update);
118+
}
119+
}

DeferredInterface.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Kraken\Promise;
4+
5+
use Exception;
6+
7+
interface DeferredInterface
8+
{
9+
/**
10+
* @param mixed|null $value
11+
* @return PromiseInterface
12+
*/
13+
public function resolve($value = null);
14+
15+
/**
16+
* @param Exception|string|null $reason
17+
* @return PromiseInterface
18+
*/
19+
public function reject($reason = null);
20+
21+
/**
22+
* @param Exception|string|null $reason
23+
* @return PromiseInterface
24+
*/
25+
public function cancel($reason = null);
26+
27+
/**
28+
* @param mixed|null $update
29+
* @return PromiseInterface
30+
*/
31+
public function notify($update = null);
32+
}

0 commit comments

Comments
 (0)