use a queue to run xhr jobs, so node isn't super slow

This commit is contained in:
John Bintz 2012-03-14 20:32:01 -04:00
parent d8ef63be6a
commit 2cb9e44629

View File

@ -7,25 +7,33 @@ Flowerbox =
debug: false debug: false
ping: -> ping: ->
Flowerbox.contact('ping') Flowerbox.contact('ping')
working: false
contact: (url, data...) -> contact: (url, data...) ->
if !Flowerbox.debug if !Flowerbox.debug
attempts = 3 Flowerbox.contactQueue ||= []
doContact = -> Flowerbox.contactQueue.push([url, data])
attempts -= 1 Flowerbox.workOffQueue()
try workOffQueue: ->
xhr = new XMLHttpRequest() if !Flowerbox.working
xhr.open("POST", Flowerbox.baseUrl + url, false) Flowerbox.working = true
xhr.setRequestHeader("Accept", "application/json") Flowerbox.doWorkOffQueue()
xhr.send(JSON.stringify(data))
catch e doWorkOffQueue: ->
if attempts == 0 if Flowerbox.contactQueue.length > 0
throw e [ url, data ] = Flowerbox.contactQueue.shift()
else
doContact() xhr = new XMLHttpRequest()
xhr.open("POST", Flowerbox.baseUrl + url, true)
xhr.setRequestHeader("Accept", "application/json")
xhr.onreadystatechange = ->
if @readyState == @DONE
Flowerbox.doWorkOffQueue()
xhr.send(JSON.stringify(data))
else
Flowerbox.working = false
doContact()
fail: -> fail: ->