mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-14 20:40:17 -05:00
50 lines
No EOL
1 KiB
CoffeeScript
50 lines
No EOL
1 KiB
CoffeeScript
define [
|
|
"base"
|
|
], (App) ->
|
|
|
|
App.factory "queuedHttp", ($http, $q) ->
|
|
pendingRequests = []
|
|
inflight = false
|
|
|
|
processPendingRequests = () ->
|
|
return if inflight
|
|
doRequest = pendingRequests.shift()
|
|
if doRequest?
|
|
inflight = true
|
|
doRequest()
|
|
.success () ->
|
|
inflight = false
|
|
processPendingRequests()
|
|
.error () ->
|
|
inflight = false
|
|
processPendingRequests()
|
|
|
|
queuedHttp = (args...) ->
|
|
deferred = $q.defer()
|
|
promise = deferred.promise
|
|
|
|
# Adhere to the $http promise conventions
|
|
promise.success = (callback) ->
|
|
promise.then(callback)
|
|
return promise
|
|
|
|
promise.error = (callback) ->
|
|
promise.catch(callback)
|
|
return promise
|
|
|
|
doRequest = () ->
|
|
$http(args...)
|
|
.success (successArgs...) ->
|
|
deferred.resolve(successArgs...)
|
|
.error (errorArgs...) ->
|
|
deferred.reject(errorArgs...)
|
|
|
|
pendingRequests.push doRequest
|
|
processPendingRequests()
|
|
|
|
return promise
|
|
|
|
queuedHttp.post = (url, data) ->
|
|
queuedHttp({method: "POST", url: url, data: data})
|
|
|
|
return queuedHttp |