Improved sync of open tasks in different browser windows. Issue #57.

This commit is contained in:
Martin Edenhofer 2013-04-20 16:44:53 +02:00
parent a5963cf880
commit 2dc2287a2d
2 changed files with 61 additions and 19 deletions

View file

@ -15,6 +15,12 @@ class App.TaskWidget extends App.Controller
App.TaskManager.reset()
@el.html('')
@interval(
-> App.TaskManager.sync()
2000,
'task-widget',
)
render: ->
return if _.isEmpty( @Session.all() )

View file

@ -24,7 +24,12 @@ class App.TaskManager
_instance ?= new _Singleton
_instance.reset()
class _Singleton extends Spine.Module
@sync: ->
if _instance == undefined
_instance ?= new _Singleton
_instance.syncTasks()
class _Singleton extends App.Controller
@include App.Log
constructor: ->
@ -37,7 +42,7 @@ class _Singleton extends Spine.Module
task_count = 0
for task in cache
task_count += 1
setTimeout(
@delay(
->
task = cache.shift()
App.TaskManager.add(task.type, task.type_id, task.callback, task.params, true)
@ -62,7 +67,6 @@ class _Singleton extends Spine.Module
else
task.active = true
App.Event.trigger 'ui:rerender'
@syncTasks()
return key
@task_count++
@ -72,7 +76,6 @@ class _Singleton extends Spine.Module
active = true
if to_not_show
active = false
console.log('start...', type, type_id, callback, params, @task_count )
if active
$('#content').empty()
@ -94,24 +97,27 @@ class _Singleton extends Spine.Module
active: active
@tasks[@task_count] = task
App.Event.trigger 'ui:rerender'
@syncTasks()
if !to_not_show
@syncAdd(task)
@task_count
remove: ( key ) =>
remove: ( key, to_not_show = false ) =>
if @tasks[key]
@tasks[key].worker.release()
if !to_not_show
@syncRemove( @tasks[key] )
delete @tasks[key]
App.Event.trigger 'ui:rerender'
@syncTasks()
reset: =>
@tasks = {}
App.Event.trigger 'ui:rerender'
syncTasks: =>
store = []
for task_key, task of @tasks
syncAdd: (task) =>
store = @syncLoad() || []
for item in store
return if item.type is task.type && item.type_id is task.type_id
item =
type: task.type
type_id: task.type_id
@ -119,7 +125,37 @@ class _Singleton extends Spine.Module
callback: task.callback
active: task.active
store.push item
console.log('to write', store)
App.Store.write( 'tasks', store )
syncRemove: (task) =>
store = @syncLoad() || []
storeNew = []
for item in store
if item.type isnt task.type || item.type_id isnt task.type_id
storeNew.push item
App.Store.write( 'tasks', storeNew )
syncLoad: =>
App.Store.get( 'tasks' )
syncTasks: =>
store = @syncLoad() || []
# open tasks
for item in store
existsLocal = false
for task_key, task of @tasks
if item.type is task.type && item.type_id is task.type_id
# also open here
existsLocal = true
if !existsLocal
@add(item.type, item.type_id, item.callback, item.params, true)
# close tasks
for task_key, task of @tasks
onlyLocal = true
for item in store
if item.type is task.type && item.type_id is task.type_id
onlyLocal = false
if onlyLocal
@remove( task_key, true )