Improved sync of open tasks in different browser windows. Issue #57.
This commit is contained in:
parent
a5963cf880
commit
2dc2287a2d
2 changed files with 61 additions and 19 deletions
|
@ -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() )
|
||||||
|
|
|
@ -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,24 +97,27 @@ 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
|
||||||
|
return if item.type is task.type && item.type_id is task.type_id
|
||||||
item =
|
item =
|
||||||
type: task.type
|
type: task.type
|
||||||
type_id: task.type_id
|
type_id: task.type_id
|
||||||
|
@ -119,7 +125,37 @@ class _Singleton extends Spine.Module
|
||||||
callback: task.callback
|
callback: task.callback
|
||||||
active: task.active
|
active: task.active
|
||||||
store.push item
|
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 )
|
||||||
|
|
Loading…
Reference in a new issue