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

78 lines
1.7 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: ->
2012-03-17 15:08:33 +00:00
queueRunner: (failsafe = 5) ->
2012-03-15 15:57:35 +00:00
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)
2012-03-17 15:08:33 +00:00
if failsafe > 0
failsafe -= 1
Flowerbox.queueRunner(failsafe)
2012-03-15 15:57:35 +00:00
, 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