diff options
Diffstat (limited to 'vendor/sabre/event/lib/Loop')
-rw-r--r-- | vendor/sabre/event/lib/Loop/Loop.php | 147 | ||||
-rw-r--r-- | vendor/sabre/event/lib/Loop/functions.php | 85 |
2 files changed, 86 insertions, 146 deletions
diff --git a/vendor/sabre/event/lib/Loop/Loop.php b/vendor/sabre/event/lib/Loop/Loop.php index 301fe8920..ec09be921 100644 --- a/vendor/sabre/event/lib/Loop/Loop.php +++ b/vendor/sabre/event/lib/Loop/Loop.php @@ -1,4 +1,6 @@ -<?php declare (strict_types=1); +<?php + +declare(strict_types=1); namespace Sabre\Event\Loop; @@ -15,20 +17,19 @@ namespace Sabre\Event\Loop; * @author Evert Pot (http://evertpot.com/) * @license http://sabre.io/license/ Modified BSD License */ -class Loop { - +class Loop +{ /** * Executes a function after x seconds. - * - * @return void */ - function setTimeout(callable $cb, float $timeout) { - + public function setTimeout(callable $cb, float $timeout) + { $triggerTime = microtime(true) + ($timeout); if (!$this->timers) { // Special case when the timers array was empty. $this->timers[] = [$triggerTime, $cb]; + return; } @@ -46,14 +47,12 @@ class Loop { [[$triggerTime, $cb]] ); break; - } elseif ($index === 0) { + } elseif (0 === $index) { array_unshift($this->timers, [$triggerTime, $cb]); break; } - $index--; - + --$index; } - } /** @@ -62,12 +61,12 @@ class Loop { * The value this function returns can be used to stop the interval with * clearInterval. */ - function setInterval(callable $cb, float $timeout) : array { - + public function setInterval(callable $cb, float $timeout): array + { $keepGoing = true; $f = null; - $f = function() use ($cb, &$f, $timeout, &$keepGoing) { + $f = function () use ($cb, &$f, $timeout, &$keepGoing) { if ($keepGoing) { $cb(); $this->setTimeout($f, $timeout); @@ -82,32 +81,24 @@ class Loop { // Because I'm worried people will be confused by using a boolean as a // sort of identifier, I added an extra string. return ['I\'m an implementation detail', &$keepGoing]; - } /** * Stops a running interval. - * - * @return void */ - function clearInterval(array $intervalId) { - + public function clearInterval(array $intervalId) + { $intervalId[1] = false; - } /** * Runs a function immediately at the next iteration of the loop. - * - * @return void */ - function nextTick(callable $cb) { - + public function nextTick(callable $cb) + { $this->nextTick[] = $cb; - } - /** * Adds a read stream. * @@ -118,13 +109,11 @@ class Loop { * prevent the eventloop from never stopping. * * @param resource $stream - * @return void */ - function addReadStream($stream, callable $cb) { - - $this->readStreams[(int)$stream] = $stream; - $this->readCallbacks[(int)$stream] = $cb; - + public function addReadStream($stream, callable $cb) + { + $this->readStreams[(int) $stream] = $stream; + $this->readCallbacks[(int) $stream] = $cb; } /** @@ -137,65 +126,53 @@ class Loop { * prevent the eventloop from never stopping. * * @param resource $stream - * @return void */ - function addWriteStream($stream, callable $cb) { - - $this->writeStreams[(int)$stream] = $stream; - $this->writeCallbacks[(int)$stream] = $cb; - + public function addWriteStream($stream, callable $cb) + { + $this->writeStreams[(int) $stream] = $stream; + $this->writeCallbacks[(int) $stream] = $cb; } /** * Stop watching a stream for reads. * * @param resource $stream - * @return void */ - function removeReadStream($stream) { - + public function removeReadStream($stream) + { unset( - $this->readStreams[(int)$stream], - $this->readCallbacks[(int)$stream] + $this->readStreams[(int) $stream], + $this->readCallbacks[(int) $stream] ); - } /** * Stop watching a stream for writes. * * @param resource $stream - * @return void */ - function removeWriteStream($stream) { - + public function removeWriteStream($stream) + { unset( - $this->writeStreams[(int)$stream], - $this->writeCallbacks[(int)$stream] + $this->writeStreams[(int) $stream], + $this->writeCallbacks[(int) $stream] ); - } - /** * Runs the loop. * - * This function will run continiously, until there's no more events to + * This function will run continuously, until there's no more events to * handle. - * - * @return void */ - function run() { - + public function run() + { $this->running = true; do { - $hasEvents = $this->tick(true); - } while ($this->running && $hasEvents); $this->running = false; - } /** @@ -210,8 +187,8 @@ class Loop { * This function will return true if there are _any_ events left in the * loop after the tick. */ - function tick(bool $block = false) : bool { - + public function tick(bool $block = false): bool + { $this->runNextTicks(); $nextTimeout = $this->runTimers(); @@ -232,19 +209,15 @@ class Loop { $this->runStreams($streamWait); - return ($this->readStreams || $this->writeStreams || $this->nextTick || $this->timers); - + return $this->readStreams || $this->writeStreams || $this->nextTick || $this->timers; } /** - * Stops a running eventloop - * - * @return void + * Stops a running eventloop. */ - function stop() { - + public function stop() + { $this->running = false; - } /** @@ -252,15 +225,14 @@ class Loop { * * return void */ - protected function runNextTicks() { - + protected function runNextTicks() + { $nextTick = $this->nextTick; $this->nextTick = []; foreach ($nextTick as $cb) { $cb(); } - } /** @@ -273,8 +245,8 @@ class Loop { * * @return float|null */ - protected function runTimers() { - + protected function runTimers() + { $now = microtime(true); while (($timer = array_pop($this->timers)) && $timer[0] < $now) { $timer[1](); @@ -282,9 +254,9 @@ class Loop { // Add the last timer back to the array. if ($timer) { $this->timers[] = $timer; + return max(0, $timer[0] - microtime(true)); } - } /** @@ -295,36 +267,31 @@ class Loop { * * @param float|null timeout */ - protected function runStreams($timeout) { - + protected function runStreams($timeout) + { if ($this->readStreams || $this->writeStreams) { - $read = $this->readStreams; $write = $this->writeStreams; $except = null; - if (stream_select($read, $write, $except, ($timeout === null) ? null : 0, $timeout ? (int)($timeout * 1000000) : 0)) { - + if (stream_select($read, $write, $except, (null === $timeout) ? null : 0, $timeout ? (int) ($timeout * 1000000) : 0)) { // See PHP Bug https://bugs.php.net/bug.php?id=62452 // Fixed in PHP7 foreach ($read as $readStream) { - $readCb = $this->readCallbacks[(int)$readStream]; + $readCb = $this->readCallbacks[(int) $readStream]; $readCb(); } foreach ($write as $writeStream) { - $writeCb = $this->writeCallbacks[(int)$writeStream]; + $writeCb = $this->writeCallbacks[(int) $writeStream]; $writeCb(); } - } - } elseif ($this->running && ($this->nextTick || $this->timers)) { - usleep($timeout !== null ? intval($timeout * 1000000) : 200000); + usleep(null !== $timeout ? intval($timeout * 1000000) : 200000); } - } /** - * Is the main loop active + * Is the main loop active. * * @var bool */ @@ -361,16 +328,14 @@ class Loop { /** * List of read callbacks, indexed by stream id. * - * @var callback[] + * @var callable[] */ protected $readCallbacks = []; /** * List of write callbacks, indexed by stream id. * - * @var callback[] + * @var callable[] */ protected $writeCallbacks = []; - - } diff --git a/vendor/sabre/event/lib/Loop/functions.php b/vendor/sabre/event/lib/Loop/functions.php index b5884b2b6..bf4d933f2 100644 --- a/vendor/sabre/event/lib/Loop/functions.php +++ b/vendor/sabre/event/lib/Loop/functions.php @@ -1,16 +1,15 @@ -<?php declare (strict_types=1); +<?php + +declare(strict_types=1); namespace Sabre\Event\Loop; /** * Executes a function after x seconds. - * - * @return void */ -function setTimeout(callable $cb, float $timeout) { - +function setTimeout(callable $cb, float $timeout) +{ instance()->setTimeout($cb, $timeout); - } /** @@ -19,35 +18,27 @@ function setTimeout(callable $cb, float $timeout) { * The value this function returns can be used to stop the interval with * clearInterval. */ -function setInterval(callable $cb, float $timeout) : array { - +function setInterval(callable $cb, float $timeout): array +{ return instance()->setInterval($cb, $timeout); - } /** * Stops a running interval. - * - * @return void */ -function clearInterval(array $intervalId) { - +function clearInterval(array $intervalId) +{ instance()->clearInterval($intervalId); - } /** * Runs a function immediately at the next iteration of the loop. - * - * @return void */ -function nextTick(callable $cb) { - +function nextTick(callable $cb) +{ instance()->nextTick($cb); - } - /** * Adds a read stream. * @@ -58,12 +49,10 @@ function nextTick(callable $cb) { * prevent the eventloop from never stopping. * * @param resource $stream - * @return void */ -function addReadStream($stream, callable $cb) { - +function addReadStream($stream, callable $cb) +{ instance()->addReadStream($stream, $cb); - } /** @@ -76,51 +65,41 @@ function addReadStream($stream, callable $cb) { * prevent the eventloop from never stopping. * * @param resource $stream - * @return void */ -function addWriteStream($stream, callable $cb) { - +function addWriteStream($stream, callable $cb) +{ instance()->addWriteStream($stream, $cb); - } /** * Stop watching a stream for reads. * * @param resource $stream - * @return void */ -function removeReadStream($stream) { - +function removeReadStream($stream) +{ instance()->removeReadStream($stream); - } /** * Stop watching a stream for writes. * * @param resource $stream - * @return void */ -function removeWriteStream($stream) { - +function removeWriteStream($stream) +{ instance()->removeWriteStream($stream); - } - /** * Runs the loop. * - * This function will run continiously, until there's no more events to + * This function will run continuously, until there's no more events to * handle. - * - * @return void */ -function run() { - +function run() +{ instance()->run(); - } /** @@ -135,34 +114,30 @@ function run() { * This function will return true if there are _any_ events left in the * loop after the tick. */ -function tick(bool $block = false) : bool { - +function tick(bool $block = false): bool +{ return instance()->tick($block); - } /** - * Stops a running eventloop - * - * @return void + * Stops a running eventloop. */ -function stop() { - +function stop() +{ instance()->stop(); - } /** * Retrieves or sets the global Loop object. */ -function instance(Loop $newLoop = null) : Loop { - +function instance(Loop $newLoop = null): Loop +{ static $loop; if ($newLoop) { $loop = $newLoop; } elseif (!$loop) { $loop = new Loop(); } - return $loop; + return $loop; } |