mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Queue up http requests so we can fire them in parallel without a problem
This commit is contained in:
parent
70673d2881
commit
e07aa9165f
2 changed files with 60 additions and 12 deletions
|
@ -15,7 +15,56 @@ define [
|
||||||
(date, format = "Do MMM YYYY, h:mm a") ->
|
(date, format = "Do MMM YYYY, h:mm a") ->
|
||||||
moment(date).format(format)
|
moment(date).format(format)
|
||||||
|
|
||||||
App.controller "ProjectPageController", ($scope, $modal, $http, $q) ->
|
App.factory "queuedHttp", ["$http", "$q", ($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
|
||||||
|
processRequests()
|
||||||
|
|
||||||
|
return promise
|
||||||
|
|
||||||
|
queuedHttp.post = (url, data) ->
|
||||||
|
queuedHttp({method: "POST", url: url, data: data})
|
||||||
|
|
||||||
|
return queuedHttp
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
App.controller "ProjectPageController", ($scope, $modal, $q, queuedHttp) ->
|
||||||
$scope.projects = window.data.projects
|
$scope.projects = window.data.projects
|
||||||
$scope.visibleProjects = $scope.projects
|
$scope.visibleProjects = $scope.projects
|
||||||
$scope.tags = window.data.tags
|
$scope.tags = window.data.tags
|
||||||
|
@ -155,7 +204,7 @@ define [
|
||||||
project.tags.splice(index, 1)
|
project.tags.splice(index, 1)
|
||||||
|
|
||||||
for project_id in removed_project_ids
|
for project_id in removed_project_ids
|
||||||
$http.post "/project/#{project_id}/tag", {
|
queuedHttp.post "/project/#{project_id}/tag", {
|
||||||
deletedTag: tag.name
|
deletedTag: tag.name
|
||||||
_csrf: window.csrfToken
|
_csrf: window.csrfToken
|
||||||
}
|
}
|
||||||
|
@ -181,8 +230,7 @@ define [
|
||||||
project.tags.push tag
|
project.tags.push tag
|
||||||
|
|
||||||
for project_id in added_project_ids
|
for project_id in added_project_ids
|
||||||
# TODO Factor this out into another provider?
|
queuedHttp.post "/project/#{project_id}/tag", {
|
||||||
$http.post "/project/#{project_id}/tag", {
|
|
||||||
tag: tag.name
|
tag: tag.name
|
||||||
_csrf: window.csrfToken
|
_csrf: window.csrfToken
|
||||||
}
|
}
|
||||||
|
@ -208,7 +256,7 @@ define [
|
||||||
$scope.createProject = (name, template = "none") ->
|
$scope.createProject = (name, template = "none") ->
|
||||||
deferred = $q.defer()
|
deferred = $q.defer()
|
||||||
|
|
||||||
$http
|
queuedHttp
|
||||||
.post("/project/new", {
|
.post("/project/new", {
|
||||||
_csrf: window.csrfToken
|
_csrf: window.csrfToken
|
||||||
projectName: name
|
projectName: name
|
||||||
|
@ -245,7 +293,7 @@ define [
|
||||||
|
|
||||||
$scope.renameProject = (project, newName) ->
|
$scope.renameProject = (project, newName) ->
|
||||||
project.name = newName
|
project.name = newName
|
||||||
$http.post "/project/#{project.id}/rename", {
|
queuedHttp.post "/project/#{project.id}/rename", {
|
||||||
newProjectName: newName
|
newProjectName: newName
|
||||||
_csrf: window.csrfToken
|
_csrf: window.csrfToken
|
||||||
}
|
}
|
||||||
|
@ -269,7 +317,7 @@ define [
|
||||||
$scope.cloneProject = (project, cloneName) ->
|
$scope.cloneProject = (project, cloneName) ->
|
||||||
deferred = $q.defer()
|
deferred = $q.defer()
|
||||||
|
|
||||||
$http
|
queuedHttp
|
||||||
.post("/project/#{project.id}/clone", {
|
.post("/project/#{project.id}/clone", {
|
||||||
_csrf: window.csrfToken
|
_csrf: window.csrfToken
|
||||||
projectName: cloneName
|
projectName: cloneName
|
||||||
|
@ -325,7 +373,7 @@ define [
|
||||||
for project in selected_projects
|
for project in selected_projects
|
||||||
if project.accessLevel == "owner"
|
if project.accessLevel == "owner"
|
||||||
project.archived = true
|
project.archived = true
|
||||||
$http {
|
queuedHttp {
|
||||||
method: "DELETE"
|
method: "DELETE"
|
||||||
url: "/project/#{project.id}"
|
url: "/project/#{project.id}"
|
||||||
headers:
|
headers:
|
||||||
|
@ -334,7 +382,7 @@ define [
|
||||||
else
|
else
|
||||||
$scope._removeProjectFromList project
|
$scope._removeProjectFromList project
|
||||||
|
|
||||||
$http {
|
queuedHttp {
|
||||||
method: "POST"
|
method: "POST"
|
||||||
url: "/project/#{project.id}/leave"
|
url: "/project/#{project.id}/leave"
|
||||||
headers:
|
headers:
|
||||||
|
@ -368,7 +416,7 @@ define [
|
||||||
$scope._removeProjectIdsFromTagArray(tag, selected_project_ids)
|
$scope._removeProjectIdsFromTagArray(tag, selected_project_ids)
|
||||||
|
|
||||||
for project_id in selected_project_ids
|
for project_id in selected_project_ids
|
||||||
$http {
|
queuedHttp {
|
||||||
method: "DELETE"
|
method: "DELETE"
|
||||||
url: "/project/#{project_id}?forever=true"
|
url: "/project/#{project_id}?forever=true"
|
||||||
headers:
|
headers:
|
||||||
|
@ -385,7 +433,7 @@ define [
|
||||||
project.archived = false
|
project.archived = false
|
||||||
|
|
||||||
for project_id in selected_project_ids
|
for project_id in selected_project_ids
|
||||||
$http {
|
queuedHttp {
|
||||||
method: "POST"
|
method: "POST"
|
||||||
url: "/project/#{project_id}/restore"
|
url: "/project/#{project_id}/restore"
|
||||||
headers:
|
headers:
|
||||||
|
|
Loading…
Reference in a new issue