@@ -14,6 +14,7 @@ single [`run()`](#run) call that is controlled by the user.
14
14
* [ Quickstart example] ( #quickstart-example )
15
15
* [ Usage] ( #usage )
16
16
* [ Loop] ( #loop )
17
+ * [ Loop methods] ( #loop-methods )
17
18
* [ get()] ( #get )
18
19
* [ Factory] ( #factory )
19
20
* [ ~~ create()~~ ] ( #create )
@@ -52,35 +53,53 @@ use React\EventLoop\Loop;
52
53
$server = stream_socket_server('tcp://127.0.0.1:8080');
53
54
stream_set_blocking($server, false);
54
55
55
- Loop::get()-> addReadStream($server, function ($server) {
56
+ Loop::addReadStream($server, function ($server) {
56
57
$conn = stream_socket_accept($server);
57
58
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
58
- Loop::get()-> addWriteStream($conn, function ($conn) use (& $data) {
59
+ Loop::addWriteStream($conn, function ($conn) use (& $data) {
59
60
$written = fwrite($conn, $data);
60
61
if ($written === strlen($data)) {
61
62
fclose($conn);
62
- Loop::get()-> removeWriteStream($conn);
63
+ Loop::removeWriteStream($conn);
63
64
} else {
64
65
$data = substr($data, $written);
65
66
}
66
67
});
67
68
});
68
69
69
- Loop::get()-> addPeriodicTimer(5, function () {
70
+ Loop::addPeriodicTimer(5, function () {
70
71
$memory = memory_get_usage() / 1024;
71
72
$formatted = number_format($memory, 3).'K';
72
73
echo "Current memory usage: {$formatted}\n";
73
74
});
74
75
75
- Loop::get()-> run();
76
+ Loop::run();
76
77
```
77
78
78
79
See also the [ examples] ( examples ) .
79
80
80
81
## Usage
81
82
82
- Typical applications use a single event loop which is created at the beginning
83
- and run at the end of the program.
83
+ As of ` v1.2.0 ` , typical applications would use the [ ` Loop ` object] ( #loop )
84
+ to use the currently active event loop instance like this:
85
+
86
+ ``` php
87
+ use React\EventLoop\Loop;
88
+
89
+ $timer = Loop::addPeriodicTimer(0.1, function () {
90
+ echo "Tick" . PHP_EOL;
91
+ });
92
+ Loop::addTimer(1.0, function () use ($timer) {
93
+ Loop::cancelTimer($timer);
94
+ echo 'Done' . PHP_EOL;
95
+ });
96
+
97
+ Loop::run();
98
+ ```
99
+
100
+ As an alternative, you can also explicitly create an event loop instance at the
101
+ beginning, reuse it throughout your program and finally run it at the end of the
102
+ program like this:
84
103
85
104
``` php
86
105
$loop = React\EventLoop\Loop::get(); // or deprecated React\EventLoop\Factory::create();
@@ -96,6 +115,9 @@ $loop->addTimer(1.0, function () use ($loop, $timer) {
96
115
$loop->run();
97
116
```
98
117
118
+ While the former is more concise, the latter is more explicit.
119
+ In both cases, the program would perform the exact same steps.
120
+
99
121
1 . The event loop instance is created at the beginning of the program. This is
100
122
implicitly done the first time you call the [ ` Loop ` class] ( #loop ) or
101
123
explicitly when using the deprecated [ ` Factory::create() method ` ] ( #create )
@@ -107,28 +129,137 @@ $loop->run();
107
129
3 . The event loop is run at the end of the program with a single [ ` run() ` ] ( #run )
108
130
call at the end of the program.
109
131
132
+ As of ` v1.2.0 ` , we highly recommend using the [ ` Loop ` class] ( #loop ) .
133
+ The explicit loop instructions are still valid and may still be useful in some
134
+ applications, especially for a transition period towards the more concise style.
135
+
110
136
### Loop
111
137
112
138
The ` Loop ` class exists as a convenient global accessor for the event loop.
113
139
114
- #### get()
140
+ #### Loop methods
141
+
142
+ The ` Loop ` class provides all methods that exist on the [ ` LoopInterface ` ] ( #loopinterface )
143
+ as static methods:
115
144
116
- The ` get(): LoopInterface ` method is the preferred way to get and use the event loop. With
117
- it there is no need to always pass the loop around anymore.
145
+ * [ run()] ( #run )
146
+ * [ stop()] ( #stop )
147
+ * [ addTimer()] ( #addtimer )
148
+ * [ addPeriodicTimer()] ( #addperiodictimer )
149
+ * [ cancelTimer()] ( #canceltimer )
150
+ * [ futureTick()] ( #futuretick )
151
+ * [ addSignal()] ( #addsignal )
152
+ * [ removeSignal()] ( #removesignal )
153
+ * [ addReadStream()] ( #addreadstream )
154
+ * [ addWriteStream()] ( #addwritestream )
155
+ * [ removeReadStream()] ( #removereadstream )
156
+ * [ removeWriteStream()] ( #removewritestream )
157
+
158
+ If you're working with the event loop in your application code, it's often
159
+ easiest to directly interface with the static methods defined on the ` Loop ` class
160
+ like this:
118
161
119
162
``` php
120
163
use React\EventLoop\Loop;
121
164
122
- Loop::get()->addTimer(0.02 , function () {
123
- echo 'World!' ;
165
+ $timer = Loop::addPeriodicTimer(0.1 , function () {
166
+ echo 'tick!' . PHP_EOL ;
124
167
});
125
- Loop::get()->addTimer(0.01, function () {
126
- echo 'Hello ';
168
+
169
+ Loop::addTimer(1.0, function () use ($timer) {
170
+ Loop::cancelTimer($timer);
171
+ echo 'Done' . PHP_EOL;
127
172
});
128
173
129
- Loop::get()-> run();
174
+ Loop::run();
130
175
```
131
176
177
+ On the other hand, if you're familiar with object-oriented programming (OOP) and
178
+ dependency injection (DI), you may want to inject an event loop instance and
179
+ invoke instance methods on the ` LoopInterface ` like this:
180
+
181
+ ``` php
182
+ use React\EventLoop\Loop;
183
+ use React\EventLoop\LoopInterface;
184
+
185
+ class Greeter
186
+ {
187
+ private $loop;
188
+
189
+ public function __construct(LoopInterface $loop)
190
+ {
191
+ $this->loop = $loop;
192
+ }
193
+
194
+ public function greet(string $name)
195
+ {
196
+ $this->loop->addTimer(1.0, function () use ($name) {
197
+ echo 'Hello ' . $name . '!' . PHP_EOL;
198
+ });
199
+ }
200
+ }
201
+
202
+ $greeter = new Greeter(Loop::get());
203
+ $greeter->greet('Alice');
204
+ $greeter->greet('Bob');
205
+
206
+ Loop::run();
207
+ ```
208
+
209
+ See [ ` LoopInterface ` ] ( #loopinterface ) for more details about available methods.
210
+
211
+ #### get()
212
+
213
+ The ` get(): LoopInterface ` method can be used to
214
+ get the currently active event loop instance.
215
+
216
+ This method will always return the same event loop instance throughout the
217
+ lifetime of your application.
218
+
219
+ ``` php
220
+ use React\EventLoop\Loop;
221
+ use React\EventLoop\LoopInterface;
222
+
223
+ $loop = Loop::get();
224
+
225
+ assert($loop instanceof LoopInterface);
226
+ assert($loop === Loop::get());
227
+ ```
228
+
229
+ This is particularly useful if you're using object-oriented programming (OOP)
230
+ and dependency injection (DI). In this case, you may want to inject an event
231
+ loop instance and invoke instance methods on the ` LoopInterface ` like this:
232
+
233
+ ``` php
234
+ use React\EventLoop\Loop;
235
+ use React\EventLoop\LoopInterface;
236
+
237
+ class Greeter
238
+ {
239
+ private $loop;
240
+
241
+ public function __construct(LoopInterface $loop)
242
+ {
243
+ $this->loop = $loop;
244
+ }
245
+
246
+ public function greet(string $name)
247
+ {
248
+ $this->loop->addTimer(1.0, function () use ($name) {
249
+ echo 'Hello ' . $name . '!' . PHP_EOL;
250
+ });
251
+ }
252
+ }
253
+
254
+ $greeter = new Greeter(Loop::get());
255
+ $greeter->greet('Alice');
256
+ $greeter->greet('Bob');
257
+
258
+ Loop::run();
259
+ ```
260
+
261
+ See [ ` LoopInterface ` ] ( #loopinterface ) for more details about available methods.
262
+
132
263
### Factory
133
264
134
265
The ` Factory ` class exists as a convenient way to pick the best available
0 commit comments