Improved sync and reopen of tasks.

This commit is contained in:
Martin Edenhofer 2013-04-20 18:22:01 +02:00
parent 2dc2287a2d
commit 6e882a7afb
2 changed files with 26 additions and 20 deletions

View file

@ -15,11 +15,13 @@ class App.TaskWidget extends App.Controller
App.TaskManager.reset() App.TaskManager.reset()
@el.html('') @el.html('')
@interval( App.TaskManager.syncInitial()
-> App.TaskManager.sync()
2000, sync = =>
'task-widget', App.TaskManager.sync()
) @delay( sync, 2000, 'task-widget' )
@delay( sync, 2500, 'task-widget' )
render: -> render: ->
@ -47,7 +49,6 @@ class App.TaskWidget extends App.Controller
tasks_all = App.TaskManager.all() tasks_all = App.TaskManager.all()
active_is_closed = false active_is_closed = false
for task_key, task of tasks_all for task_key, task of tasks_all
console.log('--', task_key, task)
if task.active && task_key.toString() is key.toString() if task.active && task_key.toString() is key.toString()
active_is_closed = true active_is_closed = true

View file

@ -24,6 +24,11 @@ class App.TaskManager
_instance ?= new _Singleton _instance ?= new _Singleton
_instance.reset() _instance.reset()
@syncInitial: ->
if _instance == undefined
_instance ?= new _Singleton
_instance.syncTasksInitial()
@sync: -> @sync: ->
if _instance == undefined if _instance == undefined
_instance ?= new _Singleton _instance ?= new _Singleton
@ -36,26 +41,12 @@ class _Singleton extends App.Controller
@tasks = {} @tasks = {}
@task_count = 0 @task_count = 0
# reopen tasks
cache = App.Store.get( 'tasks' )
if cache
task_count = 0
for task in cache
task_count += 1
@delay(
->
task = cache.shift()
App.TaskManager.add(task.type, task.type_id, task.callback, task.params, true)
task_count * 500
)
all: -> all: ->
@tasks @tasks
add: ( type, type_id, callback, params, to_not_show = false ) -> add: ( type, type_id, callback, params, to_not_show = false ) ->
for key, task of @tasks for key, task of @tasks
if task.type is type && task.type_id is type_id if task.type is type && task.type_id is type_id
console.log('STOP TASK, already exists', task)
return key if to_not_show return key if to_not_show
$('#content').empty() $('#content').empty()
$('.content_permanent').hide() $('.content_permanent').hide()
@ -138,6 +129,20 @@ class _Singleton extends App.Controller
syncLoad: => syncLoad: =>
App.Store.get( 'tasks' ) App.Store.get( 'tasks' )
syncTasksInitial: =>
# reopen tasks
store = _.clone(@syncLoad())
return if !store
task_count = 0
for task in store
task_count += 1
@delay(
=>
task = store.shift()
@add(task.type, task.type_id, task.callback, task.params, true)
task_count * 500
)
syncTasks: => syncTasks: =>
store = @syncLoad() || [] store = @syncLoad() || []