jQuery helper object for AJAX polling a progress-aware endpoint.
req = new PollingRequest
url: "/states.csv"
interval: 1000 # polling interval in milliseconds
progress: (n)->
console.log "Progress: #{n} percent"
success: (res)->
console.log "Response body: #{res}"
error: (status, response)->
console.log("Status: #{status}, Body: #{response}")
complete: (status, response)->
console.log("This is called after everything else")
req.status # 'pending'
req.start()
req.status # 'running'
req.progress # 0
# sometime later
req.status # 'running'
req.progress # 50
# much later
req.status # 'success'
req.progress # 100
# example of setting a hard timeout
setTimeout ->
req.stop()
, 5000
PollingRequests will have a header X-POLLING-REQUEST
set. It expects an HTTP
endpoint to respond with a 202 Accepted status code when there is more
processing to be done. The JSON response body can optionally include a
progress
property whose value is an integer 0..100 inclusive to indicate the
percentage of processing completed.
$ curl -i http://localhost/endpoint
HTTP/1.1 202 Accepted
{ progress: 0 }
$ curl -i http://localhost/endpoint
HTTP/1.1 202 Accepted
{ progress: 50 }
$ curl -i http://localhost/endpoint
HTTP/1.1 200 Success
Content-Type: text/plain
Successful responses can be any content type
To access PollingRequest from the asset pipeline, add the following to your Gemfile:
gem 'polling_request'
And this to your asset manifest:
//= require polling_request
Tests are written with QUnit and can be run in a browser by opening
test/test.html
. Continuous integration is run with phantomjs:
$ phantomjs test/runner.js test/test.html
- Better docs for building and installing project
- Bower compatibility
- Travis CI
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request