Skip to content

Commit a503b85

Browse files
committed
Lazy factory instantiation and save method call
1 parent 1c30c34 commit a503b85

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

src/Loop.php

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,22 @@ public static function setFactory(DriverFactory $factory = null)
4040
}
4141

4242
self::$factory = $factory;
43-
44-
if ($factory === null) {
45-
self::$driver = null;
46-
} else {
47-
self::$driver = self::createDriver();
48-
}
43+
self::$driver = null; // reset it here, it will be actually instantiated inside execute() or get()
4944
}
5045

5146
/**
5247
* Execute a callback within the scope of an event loop driver.
5348
*
5449
* @param callable $callback The callback to execute
55-
* @param Driver $driver The event loop driver
50+
* @param Driver $driver The event loop driver. If null a new one is created from the set factory.
5651
*
5752
* @return void
5853
*/
5954
public static function execute(callable $callback, Driver $driver = null)
6055
{
6156
$previousDriver = self::$driver;
6257

63-
$driver = $driver ?: self::createDriver();
64-
65-
self::$driver = $driver;
58+
self::$driver = $driver ?: self::createDriver();
6659
self::$level++;
6760

6861
try {
@@ -103,11 +96,10 @@ private static function createDriver()
10396
*/
10497
public static function get()
10598
{
106-
if (null === self::$driver) {
107-
throw new \RuntimeException('Missing driver; Neither in Loop::execute nor factory set.');
99+
if (self::$driver) {
100+
return self::$driver;
108101
}
109-
110-
return self::$driver;
102+
return self::$driver = self::createDriver();
111103
}
112104

113105
/**
@@ -117,7 +109,8 @@ public static function get()
117109
*/
118110
public static function stop()
119111
{
120-
self::get()->stop();
112+
$driver = self::$driver ?: self::get();
113+
$driver->stop();
121114
}
122115

123116
/**
@@ -130,7 +123,8 @@ public static function stop()
130123
*/
131124
public static function defer(callable $callback, $data = null)
132125
{
133-
return self::get()->defer($callback, $data);
126+
$driver = self::$driver ?: self::get();
127+
return $driver->defer($callback, $data);
134128
}
135129

136130
/**
@@ -146,7 +140,8 @@ public static function defer(callable $callback, $data = null)
146140
*/
147141
public static function delay($time, callable $callback, $data = null)
148142
{
149-
return self::get()->delay($time, $callback, $data);
143+
$driver = self::$driver ?: self::get();
144+
return $driver->delay($time, $callback, $data);
150145
}
151146

152147
/**
@@ -163,7 +158,8 @@ public static function delay($time, callable $callback, $data = null)
163158
*/
164159
public static function repeat($interval, callable $callback, $data = null)
165160
{
166-
return self::get()->repeat($interval, $callback, $data);
161+
$driver = self::$driver ?: self::get();
162+
return $driver->repeat($interval, $callback, $data);
167163
}
168164

169165
/**
@@ -177,7 +173,8 @@ public static function repeat($interval, callable $callback, $data = null)
177173
*/
178174
public static function onReadable($stream, callable $callback, $data = null)
179175
{
180-
return self::get()->onReadable($stream, $callback, $data);
176+
$driver = self::$driver ?: self::get();
177+
return $driver->onReadable($stream, $callback, $data);
181178
}
182179

183180
/**
@@ -191,7 +188,8 @@ public static function onReadable($stream, callable $callback, $data = null)
191188
*/
192189
public static function onWritable($stream, callable $callback, $data = null)
193190
{
194-
return self::get()->onWritable($stream, $callback, $data);
191+
$driver = self::$driver ?: self::get();
192+
return $driver->onWritable($stream, $callback, $data);
195193
}
196194

197195
/**
@@ -205,7 +203,8 @@ public static function onWritable($stream, callable $callback, $data = null)
205203
*/
206204
public static function onSignal($signo, callable $callback, $data = null)
207205
{
208-
return self::get()->onSignal($signo, $callback, $data);
206+
$driver = self::$driver ?: self::get();
207+
return $driver->onSignal($signo, $callback, $data);
209208
}
210209

211210
/**
@@ -217,7 +216,8 @@ public static function onSignal($signo, callable $callback, $data = null)
217216
*/
218217
public static function enable($watcherId)
219218
{
220-
self::get()->enable($watcherId);
219+
$driver = self::$driver ?: self::get();
220+
$driver->enable($watcherId);
221221
}
222222

223223
/**
@@ -229,7 +229,8 @@ public static function enable($watcherId)
229229
*/
230230
public static function disable($watcherId)
231231
{
232-
self::get()->disable($watcherId);
232+
$driver = self::$driver ?: self::get();
233+
$driver->disable($watcherId);
233234
}
234235

235236
/**
@@ -241,7 +242,8 @@ public static function disable($watcherId)
241242
*/
242243
public static function cancel($watcherId)
243244
{
244-
self::get()->cancel($watcherId);
245+
$driver = self::$driver ?: self::get();
246+
$driver->cancel($watcherId);
245247
}
246248

247249
/**
@@ -256,7 +258,8 @@ public static function cancel($watcherId)
256258
*/
257259
public static function reference($watcherId)
258260
{
259-
self::get()->reference($watcherId);
261+
$driver = self::$driver ?: self::get();
262+
$driver->reference($watcherId);
260263
}
261264

262265
/**
@@ -271,7 +274,8 @@ public static function reference($watcherId)
271274
*/
272275
public static function unreference($watcherId)
273276
{
274-
self::get()->unreference($watcherId);
277+
$driver = self::$driver ?: self::get();
278+
$driver->unreference($watcherId);
275279
}
276280

277281
/**
@@ -287,7 +291,8 @@ public static function unreference($watcherId)
287291
*/
288292
public static function storeState($key, $value)
289293
{
290-
self::get()->storeState($key, $value);
294+
$driver = self::$driver ?: self::get();
295+
$driver->storeState($key, $value);
291296
}
292297

293298
/**
@@ -302,7 +307,8 @@ public static function storeState($key, $value)
302307
*/
303308
public static function fetchState($key)
304309
{
305-
return self::get()->fetchState($key);
310+
$driver = self::$driver ?: self::get();
311+
return $driver->fetchState($key);
306312
}
307313

308314
/**
@@ -316,7 +322,8 @@ public static function fetchState($key)
316322
*/
317323
public static function setErrorHandler(callable $callback = null)
318324
{
319-
self::get()->setErrorHandler($callback);
325+
$driver = self::$driver ?: self::get();
326+
$driver->setErrorHandler($callback);
320327
}
321328

322329
/**
@@ -342,7 +349,8 @@ public static function setErrorHandler(callable $callback = null)
342349
*/
343350
public static function info()
344351
{
345-
return self::get()->info();
352+
$driver = self::$driver ?: self::get();
353+
return $driver->info();
346354
}
347355

348356
/**

0 commit comments

Comments
 (0)