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() App.TaskManager.reset()
@el.html('') @el.html('')
@interval(
-> App.TaskManager.sync()
2000,
'task-widget',
)
render: -> render: ->
return if _.isEmpty( @Session.all() ) return if _.isEmpty( @Session.all() )

View file

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