### Install Revolt via Composer Source: https://revolt.run/ Use this command to add the revolt/event-loop package as a dependency in your PHP project. ```bash composer require revolt/event-loop ``` -------------------------------- ### Observe expected output for event loop suspension Source: https://revolt.run/fundamentals Shows the console output generated by the suspension example. ```text ++ Suspending to event loop... ++ Executing callback created by EventLoop::repeat() ++ Executing callback created by EventLoop::repeat() ++ Executing callback created by EventLoop::repeat() ++ Executing callback created by EventLoop::repeat() ++ Executing callback created by EventLoop::delay() ++ Suspension::resume() is async! ++ Script end ``` -------------------------------- ### Registering a Readability Callback Source: https://revolt.run/streams Demonstrates how to monitor a socket for incoming data and handle EOF conditions using EventLoop::onReadable. ```php resume(null); }); $timeoutId = EventLoop::delay(5, function () use ($readableId, $suspension) { EventLoop::cancel($readableId); print "Timeout reached" . PHP_EOL; $suspension->resume(null); }); $suspension->suspend(); EventLoop::cancel($readableId); EventLoop::cancel($timeoutId); ``` -------------------------------- ### Managing Writability Callbacks Source: https://revolt.run/streams Shows the pattern of initializing a writability callback in a disabled state and toggling it only when data is pending to prevent high CPU usage. ```php resume(null); print '++ Suspension::resume() is async!' . PHP_EOL; }); print '++ Suspending to event loop...' . PHP_EOL; $suspension->suspend(); print '++ Script end' . PHP_EOL; ``` -------------------------------- ### EventLoop::repeat() Source: https://revolt.run/timers Schedules a callback to repeatedly execute every n seconds. ```APIDOC ## EventLoop::repeat() ### Description Schedules a callback to repeatedly execute every `n` seconds. Unlike other timers, these must be explicitly canceled to free resources. ### Callback Signature `function (string $callbackId): void` ``` -------------------------------- ### EventLoop::defer() Source: https://revolt.run/timers Schedules a callback to execute in the next iteration of the event loop, ensuring a clean call stack. ```APIDOC ## EventLoop::defer() ### Description Schedules a callback to execute in the next iteration of the event loop. This method guarantees a clean call stack to avoid starvation of other events. ### Callback Signature `function (string $callbackId)` ``` -------------------------------- ### Schedule a deferred callback Source: https://revolt.run/timers Schedules a callback to execute in the next iteration of the event loop to ensure a clean call stack. ```php = 3) { EventLoop::cancel($callbackId); // <-- cancel myself! } }); EventLoop::run(); ``` -------------------------------- ### Schedule a delayed callback Source: https://revolt.run/timers Schedules a callback to execute after a specified number of seconds. ```php