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: '/'
|
2012-03-14 17:07:13 +00:00
|
|
|
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...) ->
|
2012-03-14 17:07:13 +00:00
|
|
|
if !Flowerbox.debug
|
2012-03-15 00:32:01 +00:00
|
|
|
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 00:32:01 +00:00
|
|
|
|
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")
|
2012-03-15 00:32:01 +00:00
|
|
|
if Flowerbox.contactQueue.length > 0
|
2012-03-15 15:57:35 +00:00
|
|
|
Flowerbox.started = true
|
|
|
|
|
|
|
|
info = Flowerbox.contactQueue.shift()
|
|
|
|
|
|
|
|
[ url, data ] = info
|
2012-03-15 00:32:01 +00:00
|
|
|
|
|
|
|
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
|
2012-03-15 00:32:01 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2012-03-15 00:32:01 +00:00
|
|
|
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
|
|
|
|