Loop
in package
A simple eventloop implementation.
This eventloop supports:
- nextTick
- setTimeout for delayed functions
- setInterval for repeating functions
- stream events using stream_select
Tags
Table of Contents
- $nextTick : array<string|int, callable>
- A list of 'nextTick' callbacks.
- $readCallbacks : array<string|int, callable>
- List of read callbacks, indexed by stream id.
- $readStreams : array<string|int, resource>
- List of readable streams for stream_select, indexed by stream id.
- $running : bool
- Is the main loop active.
- $timers : array<string|int, mixed>
- A list of timers, added by setTimeout.
- $writeCallbacks : array<string|int, callable>
- List of write callbacks, indexed by stream id.
- $writeStreams : array<string|int, resource>
- List of writable streams for stream_select, indexed by stream id.
- addReadStream() : mixed
- Adds a read stream.
- addWriteStream() : mixed
- Adds a write stream.
- clearInterval() : mixed
- Stops a running interval.
- nextTick() : mixed
- Runs a function immediately at the next iteration of the loop.
- removeReadStream() : mixed
- Stop watching a stream for reads.
- removeWriteStream() : mixed
- Stop watching a stream for writes.
- run() : mixed
- Runs the loop.
- setInterval() : array<string|int, mixed>
- Executes a function every x seconds.
- setTimeout() : mixed
- Executes a function after x seconds.
- stop() : mixed
- Stops a running eventloop.
- tick() : bool
- Executes all pending events.
- runNextTicks() : mixed
- Executes all 'nextTick' callbacks.
- runStreams() : mixed
- Runs all pending stream events.
- runTimers() : float|null
- Runs all pending timers.
Properties
$nextTick
A list of 'nextTick' callbacks.
protected
array<string|int, callable>
$nextTick
= []
$readCallbacks
List of read callbacks, indexed by stream id.
protected
array<string|int, callable>
$readCallbacks
= []
$readStreams
List of readable streams for stream_select, indexed by stream id.
protected
array<string|int, resource>
$readStreams
= []
$running
Is the main loop active.
protected
bool
$running
= false
$timers
A list of timers, added by setTimeout.
protected
array<string|int, mixed>
$timers
= []
$writeCallbacks
List of write callbacks, indexed by stream id.
protected
array<string|int, callable>
$writeCallbacks
= []
$writeStreams
List of writable streams for stream_select, indexed by stream id.
protected
array<string|int, resource>
$writeStreams
= []
Methods
addReadStream()
Adds a read stream.
public
addReadStream(resource $stream, callable $cb) : mixed
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
- $stream : resource
- $cb : callable
Return values
mixed —addWriteStream()
Adds a write stream.
public
addWriteStream(resource $stream, callable $cb) : mixed
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
- $stream : resource
- $cb : callable
Return values
mixed —clearInterval()
Stops a running interval.
public
clearInterval(array<string|int, mixed> $intervalId) : mixed
Parameters
- $intervalId : array<string|int, mixed>
Return values
mixed —nextTick()
Runs a function immediately at the next iteration of the loop.
public
nextTick(callable $cb) : mixed
Parameters
- $cb : callable
Return values
mixed —removeReadStream()
Stop watching a stream for reads.
public
removeReadStream(resource $stream) : mixed
Parameters
- $stream : resource
Return values
mixed —removeWriteStream()
Stop watching a stream for writes.
public
removeWriteStream(resource $stream) : mixed
Parameters
- $stream : resource
Return values
mixed —run()
Runs the loop.
public
run() : mixed
This function will run continuously, until there's no more events to handle.
Return values
mixed —setInterval()
Executes a function every x seconds.
public
setInterval(callable $cb, float $timeout) : array<string|int, mixed>
The value this function returns can be used to stop the interval with clearInterval.
Parameters
- $cb : callable
- $timeout : float
Return values
array<string|int, mixed> —setTimeout()
Executes a function after x seconds.
public
setTimeout(callable $cb, float $timeout) : mixed
Parameters
- $cb : callable
- $timeout : float
Return values
mixed —stop()
Stops a running eventloop.
public
stop() : mixed
Return values
mixed —tick()
Executes all pending events.
public
tick([bool $block = false ]) : bool
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
- $block : bool = false
Return values
bool —runNextTicks()
Executes all 'nextTick' callbacks.
protected
runNextTicks() : mixed
return void
Return values
mixed —runStreams()
Runs all pending stream events.
protected
runStreams(mixed $timeout) : mixed
If $timeout is 0, it will return immediately. If $timeout is null, it will wait indefinitely.
Parameters
- $timeout : mixed
Return values
mixed —runTimers()
Runs all pending timers.
protected
runTimers() : float|null
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.