Moved to App.Delay and App.Interval, each with own sigletons and level option (level page will be cleand after a new page controler is called).

This commit is contained in:
Martin Edenhofer 2013-03-14 15:51:11 +01:00
parent b20d1c4e07
commit bcd9395b5f
8 changed files with 247 additions and 54 deletions

View file

@ -203,48 +203,17 @@ class App.Controller extends Spine.Controller
@interval( update, 30000, 'frontendTimeUpdate' )
clearDelay: (delay_id) =>
clearDelay: (delay_id, level) =>
App.Delay.clear(delay_id, level)
# get global delay ids
current = @Config.get( delay_id, '_delayID' )
return if !current
clearTimeout( current )
delay: (callback, timeout, delay_id, level) =>
App.Delay.set(callback, timeout, delay_id, level)
delay: (callback, timeout, delay_id) =>
clearInterval: (interval_id, level) =>
App.Interval.clear(interval_id, level)
# request new data
call = =>
callback()
if delay_id
# clear current delay_id
@clearDelay( delay_id )
new_id = setTimeout( call, timeout )
@Config.set( delay_id, new_id, '_delayID' )
else
setTimeout( call, timeout )
clearInterval: (interval_id) =>
# get global interval ids
current = @Config.get( interval_id, '_intervalID' )
return if !current
clearInterval( current )
interval: (callback, interval, interval_id) =>
# clear current interval
@clearInterval( interval_id )
callback()
every = (ms, cb) =>
setInterval cb, ms
# request new data
new_id = every( interval, callback )
@Config.set( interval_id, new_id, '_intervalID' )
interval: (callback, interval, interval_id, level) =>
App.Interval.set(callback, interval, interval_id, level)
userPopups: (position = 'right') ->

View file

@ -53,7 +53,7 @@ class App.TicketHistory extends App.ControllerModal
@userPopups()
# show frontend times
@delay( @frontendTimeUpdate, 200 )
@delay( @frontendTimeUpdate, 200, 'ui-time-update' )
sortorder: (e) ->
e.preventDefault()
@ -79,4 +79,4 @@ class App.TicketHistory extends App.ControllerModal
@userPopups()
# show frontend times
@delay( @frontendTimeUpdate, 200 )
@delay( @frontendTimeUpdate, 200, 'ui-time-update' )

View file

@ -34,7 +34,7 @@ class Index extends App.Controller
@load(cache)
update = =>
@fetch( @ticket_id, false)
@interval( update, 30000, 'zoom_check' )
@interval( update, 30000, 'zoom_check', 'page' )
fetch: (ticket_id, force) ->
@ -177,7 +177,7 @@ class Index extends App.Controller
offset = offset - 45
scrollTo = ->
@scrollTo( 0, offset )
@delay( scrollTo, 100 )
@delay( scrollTo, 100, undefined, 'page' )
ticket_action_row: =>

View file

@ -33,6 +33,9 @@ class _Singleton extends Spine.Module
if key
@clear( key )
if !key
key = Math.floor( Math.random() * 99999 )
# setTimeout
@log 'Delay', 'debug', 'set', key, timeout, level, callback
call = =>
@ -41,7 +44,7 @@ class _Singleton extends Spine.Module
delay_id = setTimeout( call, timeout )
# remember all delays
@levelStack[ level ][ key ] = {
@levelStack[ level ][ key.toString() ] = {
delay_id: delay_id
timeout: timeout
level: level
@ -49,7 +52,7 @@ class _Singleton extends Spine.Module
return delay_id
clear: ( key ) ->
clear: ( key, level ) ->
if !level
level = '_all'
@ -58,7 +61,7 @@ class _Singleton extends Spine.Module
@levelStack[ level ] = {}
# get global delay ids
data = @levelStack[ level ][ key ]
data = @levelStack[ level ][ key.toString() ]
return if !data
@log 'Delay', 'debug', 'clear', data
@ -67,6 +70,6 @@ class _Singleton extends Spine.Module
clearLevel: (level) ->
return if !@levelStack[ level ]
for key, data of @levelStack[ level ]
@clear( key )
@clear( key, level )
@levelStack[level] = {}

View file

@ -42,6 +42,12 @@ class App.Content extends App.Controller
# remove events for page
App.Event.unbindLevel('page')
# remove delay for page
App.Delay.clearLevel('page')
# remove interval for page
App.Interval.clearLevel('page')
# unbind in controller area
@el.unbind()
@el.undelegate()

View file

@ -0,0 +1,73 @@
class App.Interval
_instance = undefined
@set: ( callback, timeout, key, level ) ->
if _instance == undefined
_instance ?= new _Singleton
_instance.set( callback, timeout, key, level )
@clear: ( key ) ->
if _instance == undefined
_instance ?= new _Singleton
_instance.clear( key )
@clearLevel: ( level ) ->
if _instance == undefined
_instance ?= new _Singleton
_instance.clearLevel( level )
class _Singleton extends Spine.Module
@include App.Log
constructor: ->
@levelStack = {}
set: ( callback, timeout, key, level ) ->
if !level
level = '_all'
if !@levelStack[level]
@levelStack[level] = {}
if key
@clear( key )
if !key
key = Math.floor( Math.random() * 99999 )
# setTimeout
@log 'Interval', 'debug', 'set', key, timeout, level, callback
callback()
interval_id = setInterval( callback, timeout )
# remember all interval
@levelStack[ level ][ key.toString() ] = {
interval_id: interval_id
timeout: timeout
level: level
}
return interval_id
clear: ( key, level ) ->
if !level
level = '_all'
if !@levelStack[ level ]
@levelStack[ level ] = {}
# get global interval ids
data = @levelStack[ level ][ key.toString() ]
return if !data
@log 'Interval', 'debug', 'clear', data
clearInterval( data['interval_id'] )
clearLevel: (level) ->
return if !@levelStack[ level ]
for key, data of @levelStack[ level ]
@clear( key, level )
@levelStack[level] = {}

View file

@ -120,11 +120,11 @@ class _Singleton extends App.Controller
@send( { action: 'ping' } )
# check if ping is back within 2 min
@clearDelay('websocket-ping-check')
@clearDelay('websocket-ping-check', 'ws')
check = =>
@log 'Websocket', 'notice', 'no websockend ping response, reconnect...'
@close()
@delay check, 120000, 'websocket-ping-check'
@delay check, 120000, 'websocket-ping-check', 'ws'
pong: ->
return if @backend is 'ajax'
@ -132,7 +132,7 @@ class _Singleton extends App.Controller
@log 'Websocket', 'debug', 'received websockend ping'
# test again after 1 min
@delay @ping, 60000
@delay @ping, 60000, undefined, 'ws'
connect: =>
return if @backend is 'ajax'
@ -171,7 +171,7 @@ class _Singleton extends App.Controller
@queue = []
# send ping to check connection
@delay @ping, 60000
@delay @ping, 60000, undefined, 'ws'
@ws.onmessage = (e) =>
pipe = JSON.parse( e.data )
@ -204,11 +204,11 @@ class _Singleton extends App.Controller
message: 'No connection to websocket, trying to reconnect...'
)
if !@tryToConnect
@delay message, 7000, 'websocket-no-connection-try-reconnect'
@delay message, 7000, 'websocket-no-connection-try-reconnect', 'ws'
@tryToConnect = true
# try reconnect after 4.5 sec.
@delay @connect, 4500
@delay @connect, 4500, undefined, 'ws'
@ws.onerror = (e) =>
@log 'Websocket', 'debug', "ws:onerror", e
@ -315,5 +315,5 @@ class _Singleton extends App.Controller
@client_id = undefined
@_ajaxInit( force: true )
@_ajaxReceiveWorking = false
@delay @_ajaxReceive, 5000
@delay @_ajaxReceive, 5000, undefined, 'ws'
)

View file

@ -99,6 +99,146 @@ App.Com.ajax({
}
});
// delay
App.Delay.set( function() {
test( "delay - test 1 - 1/2", function() {
// check
ok( !window.testDelay1, 'delay - test 1 - 1/2' );
window.testDelay1 = true;
});
},
1000,
'delay-test1',
'level'
);
App.Delay.set( function() {
test( "delay - test 1 - 2/2", function() {
// check
ok( window.testDelay1, 'delay - test 1 - 2/2' );
window.testDelay1 = 1;
});
},
2000,
'delay-test1',
'level'
);
App.Delay.set( function() {
test( "delay - test 2 - 1/3", function() {
// check
ok( !window.testDelay2, 'delay - test 2 - 1/3' );
window.testDelay2 = 1;
});
},
2000
);
App.Delay.set( function() {
test( "delay - test 2 - 2/3", function() {
// check
ok( !window.testDelay2, 'delay - test 2 - 2/3' );
});
},
1000
);
App.Delay.set( function() {
test( "delay - test 2 - 3/3", function() {
// check
ok( window.testDelay2, 'delay - test 2 - 3/3' );
});
},
3000
);
window.testDelay3 = 1;
App.Delay.set( function() {
test( "delay - test 3 - 1/1", function() {
// check
ok( false, 'delay - test 3 - 1/1' );
});
},
1000,
'delay3'
);
App.Delay.clear('delay3')
App.Delay.set( function() {
test( "delay - test 4 - 1/1", function() {
// check
ok( false, 'delay - test 4 - 1/1' );
});
},
1000,
undefined,
'Page'
);
App.Delay.clearLevel('Page')
// interval 1
window.testInterval1 = 1
App.Interval.set( function() {
window.testInterval1 += 1;
},
500,
'interval-test1'
);
App.Delay.set( function() {
test( "interval - test 1 - 1/1", function() {
// check
equal( window.testInterval1, 6, 'interval - test 1' );
App.Interval.clear('interval-test1')
});
},
2500
);
App.Delay.set( function() {
test( "interval - test 1 - 1/1", function() {
// check
equal( window.testInterval1, 6, 'interval - test after clear' );
});
},
3500
);
// interval 2
window.testInterval2 = 1
App.Interval.set( function() {
window.testInterval2 += 1;
},
500,
undefined,
'page'
);
App.Delay.set( function() {
test( "interval - test 2 - 1/1", function() {
// check
equal( window.testInterval2, 6, 'interval - test 2' );
App.Interval.clearLevel('page')
});
},
2500
);
App.Delay.set( function() {
test( "interval - test 2 - 1/1", function() {
// check
equal( window.testInterval2, 6, 'interval - test 2 - after clear' );
});
},
3500
);
// i18n
test( "i18n", function() {
@ -271,9 +411,11 @@ test( "config", function() {
});
// form
/*
test( "form", function() {
});
*/
// auth
App.Auth.login({