Skip to content

Commit

Permalink
Automatically run global loop in a shutdown function
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Apr 25, 2017
1 parent c52f792 commit 4d5e850
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/GlobalLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class GlobalLoop
private static $factory;

private static $didRun = false;
private static $disableRunOnShutdown = false;

public static function setFactory(callable $factory = null)
{
Expand All @@ -24,6 +25,11 @@ public static function setFactory(callable $factory = null)
self::$factory = $factory;
}

public function disableRunOnShutdown()
{
self::$disableRunOnShutdown = true;
}

/**
* @return LoopInterface
*/
Expand All @@ -33,6 +39,14 @@ public static function get()
return self::$loop;
}

register_shutdown_function(function () {
if (self::$disableRunOnShutdown || self::$didRun || !self::$loop) {
return;
}

self::$loop->run();
});

self::$loop = self::create();

self::$loop->futureTick(function () {
Expand Down

0 comments on commit 4d5e850

Please sign in to comment.