jQuery helper object for AJAX polling a progress-aware endpoint.
Usage
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
Progress Aware Endpoint
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
Rails
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
Bower
PollingRequest is in the Bower registry. JavaScript and CoffeeScript sources are included.
$ bower install polling-request
Development
Dependencies are managed by npm and Bower.
$ npm install
$ bower install
Tests are written with QUnit and can be run in a browser by opening
test/test.html
. Continuous integration is run with phantomjs:
$ ./node_modules/phantomjs/lib/phantom/bin/phantomjs test/runner.js test/test.html
Wish List
- Better docs for building and installing project
Contributing
- 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