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, 91 insertions, 56 deletions
diff --git a/vendor/sabre/event/lib/Loop/Loop.php b/vendor/sabre/event/lib/Loop/Loop.php
index ec09be921..301fe8920 100644
--- a/vendor/sabre/event/lib/Loop/Loop.php
+++ b/vendor/sabre/event/lib/Loop/Loop.php
@@ -1,6 +1,4 @@
-<?php
-
-declare(strict_types=1);
+<?php declare (strict_types=1);
namespace Sabre\Event\Loop;
@@ -17,19 +15,20 @@ 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
*/
- public function setTimeout(callable $cb, float $timeout)
- {
+ 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;
}
@@ -47,12 +46,14 @@ class Loop
[[$triggerTime, $cb]]
);
break;
- } elseif (0 === $index) {
+ } elseif ($index === 0) {
array_unshift($this->timers, [$triggerTime, $cb]);
break;
}
- --$index;
+ $index--;
+
}
+
}
/**
@@ -61,12 +62,12 @@ class Loop
* The value this function returns can be used to stop the interval with
* clearInterval.
*/
- public function setInterval(callable $cb, float $timeout): array
- {
+ 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);
@@ -81,24 +82,32 @@ 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
*/
- public function clearInterval(array $intervalId)
- {
+ function clearInterval(array $intervalId) {
+
$intervalId[1] = false;
+
}
/**
* Runs a function immediately at the next iteration of the loop.
+ *
+ * @return void
*/
- public function nextTick(callable $cb)
- {
+ function nextTick(callable $cb) {
+
$this->nextTick[] = $cb;
+
}
+
/**
* Adds a read stream.
*
@@ -109,11 +118,13 @@ class Loop
* prevent the eventloop from never stopping.
*
* @param resource $stream
+ * @return void
*/
- public function addReadStream($stream, callable $cb)
- {
- $this->readStreams[(int) $stream] = $stream;
- $this->readCallbacks[(int) $stream] = $cb;
+ function addReadStream($stream, callable $cb) {
+
+ $this->readStreams[(int)$stream] = $stream;
+ $this->readCallbacks[(int)$stream] = $cb;
+
}
/**
@@ -126,53 +137,65 @@ class Loop
* prevent the eventloop from never stopping.
*
* @param resource $stream
+ * @return void
*/
- public function addWriteStream($stream, callable $cb)
- {
- $this->writeStreams[(int) $stream] = $stream;
- $this->writeCallbacks[(int) $stream] = $cb;
+ 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
*/
- public function removeReadStream($stream)
- {
+ 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
*/
- public function removeWriteStream($stream)
- {
+ 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 continuously, until there's no more events to
+ * This function will run continiously, until there's no more events to
* handle.
+ *
+ * @return void
*/
- public function run()
- {
+ function run() {
+
$this->running = true;
do {
+
$hasEvents = $this->tick(true);
+
} while ($this->running && $hasEvents);
$this->running = false;
+
}
/**
@@ -187,8 +210,8 @@ class Loop
* This function will return true if there are _any_ events left in the
* loop after the tick.
*/
- public function tick(bool $block = false): bool
- {
+ function tick(bool $block = false) : bool {
+
$this->runNextTicks();
$nextTimeout = $this->runTimers();
@@ -209,15 +232,19 @@ 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.
+ * Stops a running eventloop
+ *
+ * @return void
*/
- public function stop()
- {
+ function stop() {
+
$this->running = false;
+
}
/**
@@ -225,14 +252,15 @@ class Loop
*
* return void
*/
- protected function runNextTicks()
- {
+ protected function runNextTicks() {
+
$nextTick = $this->nextTick;
$this->nextTick = [];
foreach ($nextTick as $cb) {
$cb();
}
+
}
/**
@@ -245,8 +273,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]();
@@ -254,9 +282,9 @@ class Loop
// Add the last timer back to the array.
if ($timer) {
$this->timers[] = $timer;
-
return max(0, $timer[0] - microtime(true));
}
+
}
/**
@@ -267,31 +295,36 @@ 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, (null === $timeout) ? null : 0, $timeout ? (int) ($timeout * 1000000) : 0)) {
+ if (stream_select($read, $write, $except, ($timeout === null) ? 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(null !== $timeout ? intval($timeout * 1000000) : 200000);
+ usleep($timeout !== null ? intval($timeout * 1000000) : 200000);
}
+
}
/**
- * Is the main loop active.
+ * Is the main loop active
*
* @var bool
*/
@@ -328,14 +361,16 @@ class Loop
/**
* List of read callbacks, indexed by stream id.
*
- * @var callable[]
+ * @var callback[]
*/
protected $readCallbacks = [];
/**
* List of write callbacks, indexed by stream id.
*
- * @var callable[]
+ * @var callback[]
*/
protected $writeCallbacks = [];
+
+
}