Amp\Loop class Loop

Accessor to allow global access to the event loop.

Summary

Properties

Methods

Properties

Methods

set — public function set(Amp\Driver $driver)

Sets the driver to be used for Loop::run().

Type Parameter Description Default Value
Amp\Driver $driver No documentation. none
activate — protected function activate(array $watchers)
No documentation.
Type Parameter Description Default Value
array $watchers No documentation. none
dispatch — protected function dispatch(bool $blocking)
No documentation.
Type Parameter Description Default Value
bool $blocking No documentation. none
deactivate — protected function deactivate(Amp\Loop\Watcher $watcher)
No documentation.
Type Parameter Description Default Value
Amp\Loop\Watcher $watcher No documentation. none
getHandle — public function getHandle()
No documentation.
run — public function run(callable|null $callback = null): void

Run the event loop and optionally execute a callback within the scope of it.

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.

Type Parameter Description Default Value
callable|null $callback The callback to execute. null
stop — public function stop(): void

Stop the event loop.

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.

defer — public function defer(callable $callback, $data = null): string
No documentation.
Type Parameter Description Default Value
callable $callback No documentation. none
mixed $data No documentation. null
delay — public function delay(int $delay, callable $callback, $data = null): string
No documentation.
Type Parameter Description Default Value
int $delay No documentation. none
callable $callback No documentation. none
mixed $data No documentation. null
repeat — public function repeat(int $interval, callable $callback, $data = null): string
No documentation.
Type Parameter Description Default Value
int $interval No documentation. none
callable $callback No documentation. none
mixed $data No documentation. null
onReadable — public function onReadable($stream, callable $callback, $data = null): string
No documentation.
Type Parameter Description Default Value
mixed $stream No documentation. none
callable $callback No documentation. none
mixed $data No documentation. null
onWritable — public function onWritable($stream, callable $callback, $data = null): string
No documentation.
Type Parameter Description Default Value
mixed $stream No documentation. none
callable $callback No documentation. none
mixed $data No documentation. null
onSignal — public function onSignal(int $signo, callable $callback, $data = null): string
No documentation.
Type Parameter Description Default Value
int $signo No documentation. none
callable $callback No documentation. none
mixed $data No documentation. null
enable — public function enable(string $watcherId): void

Enable a watcher to be active starting in the next tick.

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.

Type Parameter Description Default Value
string $watcherId The watcher identifier. none
disable — public function disable(string $watcherId): void

Disable a watcher immediately.

A watcher MUST be disabled immediately, e.g. if a defer watcher disables a later defer watcher, the second defer watcher isn't executed in this tick.

Disabling a watcher MUST NOT invalidate the watcher. Calling this function MUST NOT fail, even if passed an invalid watcher.

Type Parameter Description Default Value
string $watcherId The watcher identifier. none
cancel — public function cancel(string $watcherId): void

Cancel a watcher.

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.

Type Parameter Description Default Value
string $watcherId The watcher identifier. none
reference — public function reference(string $watcherId): void

Reference a watcher.

This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state by default.

Type Parameter Description Default Value
string $watcherId The watcher identifier. none
unreference — public function unreference(string $watcherId): void

Unreference a watcher.

The event loop should exit the run method when only unreferenced watchers are still being monitored. Watchers are all referenced by default.

Type Parameter Description Default Value
string $watcherId The watcher identifier. none
setState — public function setState(string $key, mixed $value): void

Stores information in the loop bound registry.

Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages MUST use their namespace as prefix for keys. They may do so by using SomeClass::class as key.

If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated interface for that purpose instead of sharing the storage key.

Type Parameter Description Default Value
string $key The namespaced storage key. none
mixed $value The value to be stored. none
getState — public function getState(string $key): mixed

Gets information stored bound to the loop.

Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages MUST use their namespace as prefix for keys. They may do so by using SomeClass::class as key.

If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated interface for that purpose instead of sharing the storage key.

Type Parameter Description Default Value
string $key The namespaced storage key. none
setErrorHandler — public function setErrorHandler(callable $callback = null)
No documentation.
Type Parameter Description Default Value
callable $callback No documentation. null
getInfo — public function getInfo(): array

Retrieve an associative array of information about the event loop driver.

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],
    "enabled_watchers" => ["referenced" => int, "unreferenced" => int],
    "running"          => bool
];

Implementations MAY optionally add more information in the array but at minimum the above key => value format MUST always be provided.

get — public function get(): Amp\Driver

Retrieve the event loop driver that is in scope.