{"id":13601960,"url":"https://github.com/socketry/timers","last_synced_at":"2025-05-14T01:02:29.931Z","repository":{"id":3910825,"uuid":"4999671","full_name":"socketry/timers","owner":"socketry","description":"Pure Ruby timers collections suitable for use with event loops","archived":false,"fork":false,"pushed_at":"2025-02-07T21:39:14.000Z","size":249,"stargazers_count":339,"open_issues_count":1,"forks_count":33,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-03-23T21:05:12.387Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/socketry.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","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}},"created_at":"2012-07-12T06:14:38.000Z","updated_at":"2025-02-25T00:07:45.000Z","dependencies_parsed_at":"2023-01-13T12:49:11.099Z","dependency_job_id":"f5568c23-f120-4f19-b44d-7d433e072704","html_url":"https://github.com/socketry/timers","commit_stats":{"total_commits":241,"total_committers":32,"mean_commits":7.53125,"dds":0.6265560165975104,"last_synced_commit":"039bbd2750d5e50721789ef5d3404b18c36517bc"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Ftimers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Ftimers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Ftimers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Ftimers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/socketry","download_url":"https://codeload.github.com/socketry/timers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245863153,"owners_count":20684803,"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-08-01T18:01:10.731Z","updated_at":"2025-03-30T23:01:43.582Z","avatar_url":"https://github.com/socketry.png","language":"Ruby","funding_links":[],"categories":["Web 后端","Ruby"],"sub_categories":[],"readme":"# Timers\n\nCollections of one-shot and periodic timers, intended for use with event loops such as [async](https://github.com/socketry/async).\n\n[![Development Status](https://github.com/socketry/timers/workflows/Test/badge.svg)](https://github.com/socketry/timers/actions?workflow=Test)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n``` ruby\ngem 'timers'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install timers\n\n## Usage\n\nCreate a new timer group with `Timers::Group.new`:\n\n``` ruby\nrequire 'timers'\n\ntimers = Timers::Group.new\n```\n\nSchedule a proc to run after 5 seconds with `Timers::Group#after`:\n\n``` ruby\nfive_second_timer = timers.after(5) { puts \"Take five\" }\n```\n\nThe `five_second_timer` variable is now bound to a Timers::Timer object. To\ncancel a timer, use `Timers::Timer#cancel`\n\nOnce you've scheduled a timer, you can wait until the next timer fires with `Timers::Group#wait`:\n\n``` ruby\n# Waits 5 seconds\ntimers.wait\n\n# The script will now print \"Take five\"\n```\n\nYou can schedule a block to run periodically with `Timers::Group#every`:\n\n``` ruby\nevery_five_seconds = timers.every(5) { puts \"Another 5 seconds\" }\n\nloop { timers.wait }\n```\n\nYou can also schedule a block to run immediately and periodically with `Timers::Group#now_and_every`:\n\n``` ruby\nnow_and_every_five_seconds = timers.now_and_every(5) { puts \"Now and in another 5 seconds\" }\n\nloop { timers.wait }\n```\n\nIf you'd like another method to do the waiting for you, e.g. `Kernel.select`,\nyou can use `Timers::Group#wait_interval` to obtain the amount of time to wait. When\na timeout is encountered, you can fire all pending timers with `Timers::Group#fire`:\n\n``` ruby\nloop do\n  interval = timers.wait_interval\n  ready_readers, ready_writers = select readers, writers, nil, interval\n\n  if ready_readers || ready_writers\n    # Handle IO\n    ...\n  else\n    # Timeout!\n    timers.fire\n  end\nend\n```\n\nYou can also pause and continue individual timers, or all timers:\n\n``` ruby\npaused_timer = timers.every(5) { puts \"I was paused\" }\n\npaused_timer.pause\n10.times { timers.wait } # will not fire paused timer\n\npaused_timer.resume\n10.times { timers.wait } # will fire timer\n\ntimers.pause\n10.times { timers.wait } # will not fire any timers\n\ntimers.resume\n10.times { timers.wait } # will fire all timers\n```\n\n## Contributing\n\nWe welcome contributions to this project.\n\n1.  Fork it.\n2.  Create your feature branch (`git checkout -b my-new-feature`).\n3.  Commit your changes (`git commit -am 'Add some feature'`).\n4.  Push to the branch (`git push origin my-new-feature`).\n5.  Create new Pull Request.\n\n### Developer Certificate of Origin\n\nIn order to protect users of this project, we require all contributors to comply with the [Developer Certificate of Origin](https://developercertificate.org/). This ensures that all contributions are properly licensed and attributed.\n\n### Community Guidelines\n\nThis project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketry%2Ftimers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsocketry%2Ftimers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketry%2Ftimers/lists"}