Improved memory usage.

This commit is contained in:
Martin Edenhofer 2016-09-27 23:15:27 +02:00
parent 6b81b791ed
commit 0d377d40ab

View file

@ -40,25 +40,25 @@ class _ajaxSingleton
cache: false cache: false
async: true async: true
current_request: {} currentRequest: {}
queue_list: [] queueList: []
queue_running: false queueRunning: false
count: 0 count: 0
constructor: (@args) -> constructor: (@args) ->
# run queue # run queue
@_run() @runNextInQueue()
# bindings # bindings
$(document).bind( 'ajaxSend', => $(document).bind('ajaxSend', =>
@_show_spinner() @_show_spinner()
).bind( 'ajaxComplete', => ).bind('ajaxComplete', =>
@_hide_spinner() @_hide_spinner()
) )
# show error messages # show error messages
$(document).bind( 'ajaxError', ( e, jqxhr, settings, exception ) -> $(document).bind('ajaxError', (e, jqxhr, settings, exception) ->
status = jqxhr.status status = jqxhr.status
detail = jqxhr.responseText detail = jqxhr.responseText
if !status && !detail if !status && !detail
@ -80,68 +80,79 @@ class _ajaxSingleton
# show error message # show error message
new App.ControllerModal( new App.ControllerModal(
head: 'StatusCode: ' + status head: "StatusCode: #{status}"
contentInline: '<pre>' + App.Utils.htmlEscape(detail) + '</pre>' contentInline: "<pre>#{App.Utils.htmlEscape(detail)}</pre>"
buttonClose: true buttonClose: true
buttonSubmit: false buttonSubmit: false
) )
) )
request: (params) -> request: (params) ->
data = $.extend({}, @defaults, params ) data = $.extend({}, @defaults, params)
# execute call with id, clear old call first if exists # execute call with id, clear old call first if exists
if params['id'] if params['id']
@abort( params['id'] ) @abort(params['id'])
@current_request[ params['id'] ] = $.ajax( data ) @addCurrentRequest(params['id'], data)
return params['id'] return params['id']
# generate a uniq rand id # generate a uniq rand id
params['id'] = 'rand-' + new Date().getTime() + '-' + Math.floor( Math.random() * 99999 ) params['id'] = "rand-#{new Date().getTime()}-#{Math.floor(Math.random() * 99999)}"
# queue request # queue request
if params['queue'] if params['queue']
@queue_list.push data @queueList.push data
if !@queue_running if !@queueRunning
@_run() @runNextInQueue()
# execute request # execute request
else else
@current_request[ params['id'] ] = $.ajax(data) @addCurrentRequest(params['id'], data)
params['id'] params['id']
addCurrentRequest: (id, data, queueRunning) =>
data.complete = =>
if queueRunning
@queueRunning = false
@removeCurrentRequest(id)
if queueRunning
@runNextInQueue()
@currentRequest[id] = $.ajax(data)
return if data.async is true
@removeCurrentRequest(id)
removeCurrentRequest: (id) =>
@currentRequest[id] = undefined
delete @currentRequest[id]
abort: (id) => abort: (id) =>
# abort current_request # abort currentRequest
if @current_request[ id ] if @currentRequest[id]
@current_request[ id ].abort() @currentRequest[id].abort()
delete @current_request[ id ] @currentRequest[id] = undefined
delete @currentRequest[id]
# remove from queue list # remove from queue list
@queue_list = _.filter( @queueList = _.filter(
@queue_list @queueList
(item) -> (item) ->
return item if item['id'] isnt id return item if item['id'] isnt id
return return
) )
abortAll: => abortAll: =>
return if !@current_request
abortedIds = [] abortedIds = []
for id, ajax of @current_request for id, ajax of @currentRequest
@abort(id) @abort(id)
abortedIds.push id abortedIds.push id
abortedIds abortedIds
_run: => runNextInQueue: =>
if @queue_list && @queue_list[0] if @queueList && @queueList[0]
@queue_running = true @queueRunning = true
request = @queue_list.shift() data = @queueList.shift()
request.complete = => @addCurrentRequest(params['id'], data, true)
@queue_running = false
@_run()
@current_request[ request['id'] ] = $.ajax( request )
_show_spinner: => _show_spinner: =>
@count++ @count++