3
3
namespace Clue \React \Redis ;
4
4
5
5
use Evenement \EventEmitter ;
6
- use React \Stream \Util ;
7
6
use React \EventLoop \LoopInterface ;
7
+ use React \EventLoop \TimerInterface ;
8
+ use React \Promise \PromiseInterface ;
9
+ use React \Stream \Util ;
8
10
use function React \Promise \reject ;
9
11
10
12
/**
11
13
* @internal
12
14
*/
13
15
class LazyClient extends EventEmitter implements Client
14
16
{
17
+ /** @var string */
15
18
private $ target ;
19
+
16
20
/** @var Factory */
17
21
private $ factory ;
22
+
23
+ /** @var bool */
18
24
private $ closed = false ;
19
- private $ promise ;
20
25
26
+ /** @var ?PromiseInterface */
27
+ private $ promise = null ;
28
+
29
+ /** @var LoopInterface */
21
30
private $ loop ;
31
+
32
+ /** @var float */
22
33
private $ idlePeriod = 60.0 ;
23
- private $ idleTimer ;
34
+
35
+ /** @var ?TimerInterface */
36
+ private $ idleTimer = null ;
37
+
38
+ /** @var int */
24
39
private $ pending = 0 ;
25
40
41
+ /** @var array<string,bool> */
26
42
private $ subscribed = [];
43
+
44
+ /** @var array<string,bool> */
27
45
private $ psubscribed = [];
28
46
29
- /**
30
- * @param $target
31
- */
32
- public function __construct ($ target , Factory $ factory , LoopInterface $ loop )
47
+ public function __construct (string $ target , Factory $ factory , LoopInterface $ loop )
33
48
{
34
49
$ args = [];
35
50
\parse_str ((string ) \parse_url ($ target , \PHP_URL_QUERY ), $ args );
@@ -42,7 +57,7 @@ public function __construct($target, Factory $factory, LoopInterface $loop)
42
57
$ this ->loop = $ loop ;
43
58
}
44
59
45
- private function client ()
60
+ private function client (): PromiseInterface
46
61
{
47
62
if ($ this ->promise !== null ) {
48
63
return $ this ->promise ;
@@ -71,16 +86,16 @@ private function client()
71
86
});
72
87
73
88
// keep track of all channels and patterns this connection is subscribed to
74
- $ redis ->on ('subscribe ' , function ($ channel ) {
89
+ $ redis ->on ('subscribe ' , function (string $ channel ) {
75
90
$ this ->subscribed [$ channel ] = true ;
76
91
});
77
- $ redis ->on ('psubscribe ' , function ($ pattern ) {
92
+ $ redis ->on ('psubscribe ' , function (string $ pattern ) {
78
93
$ this ->psubscribed [$ pattern ] = true ;
79
94
});
80
- $ redis ->on ('unsubscribe ' , function ($ channel ) {
95
+ $ redis ->on ('unsubscribe ' , function (string $ channel ) {
81
96
unset($ this ->subscribed [$ channel ]);
82
97
});
83
- $ redis ->on ('punsubscribe ' , function ($ pattern ) {
98
+ $ redis ->on ('punsubscribe ' , function (string $ pattern ) {
84
99
unset($ this ->psubscribed [$ pattern ]);
85
100
});
86
101
@@ -106,7 +121,7 @@ private function client()
106
121
});
107
122
}
108
123
109
- public function __call ($ name , $ args )
124
+ public function __call (string $ name , array $ args ): PromiseInterface
110
125
{
111
126
if ($ this ->closed ) {
112
127
return reject (new \RuntimeException (
@@ -122,15 +137,15 @@ function ($result) {
122
137
$ this ->idle ();
123
138
return $ result ;
124
139
},
125
- function ($ error ) {
140
+ function (\ Exception $ error ) {
126
141
$ this ->idle ();
127
142
throw $ error ;
128
143
}
129
144
);
130
145
});
131
146
}
132
147
133
- public function end ()
148
+ public function end (): void
134
149
{
135
150
if ($ this ->promise === null ) {
136
151
$ this ->close ();
@@ -140,15 +155,15 @@ public function end()
140
155
return ;
141
156
}
142
157
143
- return $ this ->client ()->then (function (Client $ redis ) {
158
+ $ this ->client ()->then (function (Client $ redis ) {
144
159
$ redis ->on ('close ' , function () {
145
160
$ this ->close ();
146
161
});
147
162
$ redis ->end ();
148
163
});
149
164
}
150
165
151
- public function close ()
166
+ public function close (): void
152
167
{
153
168
if ($ this ->closed ) {
154
169
return ;
@@ -176,10 +191,7 @@ public function close()
176
191
$ this ->removeAllListeners ();
177
192
}
178
193
179
- /**
180
- * @internal
181
- */
182
- public function awake ()
194
+ private function awake (): void
183
195
{
184
196
++$ this ->pending ;
185
197
@@ -189,10 +201,7 @@ public function awake()
189
201
}
190
202
}
191
203
192
- /**
193
- * @internal
194
- */
195
- public function idle ()
204
+ private function idle (): void
196
205
{
197
206
--$ this ->pending ;
198
207
0 commit comments