Skip to content

Commit e7c6f27

Browse files
committed
Automatically run global loop in a shutdown function
1 parent 649b5ae commit e7c6f27

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/GlobalLoop.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class GlobalLoop
1212
private static $factory;
1313

1414
private static $didRun = false;
15+
private static $disableRunOnShutdown = false;
1516

1617
public static function setFactory(callable $factory = null)
1718
{
@@ -24,6 +25,11 @@ public static function setFactory(callable $factory = null)
2425
self::$factory = $factory;
2526
}
2627

28+
public function disableRunOnShutdown()
29+
{
30+
self::$disableRunOnShutdown = true;
31+
}
32+
2733
/**
2834
* @return LoopInterface
2935
*/
@@ -33,6 +39,14 @@ public static function get()
3339
return self::$loop;
3440
}
3541

42+
register_shutdown_function(function () {
43+
if (self::$disableRunOnShutdown || self::$didRun || !self::$loop) {
44+
return;
45+
}
46+
47+
self::$loop->run();
48+
});
49+
3650
self::$loop = self::create();
3751

3852
self::$loop->futureTick(function () {

0 commit comments

Comments
 (0)