ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Sabre\Event\Loop\Loop Class Reference

A simple eventloop implementation. More...

+ Collaboration diagram for Sabre\Event\Loop\Loop:

Public Member Functions

 setTimeout (callable $cb, $timeout)
 Executes a function after x seconds. More...
 
 setInterval (callable $cb, $timeout)
 Executes a function every x seconds. More...
 
 clearInterval ($intervalId)
 Stops a running internval. More...
 
 nextTick (callable $cb)
 Runs a function immediately at the next iteration of the loop. More...
 
 addReadStream ($stream, callable $cb)
 Adds a read stream. More...
 
 addWriteStream ($stream, callable $cb)
 Adds a write stream. More...
 
 removeReadStream ($stream)
 Stop watching a stream for reads. More...
 
 removeWriteStream ($stream)
 Stop watching a stream for writes. More...
 
 run ()
 Runs the loop. More...
 
 tick ($block=false)
 Executes all pending events. More...
 
 stop ()
 Stops a running eventloop. More...
 

Protected Member Functions

 runNextTicks ()
 Executes all 'nextTick' callbacks. More...
 
 runTimers ()
 Runs all pending timers. More...
 
 runStreams ($timeout)
 Runs all pending stream events. More...
 

Protected Attributes

 $running = false
 
 $timers = []
 
 $nextTick = []
 
 $readStreams = []
 
 $writeStreams = []
 
 $readCallbacks = []
 
 $writeCallbacks = []
 

Detailed Description

A simple eventloop implementation.

This eventloop supports:

  • nextTick
  • setTimeout for delayed functions
  • setInterval for repeating functions
  • stream events using stream_select
Author
Evert Pot (http://evertpot.com/) @license http://sabre.io/license/ Modified BSD License

Definition at line 18 of file Loop.php.

Member Function Documentation

◆ addReadStream()

Sabre\Event\Loop\Loop::addReadStream (   $stream,
callable  $cb 
)

Adds a read stream.

The callback will be called as soon as there is something to read from the stream.

You MUST call removeReadStream after you are done with the stream, to prevent the eventloop from never stopping.

Parameters
resource$stream
callable$cb
Returns
void

Definition at line 132 of file Loop.php.

132 {
133
134 $this->readStreams[(int)$stream] = $stream;
135 $this->readCallbacks[(int)$stream] = $cb;
136
137 }
$stream
PHP stream implementation.

References GuzzleHttp\Psr7\$stream.

◆ addWriteStream()

Sabre\Event\Loop\Loop::addWriteStream (   $stream,
callable  $cb 
)

Adds a write stream.

The callback will be called as soon as the system reports it's ready to receive writes on the stream.

You MUST call removeWriteStream after you are done with the stream, to prevent the eventloop from never stopping.

Parameters
resource$stream
callable$cb
Returns
void

Definition at line 152 of file Loop.php.

152 {
153
154 $this->writeStreams[(int)$stream] = $stream;
155 $this->writeCallbacks[(int)$stream] = $cb;
156
157 }

References GuzzleHttp\Psr7\$stream.

◆ clearInterval()

Sabre\Event\Loop\Loop::clearInterval (   $intervalId)

Stops a running internval.

Parameters
array$intervalId
Returns
void

Definition at line 100 of file Loop.php.

100 {
101
102 $intervalId[1] = false;
103
104 }

◆ nextTick()

Sabre\Event\Loop\Loop::nextTick ( callable  $cb)

Runs a function immediately at the next iteration of the loop.

Parameters
callable$cb
Returns
void

Definition at line 112 of file Loop.php.

112 {
113
114 $this->nextTick[] = $cb;
115
116 }
nextTick(callable $cb)
Runs a function immediately at the next iteration of the loop.
Definition: Loop.php:112

References Sabre\Event\Loop\Loop\nextTick().

Referenced by Sabre\Event\Promise\invokeCallback(), Sabre\Event\Loop\Loop\nextTick(), Sabre\Event\Loop\Loop\runNextTicks(), Sabre\Event\Loop\Loop\runStreams(), Sabre\Event\Promise\PromiseTest\testWaitRejectedException(), Sabre\Event\Promise\PromiseTest\testWaitRejectedNonScalar(), Sabre\Event\Promise\PromiseTest\testWaitRejectedScalar(), Sabre\Event\Promise\PromiseTest\testWaitResolve(), and Sabre\Event\Loop\Loop\tick().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removeReadStream()

Sabre\Event\Loop\Loop::removeReadStream (   $stream)

Stop watching a stream for reads.

Parameters
resource$stream
Returns
void

Definition at line 165 of file Loop.php.

165 {
166
167 unset(
168 $this->readStreams[(int)$stream],
169 $this->readCallbacks[(int)$stream]
170 );
171
172 }

References GuzzleHttp\Psr7\$stream.

◆ removeWriteStream()

Sabre\Event\Loop\Loop::removeWriteStream (   $stream)

Stop watching a stream for writes.

Parameters
resource$stream
Returns
void

Definition at line 180 of file Loop.php.

180 {
181
182 unset(
183 $this->writeStreams[(int)$stream],
184 $this->writeCallbacks[(int)$stream]
185 );
186
187 }

References GuzzleHttp\Psr7\$stream.

◆ run()

Sabre\Event\Loop\Loop::run ( )

Runs the loop.

This function will run continiously, until there's no more events to handle.

Returns
void

Definition at line 198 of file Loop.php.

198 {
199
200 $this->running = true;
201
202 do {
203
204 $hasEvents = $this->tick(true);
205
206 } while ($this->running && $hasEvents);
207 $this->running = false;
208
209 }
tick($block=false)
Executes all pending events.
Definition: Loop.php:226

References Sabre\Event\Loop\Loop\tick().

Referenced by Sabre\Event\Promise\FunctionsTest\testAll(), Sabre\Event\Promise\PromiseTest\testAll(), Sabre\Event\Promise\FunctionsTest\testAllReject(), Sabre\Event\Promise\PromiseTest\testAllReject(), Sabre\Event\Promise\FunctionsTest\testAllRejectThenResolve(), Sabre\Event\Promise\PromiseTest\testAllRejectThenResolve(), Sabre\Event\Promise\PromiseTest\testChain(), Sabre\Event\Promise\PromiseTest\testChainPromise(), Sabre\Event\Promise\PromiseTest\testExecutorFail(), Sabre\Event\Promise\PromiseTest\testExecutorSuccess(), Sabre\Event\Promise\PromiseTest\testFail(), Sabre\Event\Promise\PromiseTest\testFromFailureHandler(), Sabre\Event\Promise\PromiseTest\testPendingFail(), Sabre\Event\Promise\PromiseTest\testPendingResult(), Sabre\Event\Promise\FunctionsTest\testRace(), Sabre\Event\Promise\FunctionsTest\testRaceReject(), Sabre\Event\Promise\FunctionsTest\testReject(), Sabre\Event\Promise\FunctionsTest\testResolve(), and Sabre\Event\Promise\PromiseTest\testSuccess().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ runNextTicks()

Sabre\Event\Loop\Loop::runNextTicks ( )
protected

Executes all 'nextTick' callbacks.

return void

Definition at line 268 of file Loop.php.

268 {
269
271 $this->nextTick = [];
272
273 foreach ($nextTick as $cb) {
274 $cb();
275 }
276
277 }

References Sabre\Event\Loop\Loop\$nextTick, and Sabre\Event\Loop\Loop\nextTick().

Referenced by Sabre\Event\Loop\Loop\tick().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ runStreams()

Sabre\Event\Loop\Loop::runStreams (   $timeout)
protected

Runs all pending stream events.

Parameters
float$timeout

Definition at line 308 of file Loop.php.

308 {
309
310 if ($this->readStreams || $this->writeStreams) {
311
312 $read = $this->readStreams;
313 $write = $this->writeStreams;
314 $except = null;
315 if (stream_select($read, $write, $except, null, $timeout)) {
316
317 // See PHP Bug https://bugs.php.net/bug.php?id=62452
318 // Fixed in PHP7
319 foreach ($read as $readStream) {
320 $readCb = $this->readCallbacks[(int)$readStream];
321 $readCb();
322 }
323 foreach ($write as $writeStream) {
324 $writeCb = $this->writeCallbacks[(int)$writeStream];
325 $writeCb();
326 }
327
328 }
329
330 } elseif ($this->running && ($this->nextTick || $this->timers)) {
331 usleep($timeout !== null ? $timeout * 1000000 : 200000);
332 }
333
334 }

References Sabre\Event\Loop\Loop\$readStreams, Sabre\Event\Loop\Loop\$writeStreams, and Sabre\Event\Loop\Loop\nextTick().

Referenced by Sabre\Event\Loop\Loop\tick().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ runTimers()

Sabre\Event\Loop\Loop::runTimers ( )
protected

Runs all pending timers.

After running the timer callbacks, this function returns the number of seconds until the next timer should be executed.

If there's no more pending timers, this function returns null.

Returns
float

Definition at line 289 of file Loop.php.

289 {
290
291 $now = microtime(true);
292 while (($timer = array_pop($this->timers)) && $timer[0] < $now) {
293 $timer[1]();
294 }
295 // Add the last timer back to the array.
296 if ($timer) {
297 $this->timers[] = $timer;
298 return $timer[0] - microtime(true);
299 }
300
301 }

Referenced by Sabre\Event\Loop\Loop\tick().

+ Here is the caller graph for this function:

◆ setInterval()

Sabre\Event\Loop\Loop::setInterval ( callable  $cb,
  $timeout 
)

Executes a function every x seconds.

The value this function returns can be used to stop the interval with clearInterval.

Parameters
callable$cb
float$timeout
Returns
array

Definition at line 71 of file Loop.php.

71 {
72
73 $keepGoing = true;
74 $f = null;
75
76 $f = function() use ($cb, &$f, $timeout, &$keepGoing) {
77 if ($keepGoing) {
78 $cb();
79 $this->setTimeout($f, $timeout);
80 }
81 };
82 $this->setTimeout($f, $timeout);
83
84 // Really the only thing that matters is returning the $keepGoing
85 // boolean value.
86 //
87 // We need to pack it in an array to allow returning by reference.
88 // Because I'm worried people will be confused by using a boolean as a
89 // sort of identifier, I added an extra string.
90 return ['I\'m an implementation detail', &$keepGoing];
91
92 }
setTimeout(callable $cb, $timeout)
Executes a function after x seconds.
Definition: Loop.php:27

References $f, and Sabre\Event\Loop\Loop\setTimeout().

+ Here is the call graph for this function:

◆ setTimeout()

Sabre\Event\Loop\Loop::setTimeout ( callable  $cb,
  $timeout 
)

Executes a function after x seconds.

Parameters
callable$cb
float$timeouttimeout in seconds
Returns
void

Definition at line 27 of file Loop.php.

27 {
28
29 $triggerTime = microtime(true) + ($timeout);
30
31 if (!$this->timers) {
32 // Special case when the timers array was empty.
33 $this->timers[] = [$triggerTime, $cb];
34 return;
35 }
36
37 // We need to insert these values in the timers array, but the timers
38 // array must be in reverse-order of trigger times.
39 //
40 // So here we search the array for the insertion point.
41 $index = count($this->timers) - 1;
42 while (true) {
43 if ($triggerTime < $this->timers[$index][0]) {
44 array_splice(
45 $this->timers,
46 $index + 1,
47 0,
48 [[$triggerTime, $cb]]
49 );
50 break;
51 } elseif ($index === 0) {
52 array_unshift($this->timers, [$triggerTime, $cb]);
53 break;
54 }
55 $index--;
56
57 }
58
59 }
$index
Definition: metadata.php:60

References $index.

Referenced by Sabre\Event\Loop\Loop\setInterval().

+ Here is the caller graph for this function:

◆ stop()

Sabre\Event\Loop\Loop::stop ( )

Stops a running eventloop.

Returns
void

Definition at line 257 of file Loop.php.

257 {
258
259 $this->running = false;
260
261 }

◆ tick()

Sabre\Event\Loop\Loop::tick (   $block = false)

Executes all pending events.

If $block is turned true, this function will block until any event is triggered.

If there are now timeouts, nextTick callbacks or events in the loop at all, this function will exit immediately.

This function will return true if there are any events left in the loop after the tick.

Parameters
bool$block
Returns
bool

Definition at line 226 of file Loop.php.

226 {
227
228 $this->runNextTicks();
229 $nextTimeout = $this->runTimers();
230
231 // Calculating how long runStreams should at most wait.
232 if (!$block) {
233 // Don't wait
234 $streamWait = 0;
235 } elseif ($this->nextTick) {
236 // There's a pending 'nextTick'. Don't wait.
237 $streamWait = 0;
238 } elseif (is_numeric($nextTimeout)) {
239 // Wait until the next Timeout should trigger.
240 $streamWait = $nextTimeout;
241 } else {
242 // Wait indefinitely
243 $streamWait = null;
244 }
245
246 $this->runStreams($streamWait);
247
248 return ($this->readStreams || $this->writeStreams || $this->nextTick || $this->timers);
249
250 }
runNextTicks()
Executes all 'nextTick' callbacks.
Definition: Loop.php:268
runStreams($timeout)
Runs all pending stream events.
Definition: Loop.php:308
runTimers()
Runs all pending timers.
Definition: Loop.php:289

References Sabre\Event\Loop\Loop\nextTick(), Sabre\Event\Loop\Loop\runNextTicks(), Sabre\Event\Loop\Loop\runStreams(), and Sabre\Event\Loop\Loop\runTimers().

Referenced by Sabre\Event\Loop\Loop\run(), and Sabre\Event\Promise\wait().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $nextTick

Sabre\Event\Loop\Loop::$nextTick = []
protected

Definition at line 355 of file Loop.php.

Referenced by Sabre\Event\Loop\Loop\runNextTicks().

◆ $readCallbacks

Sabre\Event\Loop\Loop::$readCallbacks = []
protected

Definition at line 376 of file Loop.php.

◆ $readStreams

Sabre\Event\Loop\Loop::$readStreams = []
protected

Definition at line 362 of file Loop.php.

Referenced by Sabre\Event\Loop\Loop\runStreams().

◆ $running

Sabre\Event\Loop\Loop::$running = false
protected

Definition at line 341 of file Loop.php.

◆ $timers

Sabre\Event\Loop\Loop::$timers = []
protected

Definition at line 348 of file Loop.php.

◆ $writeCallbacks

Sabre\Event\Loop\Loop::$writeCallbacks = []
protected

Definition at line 383 of file Loop.php.

◆ $writeStreams

Sabre\Event\Loop\Loop::$writeStreams = []
protected

Definition at line 369 of file Loop.php.

Referenced by Sabre\Event\Loop\Loop\runStreams().


The documentation for this class was generated from the following file: