{"id":16511159,"url":"https://github.com/danog/loop","last_synced_at":"2025-07-11T20:32:14.103Z","repository":{"id":56962177,"uuid":"281444155","full_name":"danog/loop","owner":"danog","description":"Actor abstraction for AMPHP","archived":false,"fork":false,"pushed_at":"2025-02-20T09:09:12.000Z","size":134,"stargazers_count":8,"open_issues_count":0,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-15T07:49:01.629Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danog.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"danog"}},"created_at":"2020-07-21T16:06:09.000Z","updated_at":"2025-04-10T01:22:47.000Z","dependencies_parsed_at":"2025-07-11T20:31:53.429Z","dependency_job_id":null,"html_url":"https://github.com/danog/loop","commit_stats":{"total_commits":100,"total_committers":2,"mean_commits":50.0,"dds":"0.020000000000000018","last_synced_commit":"1537d15a96d83f02e970287f3cdefb36a9d612de"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/danog/loop","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danog%2Floop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danog%2Floop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danog%2Floop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danog%2Floop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danog","download_url":"https://codeload.github.com/danog/loop/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danog%2Floop/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264892506,"owners_count":23679309,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T15:59:11.111Z","updated_at":"2025-07-11T20:32:14.042Z","avatar_url":"https://github.com/danog.png","language":"PHP","funding_links":["https://github.com/sponsors/danog"],"categories":["Event loop"],"sub_categories":[],"readme":"# Loop\n\n[![codecov](https://codecov.io/gh/danog/loop/branch/master/graph/badge.svg)](https://codecov.io/gh/danog/loop)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fdanog%2Floop%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/danog/loop/master)\n[![Psalm coverage](https://shepherd.dev/github/danog/loop/coverage.svg)](https://shepherd.dev/github/danog/loop)\n[![Psalm level 1](https://shepherd.dev/github/danog/loop/level.svg)](https://shepherd.dev/github/danog/loop)\n![License](https://img.shields.io/badge/license-MIT-blue.svg)\n\n`danog/loop` provides a set of powerful async loop APIs based on [amphp](https://amphp.org) for executing operations periodically or on demand, in background loops a-la threads.\n\n## Installation\n\n```bash\ncomposer require danog/loop\n```\n\n## API\n\n- Basic\n  - [GenericLoop](#genericloop)\n  - [PeriodicLoop](#periodicloop)\n- Advanced\n  - [Loop](#loop)\n\n### Loop\n\n[Class](https://github.com/danog/loop/blob/master/lib/Loop.php) - [Example](https://github.com/danog/loop/blob/master/examples/Loop.php)\n\nA loop capable of running in background (asynchronously) the code contained in the `loop` function.  \nImplements pause and resume functionality, and can be stopped from the outside or from the inside.\n\nAPI:\n\n```php\nnamespace danog\\Loop;\n\nabstract class Loop\n{\n    /**\n     * Stop the loop.\n     */\n    public const STOP;\n    /**\n     * Pause the loop.\n     */\n    public const PAUSE;\n    /**\n     * Rerun the loop.\n     */\n    public const CONTINUE;\n\n    /**\n     * Loop body.\n     *\n     * The return value can be:\n     * A number - the loop will be paused for the specified number of seconds\n     * Loop::STOP - The loop will stop\n     * Loop::PAUSE - The loop will pause forever (or until loop is `resume()`'d\n     *                        from outside the loop)\n     * Loop::CONTINUE - Return this if you want to rerun the loop immediately\n     *\n     * The loop can be stopped from the outside by using stop().\n     * @return float|Loop::STOP|Loop::PAUSE|Loop::CONTINUE\n     */\n    abstract protected function loop(): ?float;\n\n    /**\n     * Loop name, useful for logging.\n     */\n    abstract public function __toString(): string;\n\n    /**\n     * Start the loop.\n     *\n     * Returns false if the loop is already running.\n     */\n    public function start(): bool;\n    /**\n     * Resume the loop.\n     *\n     * If resume is called multiple times, and the event loop hasn't resumed the loop yet,\n     * the loop will be resumed only once, not N times for every call.\n     *\n     * @param bool $postpone If true, multiple resumes will postpone the resuming to the end of the callback queue instead of leaving its position unchanged.\n     *\n     * @return bool Returns false if the loop is not paused.\n     */\n    public function resume(bool $postpone = false): bool;\n    /**\n     * Stops loop.\n     *\n     * Returns false if the loop is not running.\n     */\n    public function stop(): bool;\n\n    /**\n     * Check whether loop is running.\n     */\n    public function isRunning(): bool;\n    /**\n     * Check whether loop is paused.\n     */\n    public function isPaused(): bool;\n\n    /**\n     * Report pause, can be overriden for logging.\n     *\n     * @param float $timeout Pause duration, 0 = forever\n     */\n    protected function reportPause(float $timeout): void;\n\n    /**\n     * Signal that loop was started.\n     */\n    protected function startedLoop(): void;\n    /**\n     * Signal that loop has exited.\n     */\n    protected function exitedLoop(): void;\n}\n```\n\n### GenericLoop\n\n[Class](https://github.com/danog/loop/blob/master/lib/GenericLoop.php) - [Example](https://github.com/danog/loop/blob/master/examples/GenericLoop.php)\n\nIf you want a simpler way to use the `Loop`, you can use the GenericLoop.\n\n```php\nnamespace danog\\Loop;\n\nclass GenericLoop extends Loop\n{\n    /**\n     * Constructor.\n     *\n     * The return value of the callable can be:\n     * * A number - the loop will be paused for the specified number of seconds\n     * * GenericLoop::STOP - The loop will stop\n     * * GenericLoop::PAUSE - The loop will pause forever (or until loop is `resume()`'d\n     *                        from outside the loop)\n     * * GenericLoop::CONTINUE - Return this if you want to rerun the loop immediately\n     *\n     * If the callable does not return anything,\n     * the loop will behave is if GenericLoop::PAUSE was returned.\n     *\n     * The loop can be stopped from the outside by using stop().\n     *\n     * @param callable(static):?float $callable Callable to run\n     * @param string   $name     Loop name\n     */\n    public function __construct(callable $callable, private string $name);\n    /**\n     * Get loop name, provided to constructor.\n     */\n    public function __toString(): string;\n}\n```\n\n### PeriodicLoop\n\n[Class](https://github.com/danog/loop/blob/master/lib/PeriodicLoop.php) - [Example](https://github.com/danog/loop/blob/master/examples/PeriodicLoop.php)\n\nIf you simply want to execute an action every N seconds, [PeriodicLoop](https://github.com/danog/MadelineProto/blob/master/src/danog/MadelineProto/Loop/Generic/PeriodicLoop.php) is the way to go.\n\n```php\nnamespace danog\\Loop;\n\nclass PeriodicLoop extends GenericLoop\n{\n    /**\n     * Constructor.\n     *\n     * Runs a callback at a periodic interval.\n     *\n     * The loop can be stopped from the outside by calling stop()\n     * and from the inside by returning `true`.\n     *\n     * @param callable(static):bool $callback Callable to run\n     * @param string   $name     Loop name\n     * @param ?float   $interval Loop interval; if null, pauses indefinitely or until `resume()` is called.\n     */\n    public function __construct(callable $callback, string $name, ?float $interval)\n    /**\n     * Get name of the loop, passed to the constructor.\n     *\n     * @return string\n     */\n    public function __toString(): string;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanog%2Floop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanog%2Floop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanog%2Floop/lists"}