 
 module.exports =
 
-  emitter: new Emitter()
-
-  onConnected: (cb) -> @emitter.on('connected', cb)
-  onDisconnected: (cb) -> @emitter.on('disconnected', cb)
+  # Messaging
 
   handlers: {}
   callbacks: {}

  requireClient: (f) -> @notConnectedError() or f()
  requireNoClient: (f) -> @connectedError() or f()

  msg: (type, data, f) ->
    return unless @isConnected()
    if f?
      data.callback = @id = @id+1
      @callbacks[@id] = f
      @loading.working()
    @output [type, data]

  handle: (type, f) ->
    @handlers[type] = f

  # TODO: this behaves weirdly because f is evaluated late
  # Should instead evalute f immediately and make sure messages are queued.
  withClient: (f) ->

    if @isBooting
      @isBooting = false
      @loading.done()

  # Management & UI

  connectedError: ->
    if @isConnected()
