PHP Class Interop\Async\Loop

See also: Interop\Async\Loop\Driver
Show file Open project: async-interop/event-loop Class Usage Examples

Public Methods

Method Description
cancel ( string $watcherId ) : void Cancel a watcher.
defer ( callable $callback, mixed $data = null ) : string Defer the execution of a callback.
delay ( $time, callable $callback, mixed $data = null ) : string Delay the execution of a callback.
disable ( string $watcherId ) : void Disable a watcher.
enable ( string $watcherId ) : void Enable a watcher.
execute ( callable $callback, Driver $driver = null ) : void Execute a callback within the scope of an event loop driver.
get ( ) : Driver Retrieve the event loop driver that is in scope.
getState ( string $key ) : mixed Gets information stored bound to the loop.
info ( ) : array Retrieve an associative array of information about the event loop driver.
onReadable ( resource $stream, callable $callback, mixed $data = null ) : string Execute a callback when a stream resource becomes readable or is closed for reading.
onSignal ( integer $signo, callable $callback, mixed $data = null ) : string Execute a callback when a signal is received.
onWritable ( resource $stream, callable $callback, mixed $data = null ) : string Execute a callback when a stream resource becomes writable or is closed for writing.
reference ( string $watcherId ) : void Reference a watcher.
repeat ( integer $interval, callable $callback, mixed $data = null ) : string Repeatedly execute a callback.
setErrorHandler ( callable $callback = null ) : void Set a callback to be executed when an error occurs.
setFactory ( Interop\Async\Loop\DriverFactory $factory = null ) Set the factory to be used to create a default drivers.
setState ( string $key, mixed $value ) : void Stores information in the loop bound registry.
stop ( ) : void Stop the event loop.
unreference ( string $watcherId ) : void Unreference a watcher.

Private Methods

Method Description
__construct ( ) Disable construction as this is a static class.
createDriver ( ) : Driver Create a new driver if a factory is present, otherwise throw.

Method Details

cancel() public static method

This will detatch the event loop from all resources that are associated to the watcher. After this operation the watcher is permanently invalid. Calling this function MUST NOT fail, even if passed an invalid watcher.
public static cancel ( string $watcherId ) : void
$watcherId string The watcher identifier.
return void

defer() public static method

The deferred callable MUST be executed in the next tick of the event loop and before any other type of watcher. Order of enabling MUST be preserved when executing the callbacks.
public static defer ( callable $callback, mixed $data = null ) : string
$callback callable
$data mixed Arbitrary data given to the callback function as the `$data` parameter.
return string An unique identifier that can be used to cancel, enable or disable the watcher.

delay() public static method

The delay is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be determined by which timers expire first, but timers with the same expiration time MAY be executed in any order.
public static delay ( $time, callable $callback, mixed $data = null ) : string
$callback callable
$data mixed Arbitrary data given to the callback function as the `$data` parameter.
return string An unique identifier that can be used to cancel, enable or disable the watcher.

disable() public static method

Disabling a watcher MUST NOT invalidate the watcher. Calling this function MUST NOT fail, even if passed an invalid watcher.
public static disable ( string $watcherId ) : void
$watcherId string The watcher identifier.
return void

enable() public static method

Watchers (enabling or new watchers) MUST immediately be marked as enabled, but only be activated (i.e. callbacks can be called) right before the next tick. Callbacks of watchers MUST not be called in the tick they were enabled.
public static enable ( string $watcherId ) : void
$watcherId string The watcher identifier.
return void

execute() public static method

The loop MUST continue to run until it is either stopped explicitly, no referenced watchers exist anymore, or an exception is thrown that cannot be handled. Exceptions that cannot be handled are exceptions thrown from an error handler or exceptions that would be passed to an error handler but none exists to handle them.
See also: Interop\Async\Loop::setFactory()
public static execute ( callable $callback, Driver $driver = null ) : void
$callback callable The callback to execute.
$driver Interop\Async\Loop\Driver The event loop driver. If `null`, a new one is created from the set factory.
return void

get() public static method

Retrieve the event loop driver that is in scope.
public static get ( ) : Driver
return Interop\Async\Loop\Driver

getState() public static method

Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages MUST use the following prefix for keys: vendor.package.
public static getState ( string $key ) : mixed
$key string The namespaced storage key.
return mixed The previously stored value or `null` if it doesn't exist.

info() public static method

The returned array MUST contain the following data describing the driver's currently registered watchers: [ "defer" => ["enabled" => int, "disabled" => int], "delay" => ["enabled" => int, "disabled" => int], "repeat" => ["enabled" => int, "disabled" => int], "on_readable" => ["enabled" => int, "disabled" => int], "on_writable" => ["enabled" => int, "disabled" => int], "on_signal" => ["enabled" => int, "disabled" => int], "watchers" => ["referenced" => int, "unreferenced" => int], ]; Implementations MAY optionally add more information in the array but at minimum the above key => value format MUST always be provided.
public static info ( ) : array
return array

onReadable() public static method

Warning: Closing resources locally, e.g. with fclose, might not invoke the callback. Be sure to cancel the watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed resources are therefore undefined behavior. Multiple watchers on the same stream MAY be executed in any order.
public static onReadable ( resource $stream, callable $callback, mixed $data = null ) : string
$stream resource The stream to monitor.
$callback callable
$data mixed Arbitrary data given to the callback function as the `$data` parameter.
return string An unique identifier that can be used to cancel, enable or disable the watcher.

onSignal() public static method

Warning: Installing the same signal on different instances of this interface is deemed undefined behavior. Implementations MAY try to detect this, if possible, but are not required to. This is due to technical limitations of the signals being registered globally per process. Multiple watchers on the same signal MAY be executed in any order.
public static onSignal ( integer $signo, callable $callback, mixed $data = null ) : string
$signo integer The signal number to monitor.
$callback callable
$data mixed Arbitrary data given to the callback function as the $data parameter.
return string An unique identifier that can be used to cancel, enable or disable the watcher.

onWritable() public static method

Warning: Closing resources locally, e.g. with fclose, might not invoke the callback. Be sure to cancel the watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed resources are therefore undefined behavior. Multiple watchers on the same stream MAY be executed in any order.
public static onWritable ( resource $stream, callable $callback, mixed $data = null ) : string
$stream resource The stream to monitor.
$callback callable
$data mixed Arbitrary data given to the callback function as the `$data` parameter.
return string An unique identifier that can be used to cancel, enable or disable the watcher.

reference() public static method

This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state by default.
public static reference ( string $watcherId ) : void
$watcherId string The watcher identifier.
return void

repeat() public static method

The interval between executions is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be determined by which timers expire first, but timers with the same expiration time MAY be executed in any order. The first execution is scheduled after the first interval period.
public static repeat ( integer $interval, callable $callback, mixed $data = null ) : string
$interval integer The time interval, in milliseconds, to wait between executions.
$callback callable
$data mixed Arbitrary data given to the callback function as the `$data` parameter.
return string An unique identifier that can be used to cancel, enable or disable the watcher.

setErrorHandler() public static method

The callback receives the error as the first and only parameter. The return value of the callback gets ignored. If it can't handle the error, it MUST throw the error. Errors thrown by the callback or during its invocation MUST be thrown into the run loop and stop the driver. Subsequent calls to this method will overwrite the previous handler.
public static setErrorHandler ( callable $callback = null ) : void
$callback callable
return void

setFactory() public static method

Setting a factory is only allowed as long as no loop is currently running. Passing null will reset the default driver and remove the factory. The factory will be invoked if none is passed to Loop::execute. A default driver will be created to support synchronous waits in traditional applications.
public static setFactory ( Interop\Async\Loop\DriverFactory $factory = null )
$factory Interop\Async\Loop\DriverFactory New factory to replace the previous one.

setState() public static method

This can be used to store loop bound information. Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages MUST use the following prefix for keys: vendor.package.
public static setState ( string $key, mixed $value ) : void
$key string The namespaced storage key.
$value mixed The value to be stored.
return void

stop() public static method

When an event loop is stopped, it continues with its current tick and exits the loop afterwards. Multiple calls to stop MUST be ignored and MUST NOT raise an exception.
public static stop ( ) : void
return void

unreference() public static method

The event loop should exit the run method when only unreferenced watchers are still being monitored. Watchers are all referenced by default.
public static unreference ( string $watcherId ) : void
$watcherId string The watcher identifier.
return void