flowerbox/lib/assets/javascripts/flowerbox.js.coffee

75 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2012-03-13 17:21:32 +00:00
#= require_self
#= require flowerbox/result
#= require flowerbox/exception
#
2012-03-02 18:28:52 +00:00
Flowerbox =
baseUrl: '/'
debug: false
2012-03-13 17:21:32 +00:00
ping: ->
Flowerbox.contact('ping')
2012-03-15 15:57:35 +00:00
contactQueue: []
2012-03-13 17:21:32 +00:00
2012-03-02 18:28:52 +00:00
contact: (url, data...) ->
if !Flowerbox.debug
Flowerbox.contactQueue.push([url, data])
2012-03-13 17:21:32 +00:00
2012-03-15 15:57:35 +00:00
queueIndex: 0
delay: 40
started: false
done: false
2012-03-15 15:57:35 +00:00
onQueueStateChange: ->
queueRunner: ->
Flowerbox.onQueueStateChange("checking queue")
if Flowerbox.contactQueue.length > 0
2012-03-15 15:57:35 +00:00
Flowerbox.started = true
info = Flowerbox.contactQueue.shift()
[ url, data ] = info
xhr = new XMLHttpRequest()
xhr.open("POST", Flowerbox.baseUrl + url, true)
xhr.setRequestHeader("Accept", "application/json")
2012-03-15 15:57:35 +00:00
done = false
xhr.onreadystatechange = ->
2012-03-15 15:57:35 +00:00
if xhr.readyState == 4
done = true
Flowerbox.onQueueStateChange("done #{url}")
if url == "results"
Flowerbox.onQueueStateChange("finsihed all tests")
Flowerbox.done = true
else
setTimeout(
->
Flowerbox.queueRunner()
, 1
)
Flowerbox.onQueueStateChange("running #{url}")
setTimeout(
->
if xhr.readyState != 4
xhr.abort()
Flowerbox.onQueueStateChange("aborted #{url}, rerunning")
Flowerbox.contactQueue.unshift(info)
Flowerbox.queueRunner()
, Flowerbox.delay * 5
)
xhr.send(JSON.stringify(data))
else
2012-03-15 15:57:35 +00:00
Flowerbox.startQueueRunner()
startQueueRunner: ->
setTimeout(
->
Flowerbox.queueRunner()
, Flowerbox.delay
)
2012-03-13 17:21:32 +00:00
2012-03-03 16:28:03 +00:00
fail: ->
2012-03-13 17:21:32 +00:00