29 $triggerTime = microtime(
true) + ($timeout);
33 $this->timers[] = [$triggerTime, $cb];
41 $index = count($this->timers) - 1;
43 if ($triggerTime < $this->timers[
$index][0]) {
51 } elseif ($index === 0) {
52 array_unshift($this->timers, [$triggerTime, $cb]);
76 $f =
function() use ($cb, &
$f, $timeout, &$keepGoing) {
90 return [
'I\'m an implementation detail', &$keepGoing];
102 $intervalId[1] =
false;
135 $this->readCallbacks[(int)
$stream] = $cb;
155 $this->writeCallbacks[(int)
$stream] = $cb;
168 $this->readStreams[(
int)
$stream],
169 $this->readCallbacks[(
int)$stream]
183 $this->writeStreams[(
int)
$stream],
184 $this->writeCallbacks[(
int)$stream]
200 $this->running =
true;
204 $hasEvents = $this->
tick(
true);
206 }
while ($this->running && $hasEvents);
207 $this->running =
false;
226 function tick($block =
false) {
238 } elseif (is_numeric($nextTimeout)) {
240 $streamWait = $nextTimeout;
248 return ($this->readStreams || $this->writeStreams || $this->
nextTick || $this->timers);
259 $this->running =
false;
291 $now = microtime(
true);
292 while (($timer = array_pop($this->timers)) && $timer[0] < $now) {
297 $this->timers[] = $timer;
298 return $timer[0] - microtime(
true);
310 if ($this->readStreams || $this->writeStreams) {
315 if (stream_select($read, $write, $except, null, $timeout)) {
319 foreach ($read as $readStream) {
320 $readCb = $this->readCallbacks[(int)$readStream];
323 foreach ($write as $writeStream) {
324 $writeCb = $this->writeCallbacks[(int)$writeStream];
330 } elseif ($this->running && ($this->
nextTick || $this->timers)) {
331 usleep($timeout !== null ? $timeout * 1000000 : 200000);
clearInterval($intervalId)
Stops a running internval.
removeWriteStream($stream)
Stop watching a stream for writes.
addReadStream($stream, callable $cb)
Adds a read stream.
tick($block=false)
Executes all pending events.
nextTick(callable $cb)
Runs a function immediately at the next iteration of the loop.
setInterval(callable $cb, $timeout)
Executes a function every x seconds.
removeReadStream($stream)
Stop watching a stream for reads.
runTimers()
Runs all pending timers.
$stream
PHP stream implementation.
runNextTicks()
Executes all 'nextTick' callbacks.
stop()
Stops a running eventloop.
addWriteStream($stream, callable $cb)
Adds a write stream.
setTimeout(callable $cb, $timeout)
Executes a function after x seconds.
runStreams($timeout)
Runs all pending stream events.
A simple eventloop implementation.