Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

em-scheduled-timer

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

em-scheduled-timer

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

EM::ScheduledTimer

EventMachine timers are great, but they work by waiting for a specified time interval before firing. Instead, EM::ScheduledTimer lets you specify a Time, Date or DateTime object (or indeed anything that will respond to to_time).

Why is this useful?

Imagine you're polling an HTTP-based API for changes. Because the API is nice, it will set Expires response headers so you know when to make the next request. With EM::ScheduledTimer (and the em-http-request gem), this becomes very easy:

def poll_api
  http = EM::HttpRequest.new("http://api.example.com/changes")
  http.callback do
    expires = http.response_header['EXPIRES']
    time = Time.httpdate(expires)
    EM::ScheduledTimer.new(time) { poll_api }
  end
end

Usage

Generally speaking, the API for a ScheduledTimer is modelled after that of a regular EM::Timer.

You can create EM::ScheduledTimer instances and pass in a block:

EM::ScheduledTimer.new(some_future_time) do
  puts "Fire!"
end

Alternatively, you can pass in any object that responds to #call (including a Proc):

callback = -> { puts "Fire!" }
EM::ScheduledTimer.new(some_future_time, callback)

A ScheduledTimer can also be cancelled:

timer = EM::ScheduledTimer.new(some_future_time) do
  puts "Fire!"
end

timer.cancel # The timer won't fire

As with regular timers, a convenience method is available on the EventMachine module:

EM.add_scheduled_timer(some_future_time) do
  puts "Fire!"
end

Note that in the latter case, you won't be able to cancel a timer that you've scheduled.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

FAQs

Package last updated on 06 Mar 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc