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:
parent
b20d1c4e07
commit
bcd9395b5f
8 changed files with 247 additions and 54 deletions
|
@ -203,48 +203,17 @@ class App.Controller extends Spine.Controller
|
||||||
@interval( update, 30000, 'frontendTimeUpdate' )
|
@interval( update, 30000, 'frontendTimeUpdate' )
|
||||||
|
|
||||||
|
|
||||||
clearDelay: (delay_id) =>
|
clearDelay: (delay_id, level) =>
|
||||||
|
App.Delay.clear(delay_id, level)
|
||||||
|
|
||||||
# get global delay ids
|
delay: (callback, timeout, delay_id, level) =>
|
||||||
current = @Config.get( delay_id, '_delayID' )
|
App.Delay.set(callback, timeout, delay_id, level)
|
||||||
return if !current
|
|
||||||
clearTimeout( current )
|
|
||||||
|
|
||||||
delay: (callback, timeout, delay_id) =>
|
clearInterval: (interval_id, level) =>
|
||||||
|
App.Interval.clear(interval_id, level)
|
||||||
|
|
||||||
# request new data
|
interval: (callback, interval, interval_id, level) =>
|
||||||
call = =>
|
App.Interval.set(callback, interval, interval_id, level)
|
||||||
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' )
|
|
||||||
|
|
||||||
userPopups: (position = 'right') ->
|
userPopups: (position = 'right') ->
|
||||||
|
|
||||||
|
|
|
@ -53,7 +53,7 @@ class App.TicketHistory extends App.ControllerModal
|
||||||
@userPopups()
|
@userPopups()
|
||||||
|
|
||||||
# show frontend times
|
# show frontend times
|
||||||
@delay( @frontendTimeUpdate, 200 )
|
@delay( @frontendTimeUpdate, 200, 'ui-time-update' )
|
||||||
|
|
||||||
sortorder: (e) ->
|
sortorder: (e) ->
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
@ -79,4 +79,4 @@ class App.TicketHistory extends App.ControllerModal
|
||||||
@userPopups()
|
@userPopups()
|
||||||
|
|
||||||
# show frontend times
|
# show frontend times
|
||||||
@delay( @frontendTimeUpdate, 200 )
|
@delay( @frontendTimeUpdate, 200, 'ui-time-update' )
|
||||||
|
|
|
@ -34,7 +34,7 @@ class Index extends App.Controller
|
||||||
@load(cache)
|
@load(cache)
|
||||||
update = =>
|
update = =>
|
||||||
@fetch( @ticket_id, false)
|
@fetch( @ticket_id, false)
|
||||||
@interval( update, 30000, 'zoom_check' )
|
@interval( update, 30000, 'zoom_check', 'page' )
|
||||||
|
|
||||||
fetch: (ticket_id, force) ->
|
fetch: (ticket_id, force) ->
|
||||||
|
|
||||||
|
@ -177,7 +177,7 @@ class Index extends App.Controller
|
||||||
offset = offset - 45
|
offset = offset - 45
|
||||||
scrollTo = ->
|
scrollTo = ->
|
||||||
@scrollTo( 0, offset )
|
@scrollTo( 0, offset )
|
||||||
@delay( scrollTo, 100 )
|
@delay( scrollTo, 100, undefined, 'page' )
|
||||||
|
|
||||||
ticket_action_row: =>
|
ticket_action_row: =>
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,9 @@ class _Singleton extends Spine.Module
|
||||||
if key
|
if key
|
||||||
@clear( key )
|
@clear( key )
|
||||||
|
|
||||||
|
if !key
|
||||||
|
key = Math.floor( Math.random() * 99999 )
|
||||||
|
|
||||||
# setTimeout
|
# setTimeout
|
||||||
@log 'Delay', 'debug', 'set', key, timeout, level, callback
|
@log 'Delay', 'debug', 'set', key, timeout, level, callback
|
||||||
call = =>
|
call = =>
|
||||||
|
@ -41,7 +44,7 @@ class _Singleton extends Spine.Module
|
||||||
delay_id = setTimeout( call, timeout )
|
delay_id = setTimeout( call, timeout )
|
||||||
|
|
||||||
# remember all delays
|
# remember all delays
|
||||||
@levelStack[ level ][ key ] = {
|
@levelStack[ level ][ key.toString() ] = {
|
||||||
delay_id: delay_id
|
delay_id: delay_id
|
||||||
timeout: timeout
|
timeout: timeout
|
||||||
level: level
|
level: level
|
||||||
|
@ -49,7 +52,7 @@ class _Singleton extends Spine.Module
|
||||||
|
|
||||||
return delay_id
|
return delay_id
|
||||||
|
|
||||||
clear: ( key ) ->
|
clear: ( key, level ) ->
|
||||||
|
|
||||||
if !level
|
if !level
|
||||||
level = '_all'
|
level = '_all'
|
||||||
|
@ -58,7 +61,7 @@ class _Singleton extends Spine.Module
|
||||||
@levelStack[ level ] = {}
|
@levelStack[ level ] = {}
|
||||||
|
|
||||||
# get global delay ids
|
# get global delay ids
|
||||||
data = @levelStack[ level ][ key ]
|
data = @levelStack[ level ][ key.toString() ]
|
||||||
return if !data
|
return if !data
|
||||||
|
|
||||||
@log 'Delay', 'debug', 'clear', data
|
@log 'Delay', 'debug', 'clear', data
|
||||||
|
@ -67,6 +70,6 @@ class _Singleton extends Spine.Module
|
||||||
clearLevel: (level) ->
|
clearLevel: (level) ->
|
||||||
return if !@levelStack[ level ]
|
return if !@levelStack[ level ]
|
||||||
for key, data of @levelStack[ level ]
|
for key, data of @levelStack[ level ]
|
||||||
@clear( key )
|
@clear( key, level )
|
||||||
@levelStack[level] = {}
|
@levelStack[level] = {}
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,12 @@ class App.Content extends App.Controller
|
||||||
# remove events for page
|
# remove events for page
|
||||||
App.Event.unbindLevel('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
|
# unbind in controller area
|
||||||
@el.unbind()
|
@el.unbind()
|
||||||
@el.undelegate()
|
@el.undelegate()
|
||||||
|
|
73
app/assets/javascripts/app/lib/app_post/interval.js.coffee
Normal file
73
app/assets/javascripts/app/lib/app_post/interval.js.coffee
Normal 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] = {}
|
||||||
|
|
|
@ -120,11 +120,11 @@ class _Singleton extends App.Controller
|
||||||
@send( { action: 'ping' } )
|
@send( { action: 'ping' } )
|
||||||
|
|
||||||
# check if ping is back within 2 min
|
# check if ping is back within 2 min
|
||||||
@clearDelay('websocket-ping-check')
|
@clearDelay('websocket-ping-check', 'ws')
|
||||||
check = =>
|
check = =>
|
||||||
@log 'Websocket', 'notice', 'no websockend ping response, reconnect...'
|
@log 'Websocket', 'notice', 'no websockend ping response, reconnect...'
|
||||||
@close()
|
@close()
|
||||||
@delay check, 120000, 'websocket-ping-check'
|
@delay check, 120000, 'websocket-ping-check', 'ws'
|
||||||
|
|
||||||
pong: ->
|
pong: ->
|
||||||
return if @backend is 'ajax'
|
return if @backend is 'ajax'
|
||||||
|
@ -132,7 +132,7 @@ class _Singleton extends App.Controller
|
||||||
@log 'Websocket', 'debug', 'received websockend ping'
|
@log 'Websocket', 'debug', 'received websockend ping'
|
||||||
|
|
||||||
# test again after 1 min
|
# test again after 1 min
|
||||||
@delay @ping, 60000
|
@delay @ping, 60000, undefined, 'ws'
|
||||||
|
|
||||||
connect: =>
|
connect: =>
|
||||||
return if @backend is 'ajax'
|
return if @backend is 'ajax'
|
||||||
|
@ -171,7 +171,7 @@ class _Singleton extends App.Controller
|
||||||
@queue = []
|
@queue = []
|
||||||
|
|
||||||
# send ping to check connection
|
# send ping to check connection
|
||||||
@delay @ping, 60000
|
@delay @ping, 60000, undefined, 'ws'
|
||||||
|
|
||||||
@ws.onmessage = (e) =>
|
@ws.onmessage = (e) =>
|
||||||
pipe = JSON.parse( e.data )
|
pipe = JSON.parse( e.data )
|
||||||
|
@ -204,11 +204,11 @@ class _Singleton extends App.Controller
|
||||||
message: 'No connection to websocket, trying to reconnect...'
|
message: 'No connection to websocket, trying to reconnect...'
|
||||||
)
|
)
|
||||||
if !@tryToConnect
|
if !@tryToConnect
|
||||||
@delay message, 7000, 'websocket-no-connection-try-reconnect'
|
@delay message, 7000, 'websocket-no-connection-try-reconnect', 'ws'
|
||||||
@tryToConnect = true
|
@tryToConnect = true
|
||||||
|
|
||||||
# try reconnect after 4.5 sec.
|
# try reconnect after 4.5 sec.
|
||||||
@delay @connect, 4500
|
@delay @connect, 4500, undefined, 'ws'
|
||||||
|
|
||||||
@ws.onerror = (e) =>
|
@ws.onerror = (e) =>
|
||||||
@log 'Websocket', 'debug', "ws:onerror", e
|
@log 'Websocket', 'debug', "ws:onerror", e
|
||||||
|
@ -315,5 +315,5 @@ class _Singleton extends App.Controller
|
||||||
@client_id = undefined
|
@client_id = undefined
|
||||||
@_ajaxInit( force: true )
|
@_ajaxInit( force: true )
|
||||||
@_ajaxReceiveWorking = false
|
@_ajaxReceiveWorking = false
|
||||||
@delay @_ajaxReceive, 5000
|
@delay @_ajaxReceive, 5000, undefined, 'ws'
|
||||||
)
|
)
|
||||||
|
|
|
@ -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
|
// i18n
|
||||||
test( "i18n", function() {
|
test( "i18n", function() {
|
||||||
|
@ -271,9 +411,11 @@ test( "config", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
// form
|
// form
|
||||||
|
/*
|
||||||
test( "form", function() {
|
test( "form", function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
// auth
|
// auth
|
||||||
App.Auth.login({
|
App.Auth.login({
|
||||||
|
|
Loading…
Reference in a new issue