Sha256: 9a892436e0b8a988c7f543823bed5e33fc30fc05d7f258c07dfc4042d82c3ce9

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

# 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](https://github.com/igrigorik/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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
em-scheduled-timer-0.1.0 README.md