mirror of
https://github.com/overleaf/overleaf.git
synced 2025-01-24 01:33:18 +00:00
e959529828
[web] Explicitly name angular parameters GitOrigin-RevId: 91beae68989d6c8122132b531a4338b116d87424
82 lines
2.2 KiB
JavaScript
82 lines
2.2 KiB
JavaScript
/* eslint-disable
|
|
max-len,
|
|
*/
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
// Fix any style issues and re-enable lint.
|
|
/*
|
|
* decaffeinate suggestions:
|
|
* DS101: Remove unnecessary use of Array.from
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
* DS207: Consider shorter variations of null checks
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
*/
|
|
import App from '../base'
|
|
|
|
export default App.factory('queuedHttp', [
|
|
'$http',
|
|
function ($http) {
|
|
const pendingRequests = []
|
|
let inflight = false
|
|
|
|
function processPendingRequests() {
|
|
if (inflight) {
|
|
return
|
|
}
|
|
const doRequest = pendingRequests.shift()
|
|
if (doRequest != null) {
|
|
inflight = true
|
|
return doRequest()
|
|
.then(function () {
|
|
inflight = false
|
|
return processPendingRequests()
|
|
})
|
|
.catch(function () {
|
|
inflight = false
|
|
return processPendingRequests()
|
|
})
|
|
}
|
|
}
|
|
|
|
const queuedHttp = function (...args) {
|
|
// We can't use Angular's $q.defer promises, because it only passes
|
|
// a single argument on error, and $http passes multiple.
|
|
const promise = {}
|
|
const successCallbacks = []
|
|
const errorCallbacks = []
|
|
|
|
// Adhere to the $http promise conventions
|
|
promise.then = function (callback, errCallback) {
|
|
successCallbacks.push(callback)
|
|
if (errCallback != null) {
|
|
errorCallbacks.push(errCallback)
|
|
}
|
|
return promise
|
|
}
|
|
|
|
promise.catch = function (callback) {
|
|
errorCallbacks.push(callback)
|
|
return promise
|
|
}
|
|
|
|
const doRequest = () =>
|
|
$http(...Array.from(args || []))
|
|
.then((...args) =>
|
|
Array.from(successCallbacks).map(fn =>
|
|
fn(...Array.from(args || []))
|
|
)
|
|
)
|
|
.catch((...args) =>
|
|
Array.from(errorCallbacks).map(fn => fn(...Array.from(args || [])))
|
|
)
|
|
|
|
pendingRequests.push(doRequest)
|
|
processPendingRequests()
|
|
|
|
return promise
|
|
}
|
|
|
|
queuedHttp.post = (url, data) => queuedHttp({ method: 'POST', url, data })
|
|
|
|
return queuedHttp
|
|
},
|
|
])
|