Simplify the state checking logic.

This commit is contained in:
Pagan Gazzard 2014-12-11 13:29:59 +00:00 committed by Pablo Carranza Vélez
parent 49d6e3142c
commit e67de4b279

View File

@ -85,7 +85,7 @@ blinkPattern = do ->
# and always have a timeout we can cancel. # and always have a timeout we can cancel.
delay = (ms, fn) -> delay = (ms, fn) ->
timeout = setTimeout(fn, ms) timeout = setTimeout(fn, ms)
start = () -> start = ->
interval = setInterval(utils.blink, 400) interval = setInterval(utils.blink, 400)
delay 2000, -> delay 2000, ->
# Clear the blinks after 2 second # Clear the blinks after 2 second
@ -94,7 +94,7 @@ blinkPattern = do ->
# And then repeat again after another 2 seconds # And then repeat again after another 2 seconds
start() start()
return { return {
start: () -> start: ->
return false if started return false if started
started = true started = true
start() start()
@ -110,15 +110,14 @@ exports.connectivityCheck = do ->
_check = -> _check = ->
utils.checkConnectivity() utils.checkConnectivity()
.then (connected) -> .then (connected) ->
if not connected return if connected == connectivityState
if connectivityState connectivityState = connected
console.log('Waiting for connectivity...') if connectivityState
connectivityState = false console.log('Internet Connectivity: OK')
blinkPattern.start() blinkPattern.stop()
else else
if not connectivityState console.log('Waiting for connectivity...')
console.log('Internet Connectivity: OK') blinkPattern.start()
connectivityState = true .finally ->
blinkPattern.stop()
setTimeout(_check, 10 * 1000) # Every 10 seconds perform this check. setTimeout(_check, 10 * 1000) # Every 10 seconds perform this check.
return _.once(_check) return _.once(_check)