Skip to content

Commit 85ca3b4

Browse files
authored
Merge pull request #66 from ratchetphp/run-once
Prevent loop from being run on shutdown if it has already been run
2 parents 6cb895d + 38fbdd6 commit 85ca3b4

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/functions.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Ratchet\Client;
33
use React\EventLoop\LoopInterface;
44
use React\EventLoop\Factory as ReactFactory;
5+
use React\EventLoop\Timer\Timer;
56

67
/**
78
* @param string $url
@@ -16,8 +17,16 @@ function connect($url, array $subProtocols = [], $headers = [], LoopInterface $l
1617
$connector = new Connector($loop);
1718
$connection = $connector($url, $subProtocols, $headers);
1819

19-
register_shutdown_function(function() use ($loop) {
20-
$loop->run();
20+
$runHasBeenCalled = false;
21+
22+
$loop->addTimer(Timer::MIN_INTERVAL, function () use (&$runHasBeenCalled) {
23+
$runHasBeenCalled = true;
24+
});
25+
26+
register_shutdown_function(function() use ($loop, &$runHasBeenCalled) {
27+
if (!$runHasBeenCalled) {
28+
$loop->run();
29+
}
2130
});
2231

2332
return $connection;

0 commit comments

Comments
 (0)