Refactoring for task management.

This commit is contained in:
Martin Edenhofer 2013-06-25 09:56:45 +02:00
parent 4534b3fbba
commit aa5830e123
2 changed files with 69 additions and 40 deletions

View file

@ -128,7 +128,6 @@ class App.TaskWidget extends App.Controller
# remove task # remove task
App.TaskManager.remove( key ) App.TaskManager.remove( key )
@render()
# navigate to next task if needed # navigate to next task if needed
tasks = App.TaskManager.all() tasks = App.TaskManager.all()

View file

@ -67,15 +67,28 @@ class _taskManagerSingleton extends App.Controller
@workers = {} @workers = {}
@workersStarted = {} @workersStarted = {}
@taskUpdateProcess = {} @taskUpdateProcess = {}
@activeTask = undefined @allTasks = []
@tasksToUpdate = {}
@initialLoad = true
@activeTask = undefined
@tasksInitial() @tasksInitial()
@interval( @taskUpdateLoop, 2500 )
all: -> all: ->
tasks = App.Taskbar.all()
tasks = _(tasks).sortBy( (task) -> # initial load of taskbar collection
if @initialLoad
@initialLoad = false
tasks = App.Taskbar.all()
for task in tasks
@allTasks.push task.attributes()
# sort by prio
@allTasks = _(@allTasks).sortBy( (task) ->
return task.prio; return task.prio;
) )
return tasks return @allTasks
worker: ( key ) -> worker: ( key ) ->
return @workers[ key ] if @workers[ key ] return @workers[ key ] if @workers[ key ]
@ -99,13 +112,21 @@ class _taskManagerSingleton extends App.Controller
params: params params: params
callback: callback callback: callback
client_id: 123 client_id: 123
prio: App.Taskbar.count() + 1 prio: @allTasks.length + 1
notify: false notify: false
active: active active: active
) )
task.save() @allTasks.push task.attributes()
tasks = @all() # save new task and update task collection
ui = @
task.save(
success: ->
for taskPosition of ui.allTasks
if ui.allTasks[taskPosition] && ui.allTasks[taskPosition]['key'] is @key
task = @attributes()
ui.allTasks[taskPosition] = task
)
# create div for permanent content # create div for permanent content
if !$("#content_permanent")[0] if !$("#content_permanent")[0]
@ -134,7 +155,7 @@ class _taskManagerSingleton extends App.Controller
# set all tasks to active false, only new/selected one to active # set all tasks to active false, only new/selected one to active
if active if active
for task in tasks for task in @allTasks
if task.key isnt key if task.key isnt key
if task.active if task.active
task.active = false task.active = false
@ -150,7 +171,7 @@ class _taskManagerSingleton extends App.Controller
if changed if changed
@taskUpdate( task ) @taskUpdate( task )
else else
for task in tasks for task in @allTasks
if @activeTask isnt task.key if @activeTask isnt task.key
if task.active if task.active
task.active = false task.active = false
@ -199,8 +220,7 @@ class _taskManagerSingleton extends App.Controller
return a return a
get: ( key ) => get: ( key ) =>
tasks = @all() for task in @allTasks
for task in tasks
return task if task.key is key return task if task.key is key
return return
# throw "No such task with '#{key}'" # throw "No such task with '#{key}'"
@ -243,6 +263,7 @@ class _taskManagerSingleton extends App.Controller
@taskUpdate( task ) @taskUpdate( task )
reset: => reset: =>
@allTasks = []
App.Taskbar.deleteAll() App.Taskbar.deleteAll()
App.Event.trigger 'task:render' App.Event.trigger 'task:render'
@ -252,35 +273,43 @@ class _taskManagerSingleton extends App.Controller
@TaskbarIdInt @TaskbarIdInt
taskUpdate: (task) -> taskUpdate: (task) ->
@log 'notice', "UPDATE task #{task.id}" @log 'notice', "UPDATE task #{task.id}", task
update = => @tasksToUpdate[ task.key ] = 'toUpdate'
console.log('update', @taskUpdateProcess) App.Event.trigger 'task:render'
if task.isOnline()
if !@taskUpdateProcess[task.id] taskUpdateLoop: =>
@taskUpdateProcess[task.id] = 0 for key of @tasksToUpdate
@taskUpdateProcess[task.id]++ continue if !key
task.save( task = @get( key )
success: => continue if !task
@taskUpdateProcess[task.id]-- if @tasksToUpdate[ task.key ] is 'toUpdate'
console.log('update done', @taskUpdateProcess) @tasksToUpdate[ task.key ] = 'inProgress'
error: (task) => taskUpdate = new App.Taskbar
@taskUpdateProcess[task.id]-- taskUpdate.load( task )
console.log('update done', @taskUpdateProcess) if taskUpdate.isOnline()
) ui = @
App.Event.trigger 'task:render' taskUpdate.save(
@delay( update, 100, task.id, 'taskbar' ) success: ->
if ui.tasksToUpdate[ @key ] is 'inProgress'
delete ui.tasksToUpdate[ @key ]
error: (task) =>
if ui.tasksToUpdate[ @key ] is 'inProgress'
delete ui.tasksToUpdate[ @key ]
)
taskDestroy: (task) -> taskDestroy: (task) ->
@clearDelay( task.id, 'taskbar' ) allTasks = _.filter(
destroy = -> @allTasks
task.destroy( (taskLocal) ->
success: -> return task if task.key isnt taskLocal.key
App.Event.trigger 'task:render' return
) )
console.log('delete', @taskUpdateProcess) @allTasks = allTasks || []
App.Event.trigger 'task:render'
# check if update is still in process # check if update is still in process
if @taskUpdateProcess[task.id] if @tasksToUpdate[ task.key ] is 'inProgress'
console.log( 'DESTROY', task, @tasksToUpdate[ task.key ], task.key)
@delay( @delay(
=> @taskDestroy(task) => @taskDestroy(task)
800 800
@ -288,7 +317,9 @@ class _taskManagerSingleton extends App.Controller
return return
# destory task in backend # destory task in backend
destroy() delete @tasksToUpdate[ task.key ]
App.Taskbar.destroy(task.id)
return
tasksInitial: => tasksInitial: =>
# reopen tasks # reopen tasks
@ -307,7 +338,6 @@ class _taskManagerSingleton extends App.Controller
taskbar_id: @TaskbarId() taskbar_id: @TaskbarId()
) )
# App.Taskbar.fetch()
tasks = @all() tasks = @all()
return if !tasks return if !tasks
@ -318,7 +348,7 @@ class _taskManagerSingleton extends App.Controller
=> =>
task = tasks.shift() task = tasks.shift()
@add(task.key, task.callback, task.params, true, task.state) @add(task.key, task.callback, task.params, true, task.state)
task_count * 350 task_count * 300
) )
App.Event.trigger 'taskbar:ready' App.Event.trigger 'taskbar:ready'