Fledge\Loop
A final static class. Every callback method returns a string id that you pass to enable(), disable(), cancel(), reference() and unreference(). Callbacks run in their own fibers and must return nothing; one that returns a value raises InvalidCallbackError.
Scheduling
| Method | Notes |
|---|---|
Loop::queue(\Closure $closure, mixed ...$args): void |
Runs as soon as the loop gains control, in queue order. Creates no callback, so it has no id and cannot be disabled or unreferenced |
Loop::defer(\Closure $closure): string |
Runs on the next tick |
Loop::delay(float $delay, \Closure $closure): string |
Runs once after $delay seconds |
Loop::repeat(float $interval, \Closure $closure): string |
Runs every $interval seconds |
Loop::onReadable(mixed $stream, \Closure $closure): string |
Closure receives (string $id, $stream) |
Loop::onWritable(mixed $stream, \Closure $closure): string |
Closure receives (string $id, $stream) |
Loop::onSignal(int $signal, \Closure $closure): string |
Needs ext-pcntl |
delay() and repeat() throw an \Error for a negative or NAN duration. INF is accepted and never fires.
Managing callbacks
| Method | Notes |
|---|---|
Loop::enable(string $callbackId): string |
|
Loop::disable(string $callbackId): string |
|
Loop::cancel(string $callbackId): void |
The id is permanently invalid afterwards |
Loop::reference(string $callbackId): string |
A referenced callback keeps run() alive |
Loop::unreference(string $callbackId): string |
|
Loop::getIdentifiers(): array |
|
Loop::getType(string $callbackId): CallbackType |
Cases: Defer, Delay, Repeat, Readable, Writable, Signal |
Loop::isEnabled(string $callbackId): bool |
|
Loop::isReferenced(string $callbackId): bool |
|
Loop::setErrorHandler(?\Closure $errorHandler): void |
Without a handler, an exception from a callback surfaces as UncaughtThrowable out of run() |
Loop::getErrorHandler(): ?\Closure |
An unknown id passed to enable(), reference() and the getters throws InvalidCallbackError.
Running
| Method | Notes |
|---|---|
Loop::run(): void |
Only from {main}. Returns when no enabled, referenced callback is left |
Loop::getSuspension(): Suspension |
Same fiber, same object |
Loop::inLoopFiber(): bool |
Whether the caller runs in the loop's own fiber |
Drivers
| Method | Notes |
|---|---|
Loop::install(?string $name = null): Driver |
auto, poll or select; null reads FLEDGE_EVENT_LOOP. Refuses while running or with callbacks registered |
Loop::setDriver(Driver $driver): void |
Sets a driver instance |
Loop::getDriver(): Driver |
Creates the default driver as a side effect |
Loop::hasDriver(): bool |
True once a driver was created, installed or set |
Loop::driverName(): ?string |
poll, select, the class name of a third-party driver, or null when there is none yet |
Forking
| Method | Notes |
|---|---|
Loop::fork(): int |
pcntl_fork() plus afterFork() in the child. Throws ForkFailedException when the fork fails |
Loop::afterFork(?string $name = null): Driver |
Discards the inherited driver and installs a fresh empty one. Throws ForkUnwindException when inherited fibers threw while unwinding |
Both throw LoopStateException when called from a loop callback or a fiber.
Stopping the loop is a driver method: Loop::getDriver()->stop().
Fledge\Loop\Suspension
An interface, returned by Loop::getSuspension().
| Method | Notes |
|---|---|
suspend(): mixed |
Parks the calling fiber and returns the value passed to resume() |
resume(mixed $value = null): void |
|
throw(\Throwable $throwable): void |
Resumes the fiber by throwing into it |
Fledge\Loop\FiberLocal
Fiber local storage: each instance stores data separately for each fiber.
| Member | Notes |
|---|---|
new FiberLocal(\Closure $initializer) |
The closure builds the value on first get() in a fiber |
get(): mixed |
|
set(mixed $value): void |
|
unset(): void |
|
FiberLocal::clear(): void |
Clears all values of the current fiber |
Exceptions
All in Fledge\Loop.
| Class | Extends | Thrown by |
|---|---|---|
LoopStateException |
LogicException |
install(), afterFork() and fork() refusing in the current state; a PollDriver used in a forked child or after it was replaced |
UnknownDriverException |
InvalidArgumentException |
a driver name other than auto, poll or select (argument or FLEDGE_EVENT_LOOP) |
UnsupportedFeatureException |
Exception |
poll requested where neither kqueue nor epoll exists; onSignal() and fork() without ext-pcntl; a signal handler that cannot be installed |
ForkFailedException |
RuntimeException |
fork() when pcntl_fork() fails; no child was created |
ForkUnwindException |
RuntimeException |
afterFork() when fibers inherited from the parent threw while unwinding; getUnwindErrors() lists every error caught |
InvalidCallbackError |
Error |
an unknown callback id; a callback that returns a value |
UncaughtThrowable |
Error |
run() (or the suspend() running the loop), wrapping an exception a callback threw with no error handler set |
Catch LoopStateException rather than LogicException to keep a refusal apart from an unknown driver name, since InvalidArgumentException extends LogicException.
Other throws: run() inside a fiber or while the loop runs, misuse of a Suspension, and a stream closed while watched throw an \Error. A stream_select() failure throws \Exception.
Environment variables
| Variable | Effect |
|---|---|
FLEDGE_EVENT_LOOP |
auto, poll or select |
FLEDGE_DEBUG |
Truthy value enables the closed-stream check, read once when the driver is created |