
############################################################################################################
log     = console.log
after   = ( dt, action ) -> return setTimeout  action, dt
every   = ( dt, action ) -> return setInterval action, dt


#===========================================================================================================
# GLOBALS
#-----------------------------------------------------------------------------------------------------------
tick_dt     =  100 # ms
report_dt   = 1000 # ms
blocking_dt = 2500 # ms
clear_dt    = 3500 # ms
count       = 0
tick_id     = null
t0          = null

#===========================================================================================================
# TIMING
#-----------------------------------------------------------------------------------------------------------
start_timer = -> t0   = new Date() * 1
time_now    = -> return new Date() * 1 - t0

#===========================================================================================================
# BLOCKING
#-----------------------------------------------------------------------------------------------------------
block = ->
  log "(#{time_now()}) event loop blocked"
  loop
    if time_now() > blocking_dt
      break
  log "(#{time_now()}) event loop unblocked"

#===========================================================================================================
# TICKS
#-----------------------------------------------------------------------------------------------------------
tick = ->
  count += 1
  log "(#{time_now()}) tick ##{count}"

#-----------------------------------------------------------------------------------------------------------
start_ticks = ->
  tick_id = every tick_dt, tick

#-----------------------------------------------------------------------------------------------------------
finish_ticks = ->
  if tick_id?
    clearInterval tick_id
    log "(#{time_now()}) interval trigger canceled"

#===========================================================================================================
# REPORTS
#-----------------------------------------------------------------------------------------------------------
start_reporter = ( dt ) ->
  return after dt, ->
    log "(#{time_now()}) @#{dt} count: #{count}"


############################################################################################################
# LET'S GO
#-----------------------------------------------------------------------------------------------------------
start_timer()

start_reporter report_dt * 1
start_reporter report_dt * 2
start_reporter report_dt * 3
start_reporter report_dt * 4

start_ticks()
after clear_dt, finish_ticks

block()
