aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/sabre/event/lib/Loop/Loop.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/sabre/event/lib/Loop/Loop.php')
-rw-r--r--vendor/sabre/event/lib/Loop/Loop.php147
1 files changed, 56 insertions, 91 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 = [];
-
-
}