     detail:
       counter: counter
   window.dispatchEvent event
-proxy.addListener 'network.error.retry', handleProxyNetworkErrorRetry
 
 handleProxyNetworkInvalidCode = (code) ->
   event = new CustomEvent 'network.invalid.code',

  locked = false

handleProxyGameOnResponse = (method, path, body, postBody) ->
  # Important! Clone a copy of proxy objects!
  responses.push [method, path, JSON.parse(body), JSON.parse(postBody)]
  resolveResponses() if !locked
proxy.addListener 'game.on.response', handleProxyGameOnResponse

handleProxyGameStart = ->
  window.dispatchEvent new Event 'game.start'
proxy.addListener 'game.start', handleProxyGameStart

handleProxyGamePayitem = ->
  window.dispatchEvent new Event 'game.payitem'
proxy.addListener 'game.payitem', handleProxyGamePayitem

handleProxyNetworkErrorRetry = (counter) ->
  event = new CustomEvent 'network.error.retry',

    detail:
      code: code
  window.dispatchEvent event

handleProxyNetworkError = ->
  window.dispatchEvent new Event 'network.error'

proxyListener =
  'game.on.request': handleProxyGameOnRequest
  'game.on.response': handleProxyGameOnResponse
  'game.start': handleProxyGameStart
  'game.payitem': handleProxyGamePayitem
  'network.error.retry': handleProxyNetworkErrorRetry
  'network.invalid.code': handleProxyNetworkInvalidCode
  'network.error': handleProxyNetworkError

window.addEventListener 'load', ->
  for eventName, handler of proxyListener
    proxy.addListener eventName, handler

window.addEventListener 'unload', ->
  for eventName, handler of proxyListener
    proxy.removeListener eventName, handler
